#!/bin/bash
#Usage: burndir
# http://www.linuxquestions.org/questions/linux-software-2/burning-dvd-and-verifying-no-iso-689438/#post3458795
#Make sure we are running as root (otherwise we can't burn later)
if [ `whoami` != "root" ]
then
echo "This script must be run as root."
exit 1
fi
DIRPATH="$1"
DEVICE="$2"
TMPDIR=`mktemp -d` || exit 1
ISOPIPE="$TMPDIR/pipe.iso"
MKISOERR="$TMPDIR/mkisoerr"
mkfifo "$ISOPIPE"
echo "Burning directory..."
growisofs -dvd-compat -Z "$DEVICE"="$ISOPIPE" &
BURNER=$!
MD5=`mkisofs -r -J -l "$DIRPATH" 2>"$MKISOERR" | tee "$ISOPIPE" | md5sum | egrep -o -i -e "^[0-9a-f]{32}"`
EXTENTS=`egrep -e "^[0-9]+ extents written " "$MKISOERR" | egrep -o -e "^[0-9]+"`
wait "$BURNER"
echo "Wrote $EXTENTS extents with a MD5 hash of $MD5."
echo "Cycling drive tray..."
eject "$DEVICE"
eject -t "$DEVICE"
# Arch Linux seems to have problems without a wait
# here; gives dd: opening `/dev/sr1': No medium found otherwise
sleep 15
echo "Verifying..."
DISCMD5=`dd if="$DEVICE" bs=2048 count="$EXTENTS" | md5sum | egrep -o -i -e "^[0-9a-f]{32}"`
echo "Disc has an MD5 hash of $DISCMD5."
if [ "$MD5" == "$DISCMD5" ]
then
echo "Disc appears to have burned successfully!"
else
echo "Disc appears to have burned with errors."
fi
rm -r "$TMPDIR"