Skip to content

Instantly share code, notes, and snippets.

@jfryman
Last active July 26, 2017 17:00
Show Gist options
  • Save jfryman/4d0682c0ecd460283cab9f36d47a1a78 to your computer and use it in GitHub Desktop.
Save jfryman/4d0682c0ecd460283cab9f36d47a1a78 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
DEVICE=/dev/ttyUSB0
CDROM=/dev/cdrom
EPOCH=$(date +%s)
CMD_CALIBRATE=C
CMD_GRAB_NEW_DISK=I
CMD_EJECT_DISK_SUCCESS=A
CMD_GRAB_FROM_DRIVE=G
CMD_REJECT_DISK=R
open_cdrom_drive() {
echo "Attempting to open CD Drive"
trayopen $CDROM > /dev/null 2>&1
if [ $? -ne 0 ]; then
eject_until_open
fi
}
close_cdrom_drive() {
echo "Attempting to close CD Drive"
trayopen $CDROM > /dev/null 2>&1
if [ $? -ne 1 ]; then
eject_until_open
fi
local _dvd_sleep=1
until lsdvd -c /dev/cdrom > /dev/null 2>&1;
do
echo "Sleeping to give CDROM drive a chance to wake up"
backoff $_dvd_sleep
_dvd_sleep=$(expr $_dvd_sleep + 1)
done
}
backoff() {
local _count=$1
local _sleep=$(expr $_count \* 2)
echo "This is attempt # $_count"
echo "Sleeping for $_sleep seconds"
sleep $_sleep
}
eject_until_open() {
local _loop_counter=1
until eject -T; do
echo "unable to eject disk, waiting for spindown"
backoff $_loop_counter
_loop_counter=$(expr $_loop_counter + 1)
done
}
reset_grabber_head() {
echo "Attempting to reset grabber head"
send_command $CMD_CALIBRATE "Unable to calibrate device"
}
grab_new_disk() {
echo "Grabbing new disk"
open_cdrom_drive
send_command $CMD_GRAB_NEW_DISK "Unable to grab new disk"
}
remove_disk_success() {
echo "Success, removing disk"
open_cdrom_drive
send_command $CMD_EJECT_DISK_SUCCESS "Failure to eject CD from drive"
}
remove_disk_fail() {
echo "Failed, rejecting disk"
open_cdrom_drive
send_command $CMD_GRAB_FROM_DRIVE "Unable to grab disk from drive"
close_cdrom_drive
send_command $CMD_REJECT_DISK "Unable to reject disk"
}
send_command() {
local _command=$1
shift
local _error=$@
RESPONSE=$(serial_drive $DEVICE $_command)
if [ "$RESPONSE" != "X" ]; then
echo $_error
exit 1
fi
}
get_dvd_title() {
local _title=""
local _dvd_info=$(lsdvd -c $CDROM)
if [ $? -ne 0 ];
then echo "Unable to get DVD info"
exit 1
else
_title=$(echo $_dvd_info | grep "Disc Title" | gawk '{print $3}' | tr '[:upper:]' '[:lower:]' | tr ' ' '_')
fi
echo $_title
}
get_dvd_sha() {
sha1sum $CDROM | awk '{print $1}'
}
dvd_already_ripped() {
local _sha=$1
echo "Checking for $_sha"
if ssh opt@einstein stat /opt/media/isoimages/$_sha.iso \> /dev/null 2\>\&1;
then true;
else false;
fi;
}
dd_iso_to_einstein() {
echo "Getting DVD Title..."
local _dvd_title=$1
echo "Getting DVD Sha1Sum..."
local _dvd_shasum=$(get_dvd_sha)
if dvd_already_ripped $_dvd_shasum;
then
echo "DVD has already been ripped"
exit 1
else
echo "DDing $_dvd_title ($_dvd_shasum) to einstein"
dd if=$CDROM | ssh opt@einstein "dd of=/opt/media/isoimages/${_dvd_shasum}.iso"
if [ $? -ne 0 ]; then
echo "Unable to rip $_remote_file"
exit 1
fi
log_success $_dvd_shasum $_dvd_title
fi
}
log_success() {
local _shasum=$1
local _title=$2
if [ ! -f riplog.txt ]; then
touch riplog.txt
fi
echo "$_shasum $_title" >> riplog.txt
}
rip_dvd_to_file() {
local _dvd_title=$(get_dvd_title)
dd_iso_to_einstein $_dvd_title
}
reset_grabber_head
open_cdrom_drive
grab_new_disk
if [ $? -ne 0 ]; then
exit 255
fi
close_cdrom_drive
rip_dvd_to_file
if [ $? -ne 0 ];
then remove_disk_fail
else remove_disk_success
fi
#!/usr/bin/env bash
RIP=YES
while [ "$RIP" = "YES" ]; do
./rip-dvd-to-einstein
if [ $? -eq 255 ]; then
RIP=NO
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment