#!/bin/sh
#------------------------------------------------------------------------------
# burncdda is a shell script for burning audio cd's with
# cdrdao and cdrecord
#
# (C) 2001 by Thorsten Muehlfelder
#
# This program is free software; you can redistribute it and/or 
# modify it under the terms of the GNU General Public License 
# as published by the Free Software Foundation; either version 2 
# of the License, or (at your option) any later version. 
# 
# This program is distributed in the hope that it will be useful, 
# but WITHOUT ANY WARRANTY; without even the implied warranty of 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
# GNU General Public License for more details. 
# 
# You should have received a copy of the GNU General Public License 
# along with this program; if not, write to the Free Software 
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
#------------------------------------------------------------------------------

# catch SIGINT and SIGTERM, remove TEMPFILE
trap 'test -e $TEMPFILE && rm -f $TEMPFILE; test -e $TEXTFILE && rm -f $TEXTFILE; exit 0' 2 15

# cleanup
rm -f /tmp/burncdda.??????

# some variables
VERSION="1.3.3"
BACKTITLE="burnCDDA $VERSION"
TEMPFILE=`mktemp /tmp/burncdda.XXXXXX`
TEXTFILE=`mktemp /tmp/burncdda.XXXXXX`
ERROR="An error ocurred:"
STARTDIR=`pwd`
PLAYLIST=""
LINE=""
WAVNAME=""
MAINLEV="0"
LEV1="0"
TYP="mp3"
NORMSET="0"
EXT=""
LL="0"
M3U="m3u"
IFSBAK="$IFS"
FILES=""

# sets integer variables
declare -i N=0
declare -i LN=1
declare -i MAXN=0
declare -i MAXLN=1
declare -i LNSELECTED=1
declare -i MAXLNSELECTED=1
declare -i FILESIZE=0
declare -i FILETIME=0
declare -i CDTIME=0
declare -i MINUTES=0
declare -i SECONDS=0
declare -i CORRUPTED=0

# read config file
if [ -r /etc/burncdda.conf ]; then
	source /etc/burncdda.conf
else
	dialog --backtitle "$BACKTITLE" --title "$ERROR" \
	       --msgbox "Check your installation! You don't have a config file." 6 60
	clear
	exit 1
fi
if [ -r ~/.burncdda ]; then
	source ~/.burncdda
fi

# is /etc/burncdda.conf configured?
if [ $FIRSTRUN = "1" ]; then
	dialog --backtitle "$BACKTITLE" --title "$ERROR" \
	       --msgbox "Please edit the config file: /etc/burncdda.conf" 6 60
	clear
	exit 1
fi

# create IMGDIR
if [ ! -e "$IMGDIR" ]; then
	echo "Creating '$IMGDIR'"
	mkdir -p "$IMGDIR"
	echo "Press Enter"
	read JUNK
fi

#
# begin functions
#

# decode and normalize
function decodenorm()
{
mkdir "$IMGDIR/$WAVNAME"
N=0
LN=1
while [ $LN -le $MAXLN ]; do
	LINE=`head -$LN "$PLAYLIST" | tail -1`
	LINE=`echo "$LINE" | sed s/$'\r'//`
	EXT=`echo "$LINE" | head -c 1`
	if [ "$EXT" = "#" ]; then
		LN=$LN+1
		continue
	fi
	echo "File: $LINE"
	TYP=`echo "$LINE" | tail -c 4`
	TYP=`echo "$TYP" | tr "A-Z" "a-z"`
	N=$N+1
	LN=$LN+1
	if [ $N -le 9 ]; then
		LL="0"
	else
		LL=""
	fi
	if [ -r "$LINE" ]; then
		if [ "$TYP" = "mp3" ]; then
			echo "mpg123..."
			nice -n $NICE mpg123 -r 44100 --stereo -q --wav "$IMGDIR/$WAVNAME/$WAVNAME.$LL$N.wav" "$LINE" || decerror
			echo "--> $WAVNAME.$LL$N.wav"
			if [ $NORMSET = 1 ]; then
				echo "normalize..."
				nice -n $NICE normalize -q --amplitude=$NORMAMP "$IMGDIR/$WAVNAME/$WAVNAME.$LL$N.wav" || decerror
				echo "--> $WAVNAME.$LL$N.wav was normalized to $NORMAMP"
			fi
			timecount "$IMGDIR/$WAVNAME/$WAVNAME.$LL$N.wav"
		elif [ "$TYP" = "ogg" ]; then
			echo "oggdec..."
			nice -n $NICE oggdec -Q -o "$IMGDIR/$WAVNAME/$WAVNAME.$LL$N.wav" "$LINE" || decerror
			echo "--> $WAVNAME.$LL$N.wav"
			if [ $NORMSET = 1 ]; then
				echo "normalize..."
				nice -n $NICE normalize -q --amplitude=$NORMAMP "$IMGDIR/$WAVNAME/$WAVNAME.$LL$N.wav" || decerror
				echo "--> $WAVNAME.$LL$N.wav was normalized to $NORMAMP"
			fi
			timecount "$IMGDIR/$WAVNAME/$WAVNAME.$LL$N.wav"
		elif [ "$TYP" = "mpc" ]; then
			echo "mppdec..."
			nice -n $NICE mppdec --silent "$LINE" "$IMGDIR/$WAVNAME/$WAVNAME.$LL$N.wav" || decerror
			echo "--> $WAVNAME.$LL$N.wav"
			if [ $NORMSET = 1 ]; then
				echo "normalize..."
				nice -n $NICE normalize -q --amplitude=$NORMAMP "$IMGDIR/$WAVNAME/$WAVNAME.$LL$N.wav" || decerror
				echo "--> $WAVNAME.$LL$N.wav was normalized to $NORMAMP"
			fi
			timecount "$IMGDIR/$WAVNAME/$WAVNAME.$LL$N.wav"
		elif [ "$TYP" = "wav" ]; then
			echo "sox..."
			nice -n $NICE sox "$LINE" -r 44100 -c 2 "$IMGDIR/$WAVNAME/$WAVNAME.$LL$N.wav" || decerror
			echo "--> $WAVNAME.$LL$N.wav"
			if [ $NORMSET = 1 ]; then
				echo "normalize..."
				nice -n $NICE normalize -q --amplitude=$NORMAMP "$IMGDIR/$WAVNAME/$WAVNAME.$LL$N.wav" || decerror
				echo "--> $WAVNAME.$LL$N.wav was normalized to $NORMAMP"
			fi
			timecount "$IMGDIR/$WAVNAME/$WAVNAME.$LL$N.wav"
		else
			echo "Track $LL$N: $TYP is not a supportet format. Skipping!"
			decerror
		fi
	else
		echo "$LINE does not exist or is not readable"
		decerror
	fi
	echo ""
	echo ""
done
MINUTES=($CDTIME/60)
SECONDS=`expr $CDTIME % 60`
if [ $SECONDS -le 9 ]; then
	echo "Disc Length: $MINUTES:0$SECONDS"
else
	echo "Disc Length: $MINUTES:$SECONDS"
fi
}

# counts files in playlist (MAXN), counts lines in playlist (MAXLN)
function filecount()
{
clear
MAXLN=`awk 'END {print FNR}' "$PLAYLIST"`
LN=1
MAXN=0
echo -n "Counting Files in playlist"
while [ $LN -le $MAXLN ]; do
	LINE=`head -$LN "$PLAYLIST" | tail -1`
	EXT=`echo "$LINE" | head -c 1`
	if [ "$EXT" != "#" ]; then
		MAXN=$MAXN+1
	fi
	LN=$LN+1
	echo -n "."
done
echo ""
}

# calculates length of single tracks and adds them to CDTIME
function timecount()
{
FILESIZE=`ls -l "$1" | awk '{print $5}'`
FILETIME=($FILESIZE/176400)
MINUTES=($FILETIME/60)
SECONDS=`expr $FILETIME % 60`
if [ $SECONDS -le 9 ]; then
	echo "Track Length: $MINUTES:0$SECONDS"
else
	echo "Track Length: $MINUTES:$SECONDS"
fi
CDTIME=($CDTIME+$FILETIME)
}

# errormessage of decodenorm
function decerror()
{
echo "WARNING: An error occurred!"
echo "Press Enter"
read JUNK
echo ""
continue
}

# errormessage of checkm3u
function checkerror()
{
CORRUPTED=$CORRUPTED+1
echo ""
echo "This File is corrupted and can cause bad sound quality!"
echo "Press Enter"
read JUNK
echo ""
continue
}

# checks m3u playlist
function checkm3u()
{
N=0
LN=1
CORRUPTED=0
while [ $LN -le $MAXLN ]; do
	LINE=`head -$LN "$PLAYLIST" | tail -1`
	LINE=`echo "$LINE" | sed s/$'\r'//`
	EXT=`echo "$LINE" | head -c 1`
	if [ "$EXT" = "#" ]; then
		LN=$LN+1
		continue
	fi
	echo "File: $LINE"
	TYP=`echo "$LINE" | tail -c 4`
	TYP=`echo "$TYP" | tr "A-Z" "a-z"`
	N=$N+1
	LN=$LN+1
	if [ -r "$LINE" ]; then
		if [ $TYP = "mp3" ]; then
			echo "mp3_check..."
			mp3_check "$LINE" > /dev/null || checkerror
			echo "OK"
		elif [ $TYP = "ogg" ]; then
			echo "ogginfo..."
			ogginfo "$LINE" | grep Corrupted && checkerror
			echo "OK"
		elif [ $TYP = "mpc" ]; then
			echo "MPC file. Skipping!"
		elif [ $TYP = "wav" ]; then
			echo "WAV file. Skipping!"
		else
			echo "$TYP is not a supported format. Skipping!"
		fi
	else
		echo "$LINE does not exist or is not readable"
		echo "Press Enter"
		read JUNK
		echo ""
		continue
	fi
	echo ""
	echo ""
done
echo ""
echo "mp3_check/ogginfo found $CORRUPTED corrupted files."
}

#
# functions end
#

#
# start command line options
#
case $1 in

	-c)
	PLAYLIST="$2"
	M3U=`echo "$PLAYLIST" | tail -c 4`
	M3U=`echo "$M3U" | tr "A-Z" "a-z"`
	if [ ! -r "$PLAYLIST" ]; then
		dialog --backtitle "$BACKTITLE" --title "$ERROR" \
		    --msgbox "'$PLAYLIST' does not exist or is not readable" 6 60
		clear
		exit 1
	fi
	if [ "$M3U" != "m3u" ]; then
		dialog --backtitle "$BACKTITLE" --title "$ERROR" \
		    --msgbox "'$PLAYLIST' is not a M3U file" 6 60
		clear
		exit 1
	fi
	filecount
	echo "There are $MAXN files in '$PLAYLIST'"
	echo "Press Enter"
	read JUNK
	echo ""
	checkm3u
	echo ""
	exit 0
	;;

	-b)
	PLAYLIST="$2"
	M3U=`echo "$PLAYLIST" | tail -c 4`
	M3U=`echo "$M3U" | tr "A-Z" "a-z"`
	if [ ! -r "$PLAYLIST" ]; then
		dialog --backtitle "$BACKTITLE" --title "$ERROR" \
		    --msgbox "'$PLAYLIST' does not exist or is not readable" 6 60
		clear
		exit 1
	fi
	if [ "$M3U" != "m3u" ]; then
		dialog --backtitle "$BACKTITLE" --title "$ERROR" \
		    --msgbox "'$PLAYLIST' is not a M3U file" 6 60
		clear
		exit 1
	fi
	LEV1="0"
	while [ $LEV1 = "0" ]; do
		# enter wav names
		dialog --backtitle "$BACKTITLE" --title "Burn Audio-CD from playlist" \
		       --inputbox "Enter name of WAV files (without .wav):" 8 60 2> $TEMPFILE
		if [ $? = "1" ]; then
			LEV1="1"
			clear
			continue
		fi
		WAVNAME=`cat $TEMPFILE`
		if [ -e "$IMGDIR/$WAVNAME" ]; then
			dialog --backtitle "$BACKTITLE" --title "$ERROR" \
			    --msgbox "'$WAVNAME' already exists. Choose another name!" 6 60
			continue
		fi
		# normalize? yes/no
		dialog --backtitle "$BACKTITLE" --title "Burn Audio-CD from playlist" \
		       --yesno "Do you want to normalize the WAVs to $NORMAMP?" 6 60
		if [ $? = "0" ]; then
			NORMSET="1"
		else
			NORMSET="0"
		fi
		# count files
		filecount
		# checks playlist for more than 99 tracks
		if [ $MAXN -gt 99 ]; then
			dialog --backtitle "$BACKTITLE" --title "$ERROR" \
			       --msgbox "There are $MAXN files in '$PLAYLIST' but you cannot burn more than 99 tracks." 6 60
			LEV1="1"
			clear
			continue
		else
		echo "There are $MAXN files in '$PLAYLIST'"
		echo "Press Enter"
		read JUNK
		fi
		echo ""
		# decode and normalize
		decodenorm
		echo ""
		echo "Insert CD-R and press Enter"
		read JUNK
		cdrecord "dev=$DEVICE" "speed=$WRITESPEED" $DRIVEROPTS $OTHERREC "$IMGDIR/$WAVNAME/$WAVNAME".*.wav
		echo ""
		echo "Press Enter"
		read JUNK
		LEV1="1"
	done
	exit 0
	;;

	"")
	;;

	*)
	echo "Usage: burncdda [option] file"
	echo "   -b  Burn M3U playlist"
	echo "   -c  Check files in M3U playlist"
	echo ""
	echo "Without command line options, the console GUI is started"
	echo "Try 'man burncdda' for more information!"
	echo "$BACKTITLE (C) by Thorsten Muehlfelder"
	exit 0
	;;

esac

#
# end command line options
#

#
# main menu begin
#
clear
while [ $MAINLEV = "0" ]; do
	LEV1="0"
	CDTIME=0
	cd "$STARTDIR"
	dialog --backtitle "$BACKTITLE" --title "Main Menu" --menu "Which option would you like?" 15 60 8 \
		1 "Read CD image" \
		2 "Copy CD" \
		3 "Burn M3U playlist" \
		4 "Convert M3U playlist to WAVs" \
		5 "Burn WAV/Image files" \
		6 "Check files in M3U playlist" \
		7 "Delete WAV/Image files" \
		8 "Exit" 2> $TEMPFILE

	# if cancel exit menu
	if [ $? = "1" ]; then
		MAINLEV="1"
	fi
	WAHL=`cat $TEMPFILE`
	rm -f $TEMPFILE
	# menu end

	case $WAHL in

		1)
		while [ $LEV1 = "0" ]; do
			dialog --backtitle "$BACKTITLE" --title "Read Audio-CD" \
				--inputbox "Enter CD-image filename (without .bin):" 8 60 2> $TEMPFILE
			if [ $? = "1" ]; then
				LEV1="1"
				continue
			fi
			CDNAME=`cat $TEMPFILE`
			cd "$IMGDIR"
			if [ -e "$CDNAME" ]; then
				dialog --backtitle "$BACKTITLE" --title "$ERROR" \
					--msgbox "'$CDNAME' already exists. Choose another name!" 6 60
				continue
			fi
			mkdir "$CDNAME"
			cd "$CDNAME"
			dialog --backtitle "$BACKTITLE" --title "Read Audio-CD" \
				--msgbox "\nInsert Audio-CD and press OK to read" 7 40
			clear
			nice -n $NICE cdrdao read-cd $DEVRDAO $DRVRDAO $PARANOIA --datafile "$CDNAME.bin" "$CDNAME.toc"
			echo ""
			echo "Press Enter"
			read JUNK
			LEV1="1"
		done
		;;

		2)
		while [ $LEV1 = "0" ]; do
			dialog --backtitle "$BACKTITLE" --title "Copy Audio-CD" \
				--inputbox "Enter CD-image filename (without .bin):" 8 60 2> $TEMPFILE
			if [ $? = "1" ]; then
				LEV1="1"
				continue
			fi
			CDNAME=`cat $TEMPFILE`
			cd "$IMGDIR"
			if [ -e "$CDNAME.toc" ]; then
				dialog --backtitle "$BACKTITLE" --title "$ERROR" \
					--msgbox "'$CDNAME.toc' already exists. Choose another name!" 6 60
				continue
			fi
			mkdir "$CDNAME"
			cd "$CDNAME"
			dialog --backtitle "$BACKTITLE" --title "Copy Audio-CD" \
				--msgbox "\nInsert Audio-CD and press OK to read" 7 40
			clear
			nice -n $NICE cdrdao read-cd $DEVRDAO $DRVRDAO $PARANOIA --datafile "$CDNAME.bin" "$CDNAME.toc"
			echo ""
			echo "Insert CD-R and press Enter to burn"
			read JUNK
			echo ""
			cdrdao write "--device $DEVICE" $DRVWDAO --speed $WRITESPEED "$CDNAME.toc"
			echo ""
			echo "Press Enter"
			read JUNK
			LEV1="1"
		done
		;;

		3)
		while [ $LEV1 = "0" ]; do
			FILES=`find *.m3u -maxdepth 0 -printf %f"\255"playlist"\255"off"\255"`
			if [ "$FILES" = "" ]; then
				dialog --backtitle "$BACKTITLE" --title "Burn Audio-CD from playlist" \
					--msgbox "There are no M3U playlists in current directory." 7 60
				LEV1="1"
				continue
			fi
			IFS="$(echo -e "\255")"
			dialog --backtitle "$BACKTITLE" --title "Burn Audio-CD from playlist" --separate-output \
				--checklist "Select playlist(s) with Spacebar"  16 60 8 $FILES 2> $TEMPFILE
			if [ $? = "1" ]; then
				LEV1="1"
				continue
			fi
			IFS="$IFSBAK"
			MAXLNSELECTED=`awk 'END {print FNR}' "$TEMPFILE"`
			if [ "$MAXLNSELECTED" = "0" ]; then
				dialog --backtitle "$BACKTITLE" --title "$ERROR" \
					--msgbox "Nothing to burn. Please select something with Spacebar first." 7 60
				continue
			fi
			LNSELECTED=1
			while [ $LNSELECTED -le $MAXLNSELECTED ]; do
				LINE=`head -$LNSELECTED "$TEMPFILE" | tail -1`
				LINE=`echo "$LINE" | sed s/$'\r'//`
				PLAYLIST=$LINE
				if [ ! -r "$PLAYLIST" ]; then
					dialog --backtitle "$BACKTITLE" --title "$ERROR" \
					    --msgbox "'$PLAYLIST' does not exist or is not readable" 6 60
					LNSELECTED=$LNSELECTED+1
					continue
				fi
				# WAVNEME = PLAYLIST without .m3u
				WAVNAME=`echo "$LINE" | head --bytes=-5`
				if [ -e "$IMGDIR/$WAVNAME" ]; then
					dialog --backtitle "$BACKTITLE" --title "$ERROR" \
						--msgbox "Playlist $LNSELECTED of $MAXLNSELECTED:\n'$WAVNAME' already exists. Please delete the existing WAVs!" 7 60
					LNSELECTED=$LNSELECTED+1
					continue
				fi
				dialog --backtitle "$BACKTITLE" --title "Burn Audio-CD from playlist" \
					--yesno "Playlist $LNSELECTED of $MAXLNSELECTED:\nDo you want to normalize the '$WAVNAME' WAVs to $NORMAMP?" 7 60
				if [ $? = "0" ]; then
					NORMSET="1"
				else
					NORMSET="0"
				fi
				filecount
				# checks playlist for more than 99 tracks
				if [ $MAXN -gt 99 ]; then
					dialog --backtitle "$BACKTITLE" --title "$ERROR" \
						--msgbox "Playlist $LNSELECTED of $MAXLNSELECTED:\n'$PLAYLIST'\nThere are $MAXN but you cannot burn more than 99 tracks." 8 60
					LEV1="1"
					LNSELECTED=$LNSELECTED+1
					continue
				else
				echo -e "There are $MAXN files in playlist $LNSELECTED of $MAXLNSELECTED:\n'$PLAYLIST'"
				echo "Press Enter"
				read JUNK
				fi
				echo ""
				# set CDTIME=0 for correct calculation for each playlist
				CDTIME=0
				# decode and normalize
				decodenorm
				echo ""
				echo "Burn CD $LNSELECTED of $MAXLNSELECTED: '$WAVNAME'"
				echo "Insert CD-R and press Enter"
				read JUNK
				cdrecord "dev=$DEVICE" "speed=$WRITESPEED" $DRIVEROPTS $OTHERREC "$IMGDIR/$WAVNAME/$WAVNAME".*.wav
				echo ""
				echo "CD $LNSELECTED of $MAXLNSELECTED: '$WAVNAME' finished"
				echo "Press Enter"
				read JUNK
				LNSELECTED=$LNSELECTED+1
			done
			LEV1="1"
		done
		;;

		4)
		while [ $LEV1 = "0" ]; do
			FILES=`find *.m3u -maxdepth 0 -printf %f"\255"playlist"\255"off"\255"`
			if [ "$FILES" = "" ]; then
				dialog --backtitle "$BACKTITLE" --title "Convert M3U playlist to WAVs" \
					--msgbox "There are no M3U playlists in current directory." 7 60
				LEV1="1"
				continue
			fi
			IFS="$(echo -e "\255")"
			dialog --backtitle "$BACKTITLE" --title "Convert M3U playlist to WAVs" --separate-output \
				--checklist "Select playlist(s) with Spacebar"  16 60 8 $FILES 2> $TEMPFILE
			if [ $? = "1" ]; then
				LEV1="1"
				continue
			fi
			IFS="$IFSBAK"
			MAXLNSELECTED=`awk 'END {print FNR}' "$TEMPFILE"`
			if [ "$MAXLNSELECTED" = "0" ]; then
				dialog --backtitle "$BACKTITLE" --title "$ERROR" \
					--msgbox "Nothing to convert. Please select something with Spacebar first." 7 60
				continue
			fi
			LNSELECTED=1
			while [ $LNSELECTED -le $MAXLNSELECTED ]; do
				LINE=`head -$LNSELECTED "$TEMPFILE" | tail -1`
				LINE=`echo "$LINE" | sed s/$'\r'//`
				PLAYLIST=$LINE
				if [ ! -r "$PLAYLIST" ]; then
					dialog --backtitle "$BACKTITLE" --title "$ERROR" \
					    --msgbox "'$PLAYLIST' does not exist or is not readable" 6 60
					LNSELECTED=$LNSELECTED+1
					continue
				fi
				# WAVNEME = PLAYLIST without .m3u
				WAVNAME=`echo "$LINE" | head --bytes=-5`
				if [ -e "$IMGDIR/$WAVNAME" ]; then
					dialog --backtitle "$BACKTITLE" --title "$ERROR" \
						--msgbox "Playlist $LNSELECTED of $MAXLNSELECTED:\n'$WAVNAME' already exists. Please delete the existing WAVs!" 7 60
					LNSELECTED=$LNSELECTED+1
					continue
				fi
				dialog --backtitle "$BACKTITLE" --title "Convert M3U playlist to WAVs" \
					--yesno "Playlist $LNSELECTED of $MAXLNSELECTED:\nDo you want to normalize the '$WAVNAME' WAVs to $NORMAMP?" 7 60
				if [ $? = "0" ]; then
					NORMSET="1"
				else
					NORMSET="0"
				fi
				filecount
				# checks playlist for more than 99 tracks
				if [ $MAXN -gt 99 ]; then
					dialog --backtitle "$BACKTITLE" --title "$ERROR" \
						--msgbox "Playlist $LNSELECTED of $MAXLNSELECTED:\n'$PLAYLIST'\nThere are $MAXN but you cannot burn more than 99 tracks." 8 60
					dialog --backtitle "$BACKTITLE" --title "Convert M3U playlist to WAVs" \
						--yesno "Do you still want to continue?" 7 60
					if [ $? = "0" ]; then
						echo "OK, let's see what happens..."
					else
						LEV1="1"
						LNSELECTED=$LNSELECTED+1
						continue
					fi
				else
				echo -e "There are $MAXN files in playlist $LNSELECTED of $MAXLNSELECTED:\n'$PLAYLIST'"
				echo "Press Enter"
				read JUNK
				fi
				echo ""
				# set CDTIME=0 for correct calculation for each playlist
				CDTIME=0
				# decode and normalize
				decodenorm
				echo "Press Enter"
				read JUNK
				LNSELECTED=$LNSELECTED+1
			done
			LEV1="1"
		done
		;;

		5)
		FILES=""
		while [ $LEV1 = "0" ]; do
			FILES=`find "$IMGDIR" -mindepth 1 -maxdepth 1 -printf %f"\255"image"\255"off"\255"`
			if [ "$FILES" = "" ]; then
				dialog --backtitle "$BACKTITLE" --title "Delete WAV/Image files" \
					--msgbox "There are no files in your image directory." 7 60
				LEV1="1"
				continue
			fi
			IFS="$(echo -e "\255")"
			dialog --backtitle "$BACKTITLE" --title "Burn WAV/Image files" --separate-output \
				--checklist "Select file(s) with Spacebar"  16 60 8 $FILES 2> $TEMPFILE
			if [ $? = "1" ]; then
				LEV1="1"
				continue
			fi
			IFS="$IFSBAK"
			MAXLNSELECTED=`awk 'END {print FNR}' "$TEMPFILE"`
			if [ "$MAXLNSELECTED" = "0" ]; then
				dialog --backtitle "$BACKTITLE" --title "$ERROR" \
					--msgbox "Nothing to burn. Please select something with Spacebar first." 7 60
				continue
			fi
			clear
			LNSELECTED=1
			while [ $LNSELECTED -le $MAXLNSELECTED ]; do
				LINE=`head -$LNSELECTED "$TEMPFILE" | tail -1`
				LINE=`echo "$LINE" | sed s/$'\r'//`
				if [ "$LINE" = "" ]; then
					echo "Cannot burn $IMGDIR"
					LNSELECTED=$LNSELECTED+1
					continue
				fi
				CDNAME=$LINE
				WAVNAME=$LINE
				# if toc file exists burn the image
				if [ -r "$IMGDIR/$CDNAME.toc" ]; then
					cd "$IMGDIR/$CDNAME"
					dialog --backtitle "$BACKTITLE" --title "Burn Image to Audio-CD" \
						--msgbox "\n  Insert CD-R and press OK to burn disc $LNSELECTED of $MAXLNSELECTED" 7 40
					clear
					cdrdao write "--device $DEVICE" $DRVWDAO --speed $WRITESPEED "$CDNAME".toc
					echo ""
					echo "Press Enter"
					read JUNK
					LNSELECTED=$LNSELECTED+1
					continue
				# if track.01.wav exists burn the WAVs
				elif [ -r "$IMGDIR/$WAVNAME/$WAVNAME".01.wav ]; then
					echo -e "TRACKLIST:\n" > $TEXTFILE
					find "$IMGDIR/$WAVNAME/$WAVNAME".*.wav -printf "%f\n" >> $TEXTFILE
					echo -e "\nInsert CD-R and press OK to burn disc $LNSELECTED of $MAXLNSELECTED" >> $TEXTFILE
					dialog --backtitle "$BACKTITLE" --title "Burn WAVs to Audio-CD" --exit-label "OK" \
						--textbox $TEXTFILE 16 60
					clear
					cdrecord "dev=$DEVICE" "speed=$WRITESPEED" $DRIVEROPTS $OTHERREC "$IMGDIR/$WAVNAME/$WAVNAME".*.wav
					echo ""
					echo "Press Enter"
					read JUNK
					LNSELECTED=$LNSELECTED+1
					continue
				fi
				# if no toc and no track.01.wav exists -> error
				dialog --backtitle "$BACKTITLE" --title "$ERROR" \
					--msgbox "'$CDNAME.toc' and '$WAVNAME.01.wav' could not be found -> Incomplete image)" 8 60
				LNSELECTED=$LNSELECTED+1
			done
			LEV1="1"
		done
		;;

		6)
		while [ $LEV1 = "0" ]; do
			FILES=`find *.m3u -maxdepth 0 -printf %f"\255"playlist"\255"off"\255"`
			if [ "$FILES" = "" ]; then
				dialog --backtitle "$BACKTITLE" --title "Check filess in M3U playlist" \
					--msgbox "There are no M3U playlists in current directory." 7 60
				LEV1="1"
				continue
			fi
			IFS="$(echo -e "\255")"
			dialog --backtitle "$BACKTITLE" --title "Check filess in M3U playlist" --separate-output \
				--checklist "Select playlist(s) with Spacebar"  16 60 8 $FILES 2> $TEMPFILE
			if [ $? = "1" ]; then
				LEV1="1"
				continue
			fi
			IFS="$IFSBAK"
			MAXLNSELECTED=`awk 'END {print FNR}' "$TEMPFILE"`
			if [ "$MAXLNSELECTED" = "0" ]; then
				dialog --backtitle "$BACKTITLE" --title "$ERROR" \
					--msgbox "Nothing to check. Please select something with Spacebar first." 7 60
				continue
			fi
			LNSELECTED=1
			while [ $LNSELECTED -le $MAXLNSELECTED ]; do
				LINE=`head -$LNSELECTED "$TEMPFILE" | tail -1`
				LINE=`echo "$LINE" | sed s/$'\r'//`
				PLAYLIST=$LINE
				if [ ! -r "$PLAYLIST" ]; then
					dialog --backtitle "$BACKTITLE" --title "$ERROR" \
					    --msgbox "'$PLAYLIST' does not exist or is not readable" 6 60
					LNSELECTED=$LNSELECTED+1
					continue
				fi
				clear
				echo "Checking playlist $LNSELECTED of $MAXLNSELECTED: '$PLAYLIST'"
				echo "Press Enter"
				read JUNK
				filecount
				checkm3u
				echo "Press Enter"
				read JUNK
				LNSELECTED=$LNSELECTED+1
			done
			LEV1="1"
		done
		;;

		7)
		FILES=""
		while [ $LEV1 = "0" ]; do
			FILES=`find "$IMGDIR" -mindepth 1 -maxdepth 1 -printf %f"\255"image"\255"off"\255"`
			if [ "$FILES" = "" ]; then
				dialog --backtitle "$BACKTITLE" --title "Delete WAV/Image files" \
					--msgbox "There are no files in your image directory." 7 60
				LEV1="1"
				continue
			fi
			IFS="$(echo -e "\255")"
			dialog --backtitle "$BACKTITLE" --title "Delete WAV/Image files" --separate-output \
				--checklist "Select file(s) with Spacebar"  16 60 8 $FILES 2> $TEMPFILE
			if [ $? = "1" ]; then
				LEV1="1"
				continue
			fi
			IFS="$IFSBAK"
			MAXLNSELECTED=`awk 'END {print FNR}' "$TEMPFILE"`
			if [ "$MAXLNSELECTED" = "0" ]; then
				dialog --backtitle "$BACKTITLE" --title "$ERROR" \
					--msgbox "Nothing to delete. Please select something with Spacebar first." 7 60
				continue
			fi
			clear
			LNSELECTED=1
			while [ $LNSELECTED -le $MAXLNSELECTED ]; do
				LINE=`head -$LNSELECTED "$TEMPFILE" | tail -1`
				LINE=`echo "$LINE" | sed s/$'\r'//`
				if [ "$LINE" = "" ]; then
					echo "Cannot delete $IMGDIR"
					LNSELECTED=$LNSELECTED+1
					continue
				fi
				echo "Image file: '$LINE'"
				echo "Removing file..."
				echo "Done"
				echo ""
				rm -r "$IMGDIR/$LINE"
				LNSELECTED=$LNSELECTED+1
			done
			echo ""
			echo "Press Enter"
			read JUNK
			LEV1="1"
		done
		;;

		8)
		MAINLEV="1"
		;;
	esac
done
test -e $TEMPFILE && rm -f $TEMPFILE
test -e $TEXTFILE && rm -f $TEXTFILE
clear
echo "$BACKTITLE (C) by Thorsten Muehlfelder"
