#! /bin/sh
#
# Script to generate sequentially numbered audio_*.wav files from given
# track numbers.
#
PREFIX="audio_"
# Example: When the files audio_1.wav audio_2.wav, and audio_4.wav
#          are present in the current directory, and this script is
#	   called with arguments '1 9 7',
#		track 1 is saved into audio_0.wav
#		track 9 is saved into audio_3.wav
#		track 7 is saved into audio_5.wav
#	   so gaps are filled, but no files are overwritten
COUNT=0
#          
#	   If you would like the following behaviour better,
#		track 1 is saved into audio_5.wav
#		track 9 is saved into audio_6.wav
#		track 7 is saved into audio_7.wav
#	   (which continues from the highest used number)
#	   uncomment the next line
#COUNT=`ls $PREFIX*.wav | sort -n +0.6 | tail -n1`
#
CDDA2WAV=cdda2wav
CDDA2WAVOPTS="-P0"

for i do
 while [ -e $PREFIX$COUNT.wav ]; do COUNT=$(($COUNT+1)); done
 $CDDA2WAV $CDDA2WAVOPTS -Owav -t $i $PREFIX$COUNT".wav"

 RES=$?
 if [ $RES -ne 0 ]; then
   echo "cdda2wav error, return value "$RES". Aborted." >&2
   break
 fi

 #touch $PREFIX$COUNT.wav
done
