#!/bin/bash
# burncd - contact: mail@tillmann-steinbrecher.de
# GNU GPL

# device to use for writing
device="0,0,0"

# recording speed
speed=32

# additional option for cdrecord
#option=-dummy

# additional option for cdrdao
#daooption=--simulate

# max kb on cd-r, set lower if using 74min media, set higher
# if overburning

maxsize=720408

if [ "$1" == "" ] ; then
    echo "Usage: burncd <directory name>"
    echo "       burns the content of this directory on a CD-R(W)"
    echo "The presence of a .cue/.bin image will be detected and used."
    exit;
fi

cd $1 >/dev/null 2>&1
if [ $? -ne 0 ] ; then
	echo "Error: Can't change to directory $1"
	exit 1;
fi

# check for cue/bin

echo -n "Checking for cue/bin combination..."
ls *.cue >/dev/null 2>&1
if [ $? -eq 0 ] ; then
    ls *.bin >/dev/null 2>&1
    if [ $? -eq 0 ] ; then
	echo "found!"
	cuefile=`ls *.cue | head -n 1`
	echo "Burning CD using cue file $cuefile."
	# burn cue using cdrdao
	cdrdao write --overburn --device $device --speed $speed --driver generic-mmc $daooption $cuefile
	echo "Write finished."
	cd ..
	exit 0;
    else
	echo "no (found .cue, but no .bin)"
    fi
else
    echo "no (no .cue file found)."        
fi

# no cue/bin found, so make isofs on the fly and burn it

echo -n "Checking total size..."
size=`du -s | awk '{ print $1 }'`

if [ $size -gt $maxsize ] ; then
    echo "error (size is $size kb, maximum: $maxsize kb!)"
    cd ..
    exit 1
else
    echo "OK ($size kb)"
    echo "Burning to CD."
    mkisofs --joliet-long -J -r . | cdrecord gracetime=0 -dev=$device -driveropts=burnfree -speed=$speed -fs=32m -eject -data -tao $option -
    echo "Write finished."
    cd ..
fi
