#!/bin/bash
####################################################################################"
#  A small bash utility to burn CD from prokyon3.
#					PH FERME
#
#  With special thanks to Greg Wierzchowski whose "Linux MP3 CD Burning mini-HOWTO"
#  provide all "meat" for the present  script.  Please refer to  
#  http://www.tldp.org/HOWTO/MP3-CD-Burning/index.html for help and explanation.
#
##################################################################################"

cd ~/tmp
#set -x

WAVFILE=""

#  All files to burn are passed as parameters to this script, with proper quoting
#

echo
echo "************  Converting to wav format  **********************************"
echo
for i in "$@";
    do 
    if test "`echo "$i" | grep -i '\.mp3$'`"; then
	echo "mpg123 -v -w \"`basename \"$i\" .mp3`.wav\" \"$i\""
	mpg123 -v -w "`basename "$i" .mp3`.wav" "$i"
	if [ $? -ne 0 ]; then
	    echo ">>>>>>>>>>>>>>>>>  Error running mpg123"
            echo Press RETURN to proceed.
	    read RET
	    exit 1
	fi

        WAVFILE="$WAVFILE \"`basename \"$i\" .mp3`.wav\""
    fi
    if test "`echo "$i" | grep -i '\.ogg$'`"; then
	echo "ogg123 \"$i\" -v -d wav -f \"`basename \"$i\" .ogg`.wav\""
	ogg123 "$i" -v -d wav -f "`basename "$i" .ogg`.wav"   
	if [ $? -ne 0 ]; then
	    echo ">>>>>>>>>>>>>>>>>> Error running ogg123"
            echo Press RETURN to proceed.
	    read RET
	    exit 1
	fi
        WAVFILE="$WAVFILE \"`basename \"$i\" .ogg`.wav\""
    fi
    echo
done

echo
echo "************  Normalizing audio output ********************************"
echo
echo "normalize -m $WAVFILE"
eval normalize -m $WAVFILE
if [ $? -ne 0 ]; then
    echo ">>>>>>>>>>>>>> Error running normalize"
    echo Press RETURN to proceed.
    read RET
    eval rm -vf $WAVFILE
    exit 1
fi


echo
echo "************  Burning CD **********************************************"
echo
echo "cdrecord -v -speed=4 dev=0,0,0 -audio -pad $WAVFILE"
#
# test only do not burn cd
# eval cdrecord -dummy -v -speed=4 dev=0,0,0 -audio -pad $WAVFILE
#
eval cdrecord  -v -speed=4 dev=0,0,0 -audio -pad $WAVFILE
if [ $? -ne 0 ]; then
    echo ">>>>>>>>>>>>  Error running cdrecord"
    echo Press RETURN to proceed.
    read RET
    eval rm -vf $WAVFILE
    exit 1
fi

echo
echo "************  removing temporary files **********************************"
echo
eval rm -vf $WAVFILE
if [ $? -ne 0 ]; then
    echo ">>>>>>>>>>>>  Error removing temporary files"
    echo Press RETURN to proceed.
    read RET
    exit 1
fi

echo Job completed. Press RETURN to proceed.
read RET
exit 0