#!/bin/sh
#
# /etc/init.d/clock - initialize system clock / sync hardware clock
#

WITHOUT_RC_COMPAT=1

# Source function library.
. /etc/init.d/functions

# Set the system clock.
ARC=false
SRM=false
UTC=false
HWCLOCK_SET_TIME_AT_START=true
HWCLOCK_SET_AT_HALT=false
HWCLOCK_ADJUST=false
ZONEDIR=/usr/share/zoneinfo

CLOCKMODE=
if SourceIfNotEmpty /etc/sysconfig/clock; then
	# convert old style clock config to new values
	if [ "$CLOCKMODE" = GMT ]; then
		UTC=true
	elif [ "$CLOCKMODE" = ARC ]; then
		ARC=true
	fi
fi

RETVAL=0

CLOCKFLAGS=
CLOCKDEF=

if is_yes "$UTC"; then
	CLOCKFLAGS="$CLOCKFLAGS --utc"
	CLOCKDEF="$CLOCKDEF (utc)"
elif is_no "$UTC"; then
	CLOCKFLAGS="$CLOCKFLAGS --localtime"
	CLOCKDEF="$CLOCKDEF (localtime)"
fi

if is_yes "$ARC"; then
	CLOCKFLAGS="$CLOCKFLAGS --arc"
	CLOCKDEF="$CLOCKDEF (arc)"
fi

if is_yes "$SRM"; then
	CLOCKFLAGS="$CLOCKFLAGS --srm"
	CLOCKDEF="$CLOCKDEF (srm)"
fi

set_date()
{
	action "Setting system clock$CLOCKDEF:" hwclock --hctosys $CLOCKFLAGS
	RETVAL=$?
	action "Today's date: `date`" true
	return $RETVAL
}

set_timezone()
{
	if [ ! -s /etc/localtime ] && [ -n "$ZONE" -a -s "$ZONEDIR/$ZONE" ]; then
		action "Setting timezone information ($ZONE):" cp -p "$ZONEDIR/$ZONE" /etc/localtime
	fi
	RETVAL=$?
	return $RETVAL
}

case "$1" in
	set)
		set_date
		;;
	tzset)
		set_timezone
		;;
	start)
		set_timezone
		if [ ! -s /etc/localtime ]; then
			echo -n 'Setting system clock: timezone not configured'
			passed 'clock startup'
			echo
			exit 1
		fi
		if is_yes "$HWCLOCK_ADJUST" && [ -s /etc/adjtime ]; then
			action "Adjusting hardware clock:" hwclock --adjust
		fi
		is_no "$HWCLOCK_SET_TIME_AT_START" ||
			set_date
		;;
	sync)
		action "Setting hardware clock$CLOCKDEF: `date`" hwclock --systohc $CLOCKFLAGS
		RETVAL=$?
		;;
	stop)
		if is_yes "$HWCLOCK_SET_AT_HALT" && clock_unsynced; then
			# action cannot be used in this context.
			echo -n "Setting hardware clock$CLOCKDEF: `date` "
			hwclock --systohc $CLOCKFLAGS && echo_success || echo_failure
			RETVAL=$?
			echo
		fi
		;;
	status)
		hwclock --show
		;;
	*)
		msg_usage "${0##*/} {set|tzset|start|sync|stop|status}"
		RETVAL=1
		;;
esac

exit $RETVAL
