#!/bin/sh
#  Note:	The above line determines what interpreter to use to interpret this script.
######################################################
#
#  Program Name:		validatefloppy
#
#  Program Description:	This script checks for a valid RADAR install medium.
#
#  Arguments:
#  <disk id>			- the disk number to check for
#  <s/w version>		- the software version to check for
#
#  Return value:
#	0				- the requested disk/medium is present
#	<error code>		- one of the exit codes defined below (100-127)
#	
#  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}"

DISK_REQUESTED="$1"		# Argument 1
VER_REQUESTED="$2"		# Argument 2

VERSION_APP="GetAppVersion"


##########################################################
#  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" = "$CDROM" ]
	then
		UnMountCdrom
	elif [ "$MEDIA" = "$FLOPPY" ]
	then
		UnMountFloppy
	fi

	# Force removal of any files in that we don't need from current directory
	/bin/rm -f ${INSTALL_DIR}/$DISK_ID_FILE
	
	#  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 IsRequestedFloppy
{
#	return value = 0 if requested floppy, else error code
#
	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}')
		PrintMessage "Disk in drive = $s_disknum; disk requested = $DISK_REQUESTED" 

		if [ "$s_disknum" != "$DISK_REQUESTED" ]
		then
			return $EXIT_CODE_WRONG_DISK
		fi

		LogMessage "Found install disk #$s_disknum."

		s_disklast=$(/bin/head -1 ${INSTALL_DIR}/$DISK_ID_FILE | awk '{print $3}')
		i_disknum=$((s_disknum))
		i_disklast=$((s_disklast))

		BUILD_FILE_NAME=$(/bin/grep "Build file:" ${INSTALL_DIR}/$DISK_ID_FILE | /bin/awk '{print $3}')
		local temp=${BUILD_FILE_NAME##*-}
		temp=${temp%%.zip}
		temp=${temp#*.}
		RADAR24_VER=${temp%.*}

		PrintMessage "Ver on disk = $RADAR24_VER; ver requested = $VER_REQUESTED" 

		if [ "$RADAR24_VER" != "$VER_REQUESTED" ]
		then
			return $EXIT_CODE_WRONG_VERSION
		fi

		# If this is the first diskette in the set then
		# copy version checker program to the local drive
		if [ $i_disknum -eq 1 ]
		then
			/bin/cp ${MEDIA}/${VERSION_APP}.zip ${INSTALL_DIR} > ${INSTALL_LOGS_DIR}/copy.log 2>&1
			local ret_cp=$?
			if [ "$ret_cp" != "0" ]
			then
				LogMessage "ERROR: Copying '$VERSION_APP' from '$MEDIA' to local drive failed with exit code $ret_cp."
				return $EXIT_CODE_UNABLE_TO_COPY
			fi

			/bin/unzip -o ${INSTALL_DIR}/${VERSION_APP}.zip -d ${INSTALL_DIR} > ${INSTALL_LOGS_DIR}/unzip.log 2>&1
			local ret_unzip=$?
			if [ "$ret_unzip" != "0" ]
			then
				LogMessage "ERROR: Unzipping '$VERSION_APP' failed with exit code $ret_unzip."
				return $EXIT_CODE_UNABLE_TO_UNZIP
			fi
		fi
	else
		return $EXIT_CODE_UPGRADE_NO
	fi

	# Everything is good
	return $EXIT_CODE_OK
}


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

Initialize

if [ "${#}" != "2" ]
then
	LogMessage "\nERROR: Wrong number of arguments given. This program takes 2 arguments."
	LogMessage "Usage: ${MYNAME} <disk number> <minor s/w version>\n"
	CleanUp
	exit $EXIT_CODE_WRONG_NUMBER_OF_ARGUMENTS
fi

IsRequestedFloppy
ret=$?

if [ "$ret" = "$EXIT_CODE_OK" ]
then
	LogMessage "Request to install RADAR 24 software version ${MAJOR_VER}.${VER_REQUESTED}"
else
	# Reset the net install directories to prevent a cyclic error
	if [ "$MEDIA" = "$LOCAL_INSTALLCURRENT" ]
	then
		LogMessage "Net Install directories have been reset."
		/bin/rm -rf ${LOCAL_INSTALLCURRENT}/*
		/bin/rm -f ${LOCAL_INSTALL}/*
	fi
fi

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

#
#  End of Script
#