#!/bin/sh

## Output of cdrecord --scanbus says that 0,0,0 works for me.

flist=`ls -rt *.wav`

cdrecord dev=0,0,0 blank=fast speed=4

# The following loop is to ensure that we trim flist until it's short 
# enough to fit on the CD.  cdrecord will exit with an error condition
# if we send a set of wav files that are too long.  In that case, we remove
# the most recently created wave file from the list and try again.

until cdrecord dev=ATAPI:0,0,0 speed=4 -pad -audio $flist 
do

  drop=`echo $flist | rev | cut -d " " -f 1 | rev`

  # NOTE: -s below is essential!  Without it, if $flist has only one word,
  # then the next line leaves it unchanged.  Therefore, we may have an 
  # infinite loop.
  flist=`echo $flist | rev | cut -d " " -s -f 2- | rev`
  echo Dropping $drop

  if [ -z "$flist" ]
  then 
    exit
  fi
done

echo Wrote $flist...?
eject /dev/scd0