#!/bin/bash
#
# Quick & dirty DVD ripper which yields reliable results.
#
# This program requires the following to be installed:
# ogmrip, libdvdcss2, dvdnav4, dvdisaster, mount
#

count_secs=20	# Slow DVD-ROM? Might need to make this longer.
temp_dir=`mktemp -d`	# Where to dump data while decrypting.

   ###
   ### See how the script was called
   ###
case "$1" in
  ( --help|'' )
	echo "Usage: $0 [/path/to/dvd-rom] [/path/to/dvd-image.iso]"
	exit 1
	;;
  *)
	if [ ! -e $1 ] ; then
	echo "The DVD device \"$1\" doesn't seem to exist."
	echo
	echo "Usage: $0 [/path/to/dvd-rom] [/path/to/dvd-image.iso]"
	exit 1
	fi
esac
case "$2" in
  ( --help|'' )
        echo "Usage: $0 [/path/to/dvd-rom] [/path/to/dvd-image.iso]"
        exit 1
        ;;
  *)
	touch $2
        if [ ! -e $2 ] ; then
        echo "The DVD image at \"$2\" couldn't be created."
	echo "Please check your paths and permissions and try again."
	echo
        echo "Usage: $0 [/path/to/dvd-rom] [/path/to/dvd-image.iso]"
        exit 1
        fi
esac

echo
echo "Decrypting DVD at $1 ..."
echo

if [ $count_secs -gt 0 ]
then
   ###
   ### number of seconds greater than zero
   ###

   dvdcpy -o $temp_dir $1 &

   ###
   ### assume that the PID of the command is $$
   ###
   my_PID=$!
   sleep $count_secs
   kill -15 $my_PID
fi

echo
echo
echo "Now dumping to $2 ..."
echo
rm -rf $temp_dir
dvdisaster --ignore-fatal-sense -r -d $1 -i $2
echo
echo "Mounting $2 as a loopback device..."
echo
sudo losetup -f $2
echo
echo "The following images are mounted:"
sudo losetup -a
echo
echo "Finished!"
echo