#!/bin/bash

# Script to copy a directory into a burn directory and write it to a fresh cd.
# It will pause after creating the image and waits for the user to press
# Enter to continue with writing the new cd.  This gives the user the
# opportunity to insert a blank cd for writing to.


# Determine how many copies to make

logfile=`echo ~/cdtools_log`;
touch $logfile;
copies=1;
source_dir=$1;
echo "How many copies of this cd do you want?";
read copies;
echo `date` ":" $copies "copies requested" >> $logfile;

# Prep copying process. Make a temp directory and rip the cd.

mount /dev/scd0;
if
	mkdir /backupa/cdrip;
	./makeiso $source_dir /backupa/cdrip/cd.iso;
then
	echo `date` ":" $source_dir "rip completed" >> $logfile;
else
	echo `date` ":" $source_dir "rip failed.  Exiting." >> $logfile;
	exit 0;
fi

# Issue the command to copy the dirctory 'n' times

for ((i=0;i<$copies;i++));
do
	if
		umount /mnt/cdrom;
		eject /dev/scd0;
		echo `date` ":" "Please insert a blank cdr.  Press <Enter> to continue...";
		read;
		./burndatacd /backupa/cdrip/cd.iso;
		r=$(($i+1));
	then
		echo `date` ":" "Burn completed for copy $r" >> $logfile;
	else
		echo `date` ":" "Burn failed on iteration" $r >> $logfile;
		exit 0;
	fi
done

# Clean up

if
	rm -Rf /backupa/cdrip;
then
	echo `date` ":" $source_dir "rip directories removed." >> $logfile;
else
	echo `date` ":" $source_dir "rip directory unable to be removed. Exiting." >> $logfile;
	exit 0;
fi
