#!/bin/sh
#  Note:	The above line determines what interpreter to use to interpret this script.
######################################################
#
#  Program Name:		checkforupgrade
#
#  Program Description:	This script checks for valid RADAR install media.
#
#  Return value:
#	<version #>		- version of software that the medium contains (0-99)
#	<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}"

INSTALL_SCRIPT_UPDATER="installscriptupdater"
FIRST_MOUNT=1

declare -i ret_val


##########################################################
#  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 installation disk
	s_cksum_prev=""		#  To contain the checksum of the previous valid installation disk

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

	# Force removal of any old chunks in current directory
	/bin/rm -f ${INSTALL_DIR}/$CHUNK_FILE_PREFIX*
	/bin/rm -f ${INSTALL_DIR}/$DISK_ID_FILE

	# Assume no valid install media
	/bin/echo "none" > $INSTALL_MEDIA
}

function CleanUp()
{
	# Unmount floppy disk if necessary
	#UnMountFloppy

	# Force removal of any old chunks in current directory and any ID files
	/bin/rm -f ${INSTALL_DIR}/$CHUNK_FILE_PREFIX*
	/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 GetInstallVersion
{
# return value = version of software that the floppy contains or an error
#
	#  Verify first installation diskette in a verification loop.
	#  There are two possible things that can result from this function:
	#	1.  Floppy/Cdrom in drive is not a valid Radar24 install disk.
	#	2.  Floppy/Cdrom in drive is a valid Radar24 install disk.
	#
	local install_ver=$EXIT_CODE_UPGRADE_NO

	if ValidateMedia
	then
		# Remember the install media
		/bin/echo $MEDIA > $INSTALL_MEDIA
		LogMessage "Current install media is '$MEDIA'."

		LogMessage "Checking to see which RADAR 24 sofware installation disk we just mounted ..."
		/bin/ls ${INSTALL_DIR}/$DISK_ID_FILE | $LOG_RESULTS
		/bin/cat ${INSTALL_DIR}/$DISK_ID_FILE | $LOG_RESULTS
		s_disknum=$(/bin/head -1 ${INSTALL_DIR}/$DISK_ID_FILE | awk '{print $1}')
		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%.*}
		if [ "$RADAR24_VER" = "" ]
		then
			RADAR24_VER=0
		fi

		/bin/rm -f ${INSTALL_DIR}/$DISK_ID_FILE

		if [ "$s_disknum" != "1" ]
		then
			install_ver=$EXIT_CODE_WRONG_DISK
		else
			install_ver=$RADAR24_VER

			if [ -f "${MEDIA}/$INSTALL_SCRIPT_UPDATER" ]
			then
				${MEDIA}/$INSTALL_SCRIPT_UPDATER | $LOG_RESULTS
			else
				LogMessage "'${MEDIA}/$INSTALL_SCRIPT_UPDATER' not found--no scripts will be updated."
			fi

			# Remove any old install files
			/bin/rm -f ${INSTALL_DIR}/Radar24-*.zip | $LOG_RESULTS
			local ret_rm=$?
			if [ "$ret_rm" != "0" ]
			then
				LogMessage "ERROR: Removing '${INSTALL_DIR}/Radar24-*.zip' failed with exit code $ret_rm."
			else
				LogMessage "Old install files '${INSTALL_DIR}/Radar24-*.zip' removed."
			fi
		fi
	fi

	# Return the version or error code
	return $install_ver
}


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

Initialize

GetInstallVersion
ret_val=$?

if [[ ($ret_val -ge $MIN_MINOR_VER) && ($ret_val  -le $MAX_MINOR_VER) ]]
then
	LogMessage "Request to install RADAR software version ${MAJOR_VER}.${ret_val}"
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_val}"
CleanUp
exit $ret_val

#
#  End of Script
#