#!/bin/sh
#  Note:	The above line determines what interpreter to use to interpret this script.
######################################################
#
#  Program Name:		backupcurrentver
#
#  Program Description:	This script used to back up the current RADAR software
#					version to a new directory--that function is now obsolete.
#					NOW, it copies any previous versions stored on the
#					install CD to the local hard drive.
#
#  Return value:
#	0					- Backup not necessary or backup sucessful.
#	EXIT_CODE_FAILED		- Backup unable to complete sucessfully.
#
#  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}"

PREV_VERSIONS="PreviousVersions"


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

function Initialize
{
	# 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 the CD
	if [ "$MEDIA" = "$CDROM" ]
	then
		UnMountCdrom
	fi

	#  Make a copy of the 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 BackupPreviousVersions()
{
#	Return Values:
#		EXIT_CODE_OK					- backup not necessary or backup successful
#		EXIT_CODE_FAILED				- backup necessary but unable to backup
#
	# Previous versions are only stored on CD
	if [ "$MEDIA" != "$CDROM" ]
	then
		LogMessage "No previous versions available on '$MEDIA'."
		return $EXIT_CODE_OK
	fi

	# Remount the CD
	MountCdrom
	local ret_mount=$?
	if [ "$ret_mount" != "$EXIT_CODE_OK" ]
	then
		LogMessage "ERROR: $MEDIA_NAME failed to mount to '$MEDIA' (return code = ${ret_mount})."
		#  NOTE:  We don't want a failure here to prevent the upcoming install!
		return $EXIT_CODE_OK	#$EXIT_CODE_FAILED
	fi

	# Look for the previous versions folder
	if [ -d "${MEDIA}/$PREV_VERSIONS" ]
	then
		/bin/cp -u ${MEDIA}/${PREV_VERSIONS}/*.zip $LOCAL_INSTALLPREVIOUS
		local ret_cp=$?
		if [ "$ret_cp" != "0" ]
		then
			LogMessage "ERROR: Copying previous versions from '${MEDIA}/$PREV_VERSIONS' to local drive failed with exit code $ret_cp."
			#  NOTE:  We don't want a failure here to prevent the upcoming install!
			return $EXIT_CODE_OK	#$EXIT_CODE_FAILED
		else
			LogMessage "Copied previous versions from '${MEDIA}/$PREV_VERSIONS' to local drive."
			return $EXIT_CODE_OK
		fi
	else
		LogMessage "No previous versions found on '$MEDIA'."
		return $EXIT_CODE_OK
	fi
}


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

Initialize

BackupPreviousVersions
ret=$?

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

#
#  End of Script
#