#!/bin/bash
# make_iso_image.sh
### start script header
SCRIPT_AUTHOR="Christopher Steel"
PREVIOUS_CONTRIBUTORS="The Answer Gang"
SCRIPT_DATE="2008-02-26"
PREVIOUS_SCRIPT_DATE="Unknown"
SCRIPT_REFERENCE_001="http://linuxgazette.net/issue74/tag/7.html"
SCRIPT_COPYRIGHT="${SCRIPT_AUTHOR}, ${SCRIPT_DATE}"

# TODO
# if image allready exists prompt user
# Prompt for discription at beginning of script
# test for CD/DVD device?
# Implemented changes and modifications
# English and French Prompts?
SCRIPT_CHANGES_004="Added CD/DVD drive variable 2008-06-04"
SCRIPT_CHANGES_003="Added description file text editor command 2008-06-04"
SCRIPT_CHANGES_002="2008-04-28 Added temporary label label for label related issues 2008-06-04"
SCRIPT_CHANGES_001="2008-04-28 Added quotes to dd command to prevent errors with lables and spaces 2008-06-04"



if [[ $# != 1 ]]; then
   echo ""
   echo "Error            : missing parameter for your cdrom or dvd device"
   echo "Usage            : make_iso_image.sh <device file>"
   echo "Usage example    : ${0} /dev/dvd or ${0} /dev/scd0"
   echo ""
   exit 1
fi
### end script header

ISO_DIRECTORY="${HOME}/sys/iso"

###
# Uncomment your systems DVD / or CDROM drive below
# You can check your system with mount | grep cdrom OR mount | grep dvd OR just mount)
###
#CD_DVD_DEVICE="/dev/cdrom"
#CD_DVD_DEVICE="/dev/dvd"
#CD_DVD_DEVICE="/dev/scd0"
CD_DVD_DEVICE="/dev/scd1"

# If we find an old iso with the same name do we want to replace it?
OVER_WRITE_OLD_ISO="0"
# Do we want a description file for this iso image?
CREATE_ISO_DESCRIPTION="1"
# Read the label
ISO_LABEL="$(dd bs=1 skip=32808 count=32 if=$1 2>/dev/null)"
if [[ $? != 0 ]]; then
   echo Couldn\'t get CD label!
   exit 1
fi
# Trim trailing blanks
ISO_LABEL=$(echo $ISO_LABEL  |  sed 's/ *$//')
# add if does not exist here
echo "Preparing directory ${ISO_DIRECTORY}/${ISO_LABEL}"
mkdir -p "${ISO_DIRECTORY}/${ISO_LABEL}"
# unmount our cd/dvd
umount ${CD_DVD_DEVICE}
DESTINATION_URL="${ISO_DIRECTORY}/${ISO_LABEL}/${ISO_LABEL}.iso"
# add if does not exist here

#if [ -d ${DESTINATION_URL} ]
#	then
#	echo "${DESTINATION_URL} exists, you you want to over write this files?"
#	OVER_WRITE_OLD_ISO="1"	
#fi
echo "Saving file to ${DESTINATION_URL}"
echo "running ur dd command"

dd if=$1 of="${DESTINATION_URL}"
eject $1
# add if user inputs Y for description file
# Created a discription file for this image
touch ${ISO_LABEL}/"${ISO_LABEL}"/description.txt
echo "DESCRIPTION=\"${ISO_LABEL}\"" >> ${ISO_DIRECTORY}/"${ISO_LABEL}"/description.txt
echo "DESCRIPTION=\"\"" >> ${ISO_DIRECTORY}/"${ISO_LABEL}"/description.txt
echo "SN=\"\"" >> ${ISO_DIRECTORY}/"${ISO_LABEL}"/description.txt
echo "OWNER=\"\"" >> ${ISO_DIRECTORY}/"${ISO_LABEL}"/description.txt
echo "COMMENTS=\"\"" >> ${ISO_DIRECTORY}/"${ISO_LABEL}"/description.txt
gedit ${ISO_DIRECTORY}/"${ISO_LABEL}"/description.txt


