#!/bin/bash

#========================================================
# autocdbackup v0.5.0 - 11 Dec 2002 - Mitch Sharp
# CDR/W Backup Routing using Cygwin & CDRTools for Win32
# Also works under *NIX
#========================================================

#========================================================
# To customize, edit /etc/autocdbackup.conf.
# It is documented appropriately.
#========================================================

[ -f /etc/autocdbackup.conf ] || { echo "The /etc/autocdbackup.conf config file is missing."; echo; exit 1; }
. /etc/autocdbackup.conf
if [ $? == 0 ]; then
	echo "Configuration successfully imported"
	echo
fi

#========================================================
# Introduce ourselves
#========================================================
splash() {
	echo "autocdbackup v0.6.0 - 7 Apr 2003 - Mitch Sharp"
	echo "CDR/W Backup Routine using Cygwin & CDRTools for Win32"
	echo
	}

#========================================================
# Show our environment
#========================================================
showenv() {
	echo Here are the environment variables:
	for variable in CDTYPE CDRW_DEV ISO_DIR ISO_FILE ISOSRC_DIR RETVAL; do
		echo "    $variable"
	done
	echo

	echo Here are our archive definitions:
	for (( i=0; i<${#archives[@]}; i++)); do
		echo "   ${archives[$i]}->${archive_src[$i]}"
	done
	echo
	}

#========================================================
# Verify directory existance
#========================================================
verify_env_directories() {
	echo "Checking for necessary directories..."
	for directory in $ISO_DIR $ISOSTATIC_DIR $ISOSRC_DIR; do
	    echo "    Looking for $directory..."
	    [ -d $directory ] || { echo "   $directory not found!  Exiting..."; exit 1; }
	done
	echo "...all necessary directories found!" 
	echo
	}

#========================================================
# Check for existance of needed executables
#========================================================
verify_binaries() {
	echo "Checking for necessary binaries..."
	for binary in cdrecord mkisofs zip tar gzip bzip2; do
	    echo "    Looking for /bin/$binary..."
	    ( [ -f /bin/$binary ] || [ -f /usr/bin/$binary ] ) || { echo ".../bin/$binary not found... exiting..."; exit 1; }
	done
	echo "...all necessary binaries found!"
	echo
	}

#========================================================
# Check to see if the archive sources actually exist
#========================================================
verify_archive_src() {
	echo "Checking to make sure archive sources exist..."

	# Check to see if the subscripts of both arrays match
	if [ ${#archives[@]} != ${#archive_src[@]} ]; then
		echo There is an error with the archive declaration!
		echo "   \$archives length is    : ${#archives[@]}"
		echo "   \$archive_src length is : ${#archive_src[@]}"
		echo
		echo You are missing either an archive name or an archive source definition.
		echo Exiting...
		echo
		exit 1
	fi

	# Now check to see if the archive source directories/files exist
	for (( i=0; i<${#archive_src[@]}; i++)); do
		if ( [ -f "${archive_src[$i]}" ] || [ -d "${archive_src[$i]}" ] ); then 
			echo "   ${archives[$i]}->${archive_src[$i]} is good"
		else
			echo "   ${archive_src[$i]} does not exist!!! exiting..."
			exit 1
		fi
	done
	
	echo "...done!"
	echo
	}

verify_all() {
	verify_env_directories
	verify_binaries
	verify_archive_src
	}

#========================================================
# Automatic setup functions
#========================================================
make_directories() {
	echo Creating directories...
	for directory in $ISO_DIR $ISOSTATIC_DIR $ISOSRC_DIR; do
	    echo "    Looking for $directory..."
	    [ -d $directory ] || { echo "   $directory not found!  Creating..."; mkdir $directory; }
	    RETVAL=$?
	    if [ $RETVAL==0 ]; then
		echo
		echo ...done creating $directory!
		echo
	    else
		echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
		echo "!!! An error occured during the creation of the ISO file. !!!"
		echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
		exit 1
	    fi
	done
	echo "...all necessary directories created or found!" 
	echo
	}	

#========================================================
# Define script functions
#========================================================
cleanup_directories() {
	echo Cleaning up directories...
	rm -rf $ISO_DIR/*
	rm -rf $ISOSRC_DIR/*
	echo ...done!
	echo
	}

#========================================================
# Create contents of $ISOSRC_DIR
#========================================================
create_isosrc_contents() {
	echo Creating contents of $ISOSRC_DIR...
	echo ...copying static contents...
	touch $ISOSTATIC_DIR/stamp
	cp -R $ISOSTATIC_DIR/* $ISOSRC_DIR/
	if [ $? != 0 ]; then
		echo
		echo Error copying $ISOSTATIC_DIR contents to $ISOSRC_DIR!
		echo Exiting...
		echo
		exit 1
	fi
	echo ...copying backup files...
	echo ...creating compressed archives...

	# loop through creating $archives
	for (( i=0; i<${#archives[@]}; i++)); do
		if [ $CYGWIN == 1 ]; then
			# We're using Windows/Cygwin
			# We have to use tar first and then have gzip compress the archive
			echo
			echo "Creating ${archives[$i]}..."
			echo
			tar --directory "${archive_src[$i]}" -cf "$ISOSRC_DIR/${archives[$i]}" .
			if [ $? != 0 ]; then { echo; echo "Error creating ${archives[$i]}... exiting..."; echo; exit 1; } fi

			if [ $TARGZ == 1 ]; then
				echo
				echo "Compressing ${archives[$i]}..."
				echo
				gzip "$ISOSRC_DIR/${archives[$i]}"
				if [ $? != 0 ]; then { echo; echo "Error compressing ${archives[$i]}... exiting..."; echo; exit 1; } fi
			fi
		else
			# Using *NIX
			# tar can do all the work thanks to superior piping
			echo
			echo "Creating ${archives[$i]}.gz..."
			echo
			tar --directory "${archive_src[$i]}" -zcf "$ISOSRC_DIR/${archives[$i]}.gz" .
			if [ $? != 0 ]; then { echo; echo "Error creating ${archives[$i]}.gz... exiting..."; echo; exit 1; } fi
		fi
	done

	echo ...done!
	return $RETVAL
	}

#========================================================
# Create ISO file $ISO_FILE in directory $ISO_DIR
#========================================================
create_iso() {
	echo Creating $ISO_DIR/$ISO_FILE...
	mkisofs -R -J -o $ISO_DIR/$ISO_FILE --graft-points /=$ISOSRC_DIR/
	RETVAL=$?
	if [ $RETVAL==0 ]; then
		echo
		echo ...done creating iso!
		echo
	else
		echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
		echo "!!! An error occured during the creation of the ISO file. !!!"
		echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
		exit 1
	fi
	return 0
	}

cdrw_blank_fast() {
	echo "Blanking CDRW (fast mode)..."
	echo
	cdrecord dev=$CD_DEV speed=$CD_SPEED blank=fast
	RETVAL=$?
	if [ $RETVAL==0 ]; then
		echo
		echo ...done blanking!
		echo 
	else
		echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
		echo "!!! An error occured blanking the CDRW... !!!"
		echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
		exit 1
	fi
	return 0
	}

cdrw_blank_all() {
	echo "Blanking CDRW (all/disc mode)..."
	echo
	cdrecord dev=$CD_DEV speed=$CD_SPEED blank=all
	RETVAL=$?
	if [ $RETVAL==0 ]; then
		echo ...done!
	else
		echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
		echo "!!! An error occured blanking the CDRW... !!!"
		echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
		exit 1
	fi
	return 0
	}

cdrw_write_iso() {
	echo "Writing $ISO_DIR/$ISO_FILE to CDR/W..."
	echo
	cdrecord dev=$CD_DEV speed=$CD_SPEED -v $ISO_DIR/$ISO_FILE
	RETVAL=$?
	if [ $RETVAL==0 ]; then
		echo ...done!
	else
		echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
		echo "!!! An error occured writing to the CDR/W !!!"
		echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
		exit 1
	fi
	}

cdrw_eject() {
	echo "Ejecting the CD..."
	echo
	cdrecord dev=$CD_DEV -eject
	RETVAL=$?
	if [ $RETVAL==0 ]; then
		echo ...done!
		echo
	else
		echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
		echo "!!! An error occured ejecting the CD... !!!"
		echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
		exit 1
	fi
	return 0
	}

notify_successful_backup() {
	echo
	echo "The backup completed successfully!!!"
	echo
}

#========================================================
# Done declaring functions
#========================================================

case "$1" in
	blank)
		cdrw_blank_fast
		RETVAL=$?
		;;
	blank_all)
		cdrw_blank_all
		RETVAL=$?
		;;
	go)
		verify_all

	        cleanup_directories
		create_isosrc_contents
		create_iso

		# check to see if we have a CDRW
		# if so, blank the CD
		if [ $CDTYPE == 1 ]; then
			cdrw_blank_fast
		fi

		cdrw_write_iso
		cdrw_eject
		notify_successful_backup
		RETVAL=$?
		;;
	check)
		echo "Verifying binaries & directories"
		echo
		verify_all
		RETVAL=$?
		;;
	clean)
		echo "Cleaning $ISO_DIR & $ISOSRC_DIR..."
		echo
		verify_env_directories
	        cleanup_directories
		RETVAL=$?
		;;
	create_iso)
		echo Creating $ISO_DIR/$ISO_FILE...
		echo
		verify_all
		create_iso
		RETVAL=$?
		;;
	create_isosrc)
		echo Creating $ISOSRC_DIR contents...
		echo
		verify_all
		create_isosrc_contents
		RETVAL=$?
		;;
	make_directories)
		make_directories
		verify_env_directories
		RETVAL=$?
		;;
	showenv)
		echo Showing the environment setup:
		echo
		showenv
		RETVAL=$?
		;;
	write_iso)
		echo Writing $ISO_DIR/$ISO_FILE to CDR/W...
		echo
		verify_all
		[ -f $ISO_DIR/$ISO_FILE ] || { echo "The $ISO_DIR/$ISO_FILE does not exist!  Exiting..."; echo; exit 1; }

		# check to see if we have a CDRW
		# if so, blank the CD
		if [ $CDTYPE == 1 ]; then
			cdrw_blank_fast
		fi

		cdrw_write_iso
		notify_successful_backup
		;;
	*)
	        echo $"Usage: $0 {go|blank|blank_all|clean|check|create_iso|create_isosrc|make_directories|showenv|write_iso}"
		echo
		echo  "       If you have configured and tested /etc/autocdbackup.conf,"
		echo  "       use the following command line to run your backup:"
		echo  "           $0 go"
		echo
		exit 1
esac

exit $RETVAL
