#!/bin/sh

##############################################################################
# dc_cd_wizard v1.0 by duncan brown (duncan@linuxadvocate.net)
#
# http://www.linuxadvocate.net/dc
#
# this is based upon various information i've found across the web on various
# sites, only cleaned up and made user friendly 
##############################################################################

echo
echo " $0 v1.0 by duncan brown (duncan@linuxadvocate.net)"
echo

if [ ! -f "IP.BIN" ] && [ ! -f "ip.bin" ]; then
 echo " we need a valid IP.BIN before we can proceed"
 echo
 exit
fi

# if the user didn't give us enough input, we prompt for the cd device and speed

if [ -z "$3" ]; then
 echo " entering interactive mode, you can specify all required information on the"
 echo " command line if you wish"
 echo
 echo " syntax  : $0 [cd-r scsi id] [rec speed] [cd filesystem]"
 echo " example : $0 0,0,0 4 ./files"
 echo
 cdrecord -scanbus
 if [ "$?" != "0" ]; then
  echo
  echo " something's wrong, please check that you have your cd recorder properly"
  echo " installed and configured and also have the cdrecord utility installed"
  echo
  echo " see http://www.linuxadvocate.net/apt for information on installing software"
  echo " on redhat, fedora or debian"
  echo
  exit
 fi
 echo "please enter the SCSI id of your cd burner from the above list"
 read -p "scsi id [0,0,0] : " cd_device
 if [ -z "$cd_device" ]; then
  cd_device=0,0,0
 fi
 while [ -z "$directory" ]; do
  echo "please let me know the directory that contains your desired dc cd setup"
  read -p "directory : " directory
  if [ ! -d "$directory" ]; then
   echo "directory does not exist"
   directory=''
  fi
 done
 read -p "at what speed should we write this disc [4] : " cd_speed
else
 cd_device=$1
 cd_speed=$2
 directory=$3
fi

if [ ! -d "$directory" ]; then
 echo "$directory does not exist"
 exit
fi

if [ -z "$cd_speed" ]; then
 cd_speed=4
fi

# create an audio track with 4 seconds of silence 
dd if=/dev/zero bs=2352 count=300 of=audio.raw

# burn our audio track
cdrecord -v dev=${cd_device} speed=${cd_speed} -multi -audio audio.raw

# find the msinfo information and save it
msinfo=`cdrecord dev=${cd_device} -msinfo`

# make iso with roms and emu
mkisofs -l -r -C ${msinfo} -o tmp.iso $directory

# combine information from precompiled IP.BIN and iso, skipping first 16 blocks
(cat IP.BIN ; dd if=tmp.iso bs=2048 skip=16) > data.raw

# put all of that onto the disc
cdrecord -v dev=${cd_device} speed=${cd_speed} -multi -xa data.raw

# clean up our mess
rm tmp.iso data.raw audio.raw
