#!/bin/sh
echo "`basename $0`, Paul T.Threshold 2006-2012"
FROMTRACK=$1
TOTRACK=$2
QUALITY=$3
PTT_CDDEV=/dev/cdrom
if test "$FROMTRACK" = ""; then
        FROMTRACK="--help"
fi
if test "$TOTRACK" = ""; then
        TOTRACK=$FROMTRACK
fi
if test "$TOTRACK" = "nomax"; then
        QUALITY='nomax'
fi
case "$FROMTRACK" in
	--help | -h)
	        echo "Usage: "
		echo "  `basename $0` <1st track to be read> <last track> ['nomax'|'<additional opts>']"
	        echo "  . when <1st track to be read> is '-i' retrieve only cddb infos"
	        echo "  . when <1st track to be read> is '-a' will read all tracks"
	        echo "  . when <1st track to be read> is '-A' will read all tracks and re-wav with sox"
	        echo "  . nomax stands for no maximum quality (omits -x for cdda2wav)"
	        echo "  . <additional opts> could be more options for cdda2wav, for example:"
		echo "    '-C big -e'"
		echo "    (-x will be added by default when using additional opts)"
		exit 0
	;;
	-i)
		cdda2wav -J -L 0 cddbp-server=freedb.freedb.org -D $PTT_CDDEV pttinfo
		rm -f pttinfo_*.inf
		eject $PTT_CDDEV
	;;
	*)
		if [ "$QUALITY" = "nomax" ]; then
		        QUALITY=""
		else
			QUALITY="-x -C little $QUALITY"
		fi
		if [ "$FROMTRACK" != "-a" -a "$FROMTRACK" != "-A" ]; then
			echo "Reading audio cd data tracks from #$FROMTRACK to #$TOTRACK...."
			TRACKS="-t $FROMTRACK+$TOTRACK"
		else
			echo "Reading all audio cd data tracks...."
			TRACKS=""
		fi
		cdda2wav $TRACKS $QUALITY -B -L 0 cddbp-server=freedb.freedb.org -D $PTT_CDDEV -O cdr track ;

		if [ "$FROMTRACK" = "-A" ]; then
			for i in *.cdr ; do
				sox $i $i.wav;
				mv -f $i.wav $i
			done;
		fi
	;;
esac


