#!/bin/sh
# burnaudio - contact: mail@tillmann-steinbrecher.de
# GNU GPL

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

# recording speed
speed=32

# additional option for cdrecord (not used when burning .cue files)
#option=-dummy

# maximum number of seconds in an audio CD
# 4800 = 80 minutes
# if you still use 74 minute media, set it lower
# if you wish to overburn, set this higher (at own risk)
maxseconds=4800

#!/bin/sh
if [ "$1" == "" ] ; then
    echo "Usage: burnmp3 <directory name>"
    echo "       burns the mp3s in this directory as audio CD on a CD-R(W)"
    exit;
fi


cd $1 >/dev/null 2>&1

if [ $? -ne 0 ] ; then
	echo "Error: Can't change to directory $1"
	exit 1
fi


echo -n "Checking for mp3 files in $1..."
ls *.mp3 >/dev/null 2>&1
if [ $? -eq 0 ] ; then
		echo "found."
else
		echo "not found, aborting."
		exit 1
fi    

#  check total length

echo -n "Calculating total length of mp3s..."

t=0;

for i in *.mp3 ; do
 s=`mp3info -p %S $i`
 : $((t = $t + $s))
done

seconds=`expr $t % 60`
minutes=`expr $t / 60`

if [ $t -gt $maxseconds ] ; then
    echo "too long ($minutes minutes, $seconds seconds)."
    echo "Error: MP3s in $1 won't fit a single CD."
    exit 1
fi

echo "OK ($minutes minutes, $seconds seconds)."

echo -n "Checking for cue sheet..."

ls *.cue >/dev/null 2>&1

if [ $? -eq 0 ] ; then
    echo "found."
    cuename=`ls *.cue | head -n 1`
    echo "Using cue sheet $cuename."
    
    # write cue sheet, using cdrdao via additional burnmp3cue script
    
    burnmp3cue $device $speed $cuename
    
else
    echo "not found."
    
    # write cd track by track, using cdrecord
    
    for I in *.mp3 ; do
	 echo "Decoding $I and writing to device $device..."
         mpg123 -q --cdr - "$I" | cdrecord gracetime=0 speed=$speed -driveropts=burnfree dev=$device -audio -pad -nofix -tao $option - >/dev/null
    done
    
    echo "Fixating CD..."
    cdrecord gracetime=0 speed=$speed -driveropts=burnfree dev=$device -fix $option >/dev/null

fi

echo "CD recording finished."	    
cd ..

