#!/bin/sh
#  Note:	The above line determines what interpreter to use to interpret this script.
######################################################
#
#  Program Name:		unpackfloppy
#
#  Program Description:	This script unpacks RADAR software from the install medium.
#
#  Return value:
#	<floppy disk #>		- The next disk number in the installation disk set
#					  (returns the same disk number if it is the last in the set)
#	EXIT_CODE_FAILED	-  Couldn't unpack the medium to the local drive.
#	EXIT_CODE_WRONG_DISK	-  Not a valid install disk.
#	
#
#  NOTE:	THIS SCRIPT ASSUMES THAT A VALID INSTALL FLOPPY HAS ALREADY BEEN DETECTED.
#
#  Rights to this program:
#
#  This script file is the property of iZ technology corporation (www.iZcorp.com).
#  Unauthorized distribution and/or use of this script is strictly prohibited and subject
#  to applicable laws.
#
######################################################

#
#  Common constants, variables and functions
#

source ${0%/*}/installfunctions

#
#  Declare local constants and variables.
#
#  NOTE: 	To handle file names with spaces in them, the name and variable need to be
#		"surrounded by double quotes" like in this sentence.

MYNAME=${0##*/}
INSTALL_LOG="${INSTALL_LOGS_DIR}/${MYNAME}.log"
LOG_RESULTS="/bin/tee -a ${INSTALL_LOG}"


##########################################################
#  Function definition section.
##########################################################

function Initialize
{
	s_disknum=""			#  To contain the current diskette number
	s_disklast=""			#  To contain the number of the last diskette in the set
	i_disknum=0			#  To contain the current diskette number
	i_disklast=0			#  To contain the number of the last diskette in the set
	s_cksum_current=""		#  To contain the checksum of the current valid RADAR 24 installation disk
	s_cksum_prev=""		#  To contain the checksum of the previous valid RADAR 24 installation disk

	# Initialize install log
	/bin/rm -f $INSTALL_LOG
	/bin/touch $INSTALL_LOG
	LogMessage "${MYNAME}:\n"

	# Get the install media
	read MEDIA < $INSTALL_MEDIA
	LogMessage "Current install media is '$MEDIA'."
}

function CleanUp()
{
	# Unmount floppy/Cdrom disk if necessary
	if [ "$MEDIA" = "$LOCAL_INSTALLCURRENT" ]
	then
		/bin/cp -f ${LOCAL_INSTALL}/${ZIMAGE_PATTERN} $LOCAL_INSTALLPREVIOUS
		/bin/cp -f ${LOCAL_INSTALL}/${ZIMAGE_PATTERN_OLD} $LOCAL_INSTALLPREVIOUS
		/bin/rm -rf ${LOCAL_INSTALLCURRENT}/*
		/bin/rm -f ${LOCAL_INSTALL}/*
	elif [ "$MEDIA" = "$CDROM" ]
	then
		UnMountCdrom
	elif [ "$MEDIA" = "$FLOPPY" ]
	then
		UnMountFloppy
	fi

	#  Make a copy of the install log file and add a timestamp to the file name
	/bin/cp $INSTALL_LOG ${INSTALL_LOG}.`/bin/date '+%c'|tr -s ' ' '-'`

	#  Report where the details of this particular installation instance can be found
	PrintMessage "\nSee $INSTALL_LOG for the results of this script.\n"
}

function GetFloppyInfo()
{
#	return value = next required disk number or error code
#	NOTE:	assumes floppy is already mounted
#
	declare -i ret_disknum=$EXIT_CODE_WRONG_DISK

	if ValidateMedia
	then
		LogMessage "Checking to see which RADAR 24 sofware installation disk we just mounted ..."

		s_disknum=$(/bin/head -1 ${INSTALL_DIR}/$DISK_ID_FILE | awk '{print $1}')
		s_disklast=$(/bin/head -1 ${INSTALL_DIR}/$DISK_ID_FILE | awk '{print $3}')
		LogMessage "Found install disk #$s_disknum of $s_disklast."

		i_disknum=$((s_disknum))
		i_disklast=$((s_disklast))

		if [ $i_disknum -eq $i_disklast ]
		then
			ret_disknum=$i_disknum
		else
			ret_disknum=$i_disknum+1
		fi
	fi
	
	# Return the next required disk number or error code
	return $ret_disknum
}


######################################################
#
#	Start Main() part of script.
#
######################################################

Initialize

GetFloppyInfo
ret_val=$?

if [ "$ret_val" = "0" ]
then
	LogMessage "ERROR: The inserted diskette is not a valid RADAR 24 installation disk."
fi

LogMessage "Copying data from '$MEDIA' to local disk ... "
/bin/cp ${MEDIA}/$CHUNK_FILE_PREFIX* ${INSTALL_DIR}
ret_cp=$?
if [ "$ret_cp" != "0" ]
then
	LogMessage "ERROR: Copying files from '$MEDIA' to local drive failed with exit code $ret_cp."
	ret_val=$EXIT_CODE_FAILED
fi

LogMessage "\nReturn code = ${ret_val}"
CleanUp
exit $ret_val

#
#  End of Script
#