#!/bin/sh
########################################################################
# Begin $/opt/midas/bin/sync-time-of-day-to-hardware
#
# Description : Synchronises the systems time-of-day to the BIOS
#               hardware clock.
#               NOTES: 1. This is normally done at system shutdown but
#                         this system is often just switched off.
#                      2. Run the command in background so as not to
#                         imped the calling process.
#
# Authors     : MLA
#
# Version     : 01.00
#
# Notes       : none.
#
########################################################################



# ... errors
#     NOTE: these should be handled in CHardwareUtilities.cpp.
ERROR_NONE=0
ERROR_FAILED=1




. /etc/sysconfig/rc
. ${rc_functions}
. /etc/sysconfig/clock


# ... ripped off from /etc/rd.c/init.d/setclock
CLOCKPARAMS=

case "${UTC}" in
	yes|true|1)
		CLOCKPARAMS="${CLOCKPARAMS} --utc"
		;;
		
	no|false|0)
		CLOCKPARAMS="${CLOCKPARAMS} --localtime"
		;;
esac


if [ -e /opt/midas/bin/PackageManifest.xml ]; then

	grep -q 'product=\"DL2\"\|product=\"DL1\"\|product=\"PRO2\"' /opt/midas/bin/PackageManifest.xml
	if [ "$?" -eq "0" ]; then
		# DL2 version.
		#   Without the --directisa option the operation fails with
		#   timeout waiting for /dev/rtc.
		#   Not sure what the correct solution is but this seems to be
		#   a viable workaround.
		CLOCKPARAMS="${CLOCKPARAMS} --directisa"
	fi

else
	# /opt/midas/bin/PackageManifest.xml does not exist so we can't determine
	# which product this is running on.
	CLOCKPARAMS="${CLOCKPARAMS} --directisa"
fi


hwclock --systohc ${CLOCKPARAMS} &>/dev/null
if [ "$?" -eq "0" ]; then
	#
	# Executed successfully.
	#
#	kill $(ps -AH | grep mastercont | cut -b1-6 )
	exit $ERROR_NONE
else
	#
	# Failed to excute.
	#
#	kill $(ps -AH | grep mastercont | cut -b1-6 )
	echo "ERROR: FAILED TO SYNCHRONISE SYSTEM TIME OF DAY TO HARDWARE"
	exit $ERROR_FAILED
fi

