#!/bin/sh
+PATH=/bin:/usr/bin
+
mailto=carl@ecbaldwin.net
dev="/dev/hdc"
sdev="ATA:1,0,0"
imagedir="/backup/imgs"
-img=$(ls $imagedir | head -n 1)
-echo $img
-
-if [ -n "$img" ]; then
- if cdrecord-wrapper.sh -dao speed=8 dev=$sdev $isoimage; then
- md5sum=$(tempfile)
- dd if=$dev bs=1M count=4440 | md5sum | awk '{print$1}' > $md5sum
- if ! cmp $md5sum $isoimage.md5sum; then
- echo "Failed" | mailx -s "DVD md5sum doesn't match image file!!!" $mailto
- exit 1
- else
- echo $md5sum >> $img.stat
- fi
- else
- echo "Failed" | mailx -s "DVD burning failed!!!" $mailto
- exit 1
- fi
-
- {
- echo "Image md5sum:"
- cat $img.md5sum
- echo
- echo "md5sum of burned disks:"
- cat $img.stat
- } | mailx -s "DVD burned. File it as ${img%.*}" $mailto
-
- if [ "$(wc -l $img.stat 2>/dev/null)" == "2" ]; then
- echo "Burned the image twice. Removing it!"
- rm -f $img{,.md5sum,.stat}
- rmdir $img.mnt
- fi
+# Discover disk images by looking for .img.md5sum files in $imagedir
+imgmd5=$(ls $imagedir/*.img.md5sum | head -n 1)
+
+logfile=$(tempfile)
+
+# If we didn't find an non-empty file then exit gracefully
+[ -z "$imgmd5" ] && exit 0
+[ -s "$imgmd5" ] || exit 0
+
+# Get the name of the disk image by stripping off the tailing '.md5sum'
+img=${imgmd5%.md5sum}
+
+# A little paranoia. Make sure the disk image file is there.
+[ -z "$img" ] && exit 1
+[ -s "$img" ] || exit 1
+
+# Burn the image to a disk.
+cdrecord-wrapper.sh -dao dev=$sdev $isoimage > $logfile 2>&1
+
+if [ 0 != "$?" ]; then
+ echo >&2 "cdrecord failed!"
+ exit 1
+fi
+
+# Now verify the disk by running md5sum on the entire contents of the disk
+md5sum=$(tempfile)
+dd if=$dev bs=1M count=4440 2>$logfile | md5sum | awk '{print$1}' > $md5sum
+
+# Check that the md5sums match
+if ! cmp $md5sum $isoimage.md5sum; then
+ echo "Failed" | mailx -s "DVD md5sum doesn't match image file!!!" $mailto
+ exit 1
+fi
+
+# Record the date in the stat file to indicate that this burn was a success
+date >> $img.stat
+
+{ # Send e-mail
+ echo "Image md5sum:"
+ cat $img.md5sum
+ echo
+ echo "md5sum of burned disks:"
+ cat $img.stat
+ echo
+ cat $logfile
+} | mailx -s "DVD burned. File it as ${img%.*}" $mailto > $logfile 2>&1
+
+# Two lines in the stat file indicate two successful burns. Clean-up the image.
+if [ "$(wc -l $img.stat 2>/dev/null)" == "2" ]; then
+ # Burned the image twice. Removing it and associated files!
+ rm -f $img{,.md5sum,.stat}
+ [ -d $img.mnt ] && rmdir $img.mnt
fi