#!/bin/bash
# burniso - burns ISO images
# Informatik Fakultät der Universität Freiburg
# Author: Pablo Yanez Trujillo <yanezp@informatik.uni-freiburg.de>

# color list
# GOOD (green), WARN (yellow), BAD (red), HILITE (cyan), BRACKET (blue), NORMAL (xterm default)

CDRECORD="/usr/bin/cdrecord"
GROWISOFS="/usr/bin/growisofs"

function ebegin()
{
        echo -e "$@" ...
}

function einfo()
{
        echo -e "$@"
}
function ewarn()
{
        echo -e "$@"
}
function eerror()
{
        echo -e "$@"
}
function eend()
{
        local retval=${1:-0}
        shift
        return ${retval}
}

# inclduing Gentoo Layout functions
[[ -f /sbin/functions.sh ]] && . /sbin/functions.sh

function usage()
{
cat << EOF
${BAD}!!!${NORMAL} $@
usage: ${HILITE}`basename $0` ${GOOD}type ${WARN}isofile ${BRACKET}[${BAD}-r${BRACKET}]${NORMAL}
where ${GOOD}type${NORMAL} is one of
${GOOD}cd${NORMAL}		burns a CD ISO image. Together with the option
		${BAD}-r${NORMAL} `basename $0` clears the CD-RW disc before
		burning the ISO image
${GOOD}dvd${NORMAL}		burns a DVD ISO image

Report bugs to Pablo Yanez Trujillo <yanezp@informatik.uni-freiburg.de>
EOF
}

function burn()
{
	case "$1" in
		cd)
		echo ${CDRECORD} -v gracetime=2 dev=/dev/hdc speed=52 -dao driveropts=burnfree -eject -data $2
		;;
		
		dvd)
		${GROWISOFS} -Z /dev/hdc=$2 -dvd-compat ; eject /dev/hdc
		;;

		*)
		echo false
	esac
}

function blank_cd()
{
	echo ${CDRECORD} dev=/dev/hdc blank=fast
}


if [ -z "$1" ] ; then
	usage "No type or unknown type given"
	exit 1
fi

CMDLINE=true

case "$1" in
	cd)
		if [ -z "$2" ] ; then
			usage "iso filename not given"
			exit 1
		fi
		if [ -n "$3" ] ; then
			if [ "$3" == "-r" ] ; then
				CMDLINE="`blank_cd`"
			else
				usage "unknown option for type \"cd\""
				exit 1
			fi
		fi

		ERR=true

		ebegin "Ereasing the cd-rw"
		${CMDLINE}
		eend $? "Cannot erease the cd-rw" || exit $?

		CMDLINE="`burn $1 $2`"

		ebegin "Burning $2. Please wait"
		${CMDLINE}
		eend $? "Cannot burn $2" || exit $?
		;;
	dvd)
		if [ -z "$2" ] ; then
			usage "iso filename not given"
			exit 1
		fi

		ebegin "Burning $2. Please wait"
		burn $1 $2
		eend $? "Cannot burn $2" || exit $?
		;;
	*)
		usage "Unknown type"
		exit 1
		;;
esac
