bf8e3d69870ea6992af72378e90b5c279c3e3706
5 # Add periodic/startup file/directory checks.
6 # Auto check of cd drive perms.
8 echo "Autorip 0.1.3 started."
10 # Set script local variables
13 # The minimum free space to have on the local TMP directory.
17 # The timeout, in seconds (approx), for the cd-tray to be sucked back
18 # in if it's not in already, this isn't exact, as a cycle is a bit more
19 # then a second, but it comes out to about 3 hours.
24 # The cd-rom device to be used.
25 CDROM
="/dev/cdroms/cdrom1"
28 # The folder to be used for all of the actions
32 # The destination of the encoded files.
36 # Defaukt temporary directory.
40 # Remote temporary directory. (currently unused)
41 REMTEMP
="$BASE/remtmp"
44 # Local temporary directory.
48 # The syslog facility to use for logging purposes.
52 # Oher variables (Do not set)
54 # LASTSTART - Last CD in the drive that was encoded.
55 # LASTDONE - Last CD that was finished encoding.
56 # TIME - Approximately how many seconds have passed without having a cd in the drive.
57 # CURRCD - This variable obtains the discid of whatever's in the cd drive.
58 # REENC - The disc-id of an already encoded disc put into the drive.
59 # PROCESS - A short feild of the DISCID that's used for keeping track of running encodes.
60 # PROCDIR - The working directory of a process.
61 # DISCNAME - A process local variable that's just the name of the finished disc.
62 # DONELOCK - A timer for waiting for an add to the DB. Wait no more than 60 seconds.
68 # Reset the status of running.
70 echo 0 > "$BASE/lib/stop"
79 # If the cd in the drive is currently being encoded, don't try to encode it.
80 # Just sleep for 120 seconds and try again later.
82 if [ "$CURCD" != "" ] && [ "$CURCD" = "$LASTSTART" ]
88 # If the CD isn't the same as the last one started, and cdparanoia is running
89 # Assume shit has hit the fan and kill it to end the current process.
90 # The process will die on it's own.
92 elif [ "$CURCD" != "$LASTSTART" ] && [ "$(ps --no-heading -C cdparanoia)" != "" ]
95 logger
-p $LOGDEST.info
"Error ($LASTSTART): Disc removed while encoding!"
96 killall
-KILL cdparanoia
98 # Reset the last start, because it wasn't.
105 # If there's nothing in the drive, and there's no timeout yet, increase.
106 # If something was in the drive, the function would return without encoding it.
108 elif [ $TIME -lt $TIMEOUT ] && [ "$CURCD" = "" ]
114 # If the cd has been sticking out for the timeout period (which is approx three hours).
115 # Suck it back in. This could be used to send a message to the domain, or cause beeping.
117 elif [ $TIME -ge $TIMEOUT ]
126 # Eject the CD if the last CD processed is sucked (or put) back in.
129 # If there is a cd in the drive
130 # Do not encode the last CD to go through the encoding process.
131 # This would cause the same CD to be re-re-rencoded if left in the drive
134 if [ "$CURCD" != "" ] && [ "$CURCD" != "$(< "$BASE/lib
/LASTDONE
")" ] &! [ -d "$TMP/$(echo $CURCD | cut -f 1 -d \ )" ]
137 # If the cd in the drive was already encoded, set reenc to the disc-id. If the user puts the
138 # disc back in the drive, it will re-encode. If it's different, REENC won't matter.
140 if [ "`grep "$CURCD" $BASE/lib/done.db`" = "" ] ||
[ "$REENC" = "$CURCD" ]
142 until [ "$(($(stat -f "$TMP" --format=%a)*4096/1024/1024))" -gt "$FREEMIN" ]
162 # Check for a cd. If cd-discid exits successfully, then set the variable.
163 # If it exits with a 0 (backwards, I know), then blank it.
165 if $
(cd-discid
$CDROM > /dev
/null
2>&1)
168 CURCD
=$
(cd-discid
$CDROM 2>&1)
179 eject
-t $CDROM && getcurcd
181 # If the cd was the last done, assume we timed out.
183 if [ "$CURCD" = "$(< "$BASE/lib
/LASTDONE
")" ]
185 logger
-p $LOGDEST.error
"CD Left (or put back) in drive."
195 # CD Encoder function. The workhorse.
196 # Note: Only call when CD is in drive, else exit.
200 # Check to make sure the cd wasn't changed or removed.
202 if [ "$(cd-discid $CDROM 2>&1)" != "$CURCD" ]
207 # Set this process's id.
209 local PROCESS
="$(echo $CURCD | cut -f 1 -d \ )"
210 local THISDISC
="$CURCD"
211 local PROCDIR
="$TMP/$PROCESS"
221 # Okay, abcde has a bug where it tries to rip data cd's. But the version that fixes it itself has
222 # even more. So instead of trying to install the new version, which doesn't work, we use a command
223 # similar to the one ABCDE uses to determine the number of valid tracks (since it assumes the data
224 # track always occurs at the end, which in some game CD's it doesn't) and pass it to the command
225 # line. Yes, it really is one line.
227 local VALIDTRACKS
="$(cdparanoia -d $CDROM -Q 2>&1 | egrep '^[[:space:]]+[[:digit:]]' | awk '{print $1}' | tr -d ".
" | tr '\n' ' ')"
231 logger
-p $LOGDEST.info
"Info ($PROCESS): Starting encoding."
233 abcde
-Nx -d $CDROM $VALIDTRACKS > /dev
/null
2>&1
236 # If the disc is unknwon, give a warning and unique folder name.
238 if [ -d "$PROCDIR/Unknown Artist - Unknown Album/" ]
240 logger
-p $LOGDEST.warn
"Warning ($PROCESS): Processed successfully, but info unknown."
241 mv "$PROCDIR/Unknown Artist - Unknown Album" "$PROCDIR/Unkown Disc - ID $PROCESS"
245 # If more than one folder is left, or the abcde.process folder is left over,
246 # or if there's nothing in the directory, something has gone WRONG.
248 if [ $
(ls -1 "$PROCDIR" |
wc -l) -gt 1 ] ||
[ "$(ls -1 "$PROCDIR")" = "abcde.$PROCESS" ] ||
[ "$(ls -1 "$PROCDIR")" = "" ]
250 logger
-p $LOGDEST.error
"Error ($PROCESS): More than one folder or something very bad happened."
255 # Note: Don't declare discname until process is finished. Now is fine.
257 local DISCNAME
=`ls -1 "$PROCDIR"`
259 # Put the current disc's ID into a file in the disc's folder, JIC
261 echo "$THISDISC" > "$PROCDIR/$DISCNAME/disc_id"
264 logger
-p $LOGDEST.info
"Info ($PROCESS): Encoding of "$DISCNAME" completed successfully."
267 # Wait for the done database to be ready...
269 until ! [ -f "$BASE/lib/done.lock" ] ||
[ $DONELOCK -ge 60 ]
272 DONELOCK
=$
(($DONELOCK+10))
275 # Then add this disc to it.
276 if [ "`grep "$CURCD" $BASE/lib/done.db`" = "" ]
278 touch "$BASE/lib/done.lock"
279 cat "$BASE/lib/done.db" "$PROCDIR/$DISCNAME/disc_id" > "$BASE/lib/done.new"
280 mv "$BASE/lib/done.new" "$BASE/lib/done.db"
281 rm "$BASE/lib/done.lock"
283 logger
-p $LOGDEST.notice
"Not adding $PROCESS to done.db"
286 # Move the process's completed directory into the done folder.
288 if ! [ -d "$DONE/$DISCNAME" ]
290 mv -f "$PROCDIR/$DISCNAME" "$DONE/"
291 elif [ -d "$DONE/$DISCNAME" ]
293 rm -r "$DONE/$DISCNAME"
294 mv -f "$PROCDIR/$DISCNAME" "$DONE/"
297 # A file must be used here because the variable is lost when the function exits sometimes.
299 echo "$THISDISC" > "$BASE/lib/LASTDONE"
301 # If I was the last disc started, and I'm done, clear the laststart variable so the last disc
302 # encoded can be re-encoded if they really want to.
304 if [ "$THISDISC" = "$LASTSTART" ]
310 # Remove the process temp directory and call it a day.
316 until [[ $
(< "$BASE/lib/stop") == 1 ]]