#!/bin/sh
#  Note:	The above line determines what interpreter to use to interpret this script.
######################################################
#
#  Program Name:	installscriptupdater
#
#  Program Description:  This script updates the RADAR 24 install scripts from a floppy/cdrom disk.
#
#  Return value:
#	0				- Scripts updated successfully
#	1				- No update necessary
#	EXIT_CODE_xxx		-  Couldn't update scripts
#	
#
#  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.
#
######################################################

#
#  Declare 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##*/}
MYMEDIA=${0%/*}

FALSE=0
TRUE=1
FLOPPY="/boot/floppy"
CDROM="/boot/cdrom"
LOCAL_INSTALL="/boot/Install"
LOCAL_INSTALLCURRENT="/boot/InstallCurrent"
LOCAL_INSTALLPREVIOUS="/boot/InstallPrevious"

BOOT_DIR="/boot"
HOME_DIR="${BOOT_DIR}/home"
RADAR_DIR="${HOME_DIR}/radar"
INSTALL_DIR="${RADAR_DIR}/install"
INSTALL_LOGS_DIR="${INSTALL_DIR}/logs"
INSTALL_LOG="${INSTALL_LOGS_DIR}/${MYNAME}.log"
LOG_RESULTS="/bin/tee -a ${INSTALL_LOG}"

SCRIPT_UPDATE_PKG="${MYMEDIA}/install.zip"
RADAR_RESET="${INSTALL_DIR}/RadarReset"
RADAR_CHECK="${MYMEDIA}/RadarCheck"

# NOTE: 'ret' is a global constant, containing the script's final return code
#
EXIT_CODE_OK=0
EXIT_CODE_UNABLE_TO_CHECK=113
EXIT_CODE_UNABLE_TO_REQUEST_REBOOT=114
EXIT_CODE_UNABLE_TO_UNZIP=121
EXIT_CODE_UNABLE_TO_COPY=122
EXIT_CODE_NO_UPDATE_SCRIPT_PKG=125
EXIT_CODE_TARGET_DIR_DOES_NOT_EXIST=126
EXIT_CODE_FAILED=127


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

function PrintMessage
{
	/bin/echo -e $1
}

function LogMessage
{
	/bin/echo -e $1 | $LOG_RESULTS
}

function Initialize
{
	# Ensure that the install logs directory exists
	/bin/mkdir -p $INSTALL_LOGS_DIR

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

	# Clean up any logs which aren't in the LOGS folder
	/bin/mv -f ${INSTALL_DIR}/*.log* $INSTALL_LOGS_DIR
}

function CleanUp
{
	# 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 CheckCapabilities
{
	if [ ! -f "$RADAR_CHECK" ]
	then
		LogMessage "Unable to check RADAR capabilities because '$RADAR_CHECK' not found."
		# This is fine--perhaps this software release doesn't need to check
		return $EXIT_CODE_OK
	fi

	LogMessage "Running '$RADAR_CHECK' to check RADAR capabilities ... "

	$RADAR_CHECK | $LOG_RESULTS
	local ret_check=$?
	if [ "$ret_check" != "0" ]
	then
		LogMessage "ERROR: RADAR does not meet minimum hardware requirements."

		# Reset the net install directories to prevent a cyclic error
		if [ "$MYMEDIA" = "$LOCAL_INSTALLCURRENT" ]
		then
			LogMessage "Net Install directories have been reset."
			/bin/rm -rf ${LOCAL_INSTALLCURRENT}/*
			/bin/rm -f ${LOCAL_INSTALL}/*
		fi

#		/bin/sync
		ret=$EXIT_CODE_FAILED
		return $ret
	fi

	return $EXIT_CODE_OK
}

function InstallScripts
{
	if [ ! -f "$SCRIPT_UPDATE_PKG" ]
	then
		LogMessage "Install scripts not updated because '$SCRIPT_UPDATE_PKG' not found."
		ret=$EXIT_CODE_NO_UPDATE_SCRIPT_PKG
		return $ret
	fi

	if [ ! -d "$HOME_DIR" ]
	then
		LogMessage "ERROR: Target directory '$HOME_DIR' does not exist."
		ret=$EXIT_CODE_TARGET_DIR_DOES_NOT_EXIST	
		return $ret
	fi

	/bin/unzip -o $SCRIPT_UPDATE_PKG -d $HOME_DIR
	local ret_unzip=$?
	if [ "$ret_unzip" != "0" ]
	then
		LogMessage "ERROR: Unzipping '$SCRIPT_UPDATE_PKG' failed with exit code $ret_unzip."
		ret=$EXIT_CODE_UNABLE_TO_COPY
		return $ret
	fi

	LogMessage "Install scripts updated successfully."

	LogMessage "Cleaning up legacy of old backup method ... "

	RADAR_BACKUPS=$(/bin/ls -1d ${RADAR_DIR}-* 2> /dev/null | $LOG_RESULTS)
	local directory
	for directory in $RADAR_BACKUPS
	do
		if [ -d "$directory" ]
		then
			/bin/rm -rf $directory
			local ret_rm=$?
			if [ "$ret_rm" != "0" ]
			then
				LogMessage "ERROR: Removing backup directory '$directory' failed with exit code $ret_rm."
			else
				LogMessage "Backup directory '$directory' purged."
			fi
		fi
	done

	return $EXIT_CODE_OK
}

function RequestReboot
{
	if [ ! -f "$RADAR_RESET" ]
	then
		LogMessage "Unable to run Reboot Notification app because '$RADAR_RESET' not found."
		# This is not a global error
		return $EXIT_CODE_UNABLE_TO_REQUEST_REBOOT
	fi

	LogMessage "Running '$RADAR_RESET' to request a reboot ... "

	# Clean up and sync filesystem now, since we won't be exiting normally
	CleanUp
	/bin/sync

	$RADAR_RESET

	# Remove the Radar reset program we just ran so that it does not run again
	# until next time it is installed (then sync filesystem again)
	PrintMessage "Removing '$RADAR_RESET' ... "
	/bin/rm -f $RADAR_RESET
	/bin/sync

	return $EXIT_CODE_OK
}


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

ret=$EXIT_CODE_OK
Initialize

if CheckCapabilities
then
	if InstallScripts
	then
		if RequestReboot
		then
			# Reboot immediately, assuming all application data has already been saved
			PrintMessage "System is rebooting ... "
			/bin/shutdown -r -q -d now &
			exit $ret
		fi
	fi
fi

CleanUp
exit $ret

#
#  End of Script
#