
unset SKIP_SYNC

egrep "^UTC=no" /etc/default/rcS
if [ $? -eq 0 ] ; then
	LOCALTIME='--localtime'
fi

echo '===================== before clock sync ======================'
echo current time is:
date

echo 'hwclock --localtime'
hwclock --localtime

if [ "$GX_TIME_SYNC_EVERY_TIME" = "true" ] ; then
	echo "going to run 30-clock-sync because GX_TIME_SYNC_EVERY_TIME = true"
else
	DATEDIR=$(date +%Y-%m-%d)
	if [ -e /var/lib/groovix/times-per-day/$DATEDIR/30-clock-sync ] ; then
		echo "already ran 30-clock-sync today, not running again"
		SKIP_SYNC="true"
	else
		mkdir -p /var/lib/groovix/times-per-day/$DATEDIR
		touch /var/lib/groovix/times-per-day/$DATEDIR/30-clock-sync
	fi
	
fi


if [ -z "$SKIP_SYNC" ] &&  [ -n "$GX_TIME_SYNC_SERVER" ] ; then
        #sync time using unprivileged port in case 123 is blocked by firewall
        echo running time ntpdate -b -u $GX_TIME_SYNC_SERVER  at `date`
        time ntpdate -b -u $GX_TIME_SYNC_SERVER 
	echo done running ntpdate at `date`

        #write out updated time to hardware, otherwise it might be lost on hard reset.  Normally it syncs the hardware clock during shutdown, but the clock could have drifted by then
        #does ntpdate already sync the hardware clock? we probably don't need this
        #start hwclock-save >> /var/log/groovix/pacprep.log 2>&1

	echo syncing hwclock
	echo "hwclock $LOCALTIME --systohc"
	hwclock $LOCALTIME --systohc
fi

echo '===================== after clock sync ======================'
echo current time is:
date

echo "hwclock $LOCALTIME"
hwclock $LOCALTIME

