#!/bin/sh

CDROM=/dev/cdrom

if [ "$#" -eq "2" -o "$#" -eq "3" ]
then
	FILENAME=$1
	SPAN=$2

	if [ "$#" -eq "3" ]
	then
		CDROM=$3
	fi

	CDID=`discid $CDROM`
	if cdparanoia $SPAN $FILENAME.bin
	then
		echo "Edit the CUE file to remove the data track"
		sleep 10
		$EDITOR $FILENAME.cue
		if nice flac -8 --replay-gain --endian=big --sign=signed --channels=2 --bps=16 --sample-rate=44100 --cuesheet=$FILENAME.cue $FILENAME.bin
		then
			rm -f $FILENAME.bin
			rm -f ${FILENAME}-fixed.toc
			rm -f $FILENAME.toc
			rm -f $FILENAME.cue*

			FLACID=`flactag --discid $FILENAME.flac | cut -d':' -f 2| cut -d' ' -f 2`

			if [ "$FLACID" != "$CDID" ]
			then
				echo "**********"
				echo "**********"
				echo "**********"
				echo "********** Disc ID of FLAC doesn't match Disc ID of CD **********"
				echo "**********"
				echo "**********"
				echo "**********"
			fi
		fi
	fi
else
	echo "Usage: $0 cdname tracks [ /path/to/cdrom ]"
fi
