#!/sbin/runscript
# Copyright 1999-2002 Gentoo Technologies, Inc.
# Distributed under the terms of the GNU General Public License v2
# $Header: /home/cvsroot/gentoo-src/rc-scripts/init.d/clock,v 1.10 2002/11/18 13:07:02 azarah Exp $


depend() {
	need localmount
}

start() {
	local myopts=""
	local TBLURB=""
	local errstr=""
	if [ ! -f /etc/adjtime ]
	then
		echo "0.0 0 0.0" > /etc/adjtime
	fi
	if [ "${CLOCK}" = "UTC" ]
	then
		myopts="--utc"
		TBLURB="[UTC]"
	else
		myopts="--localtime"
		TBLURB="[Local Time]"
	fi

	if [ -x /sbin/hwclock ]
	then
		ebegin "Setting system clock to hardware clock ${TBLURB}"
		errstr="$(/sbin/hwclock --adjust ${myopts} 2>&1 >/dev/null)"
		errstr="${errstr}$(/sbin/hwclock --hctosys ${myopts} 2>&1 >/dev/null)"
		#hwclock do not return a fail status
		if [ -n "${errstr}" ]
		then
			eend 1 "Failed to set system clock to hardware clock"
			return 1
		else
			eend 0
			return 0
		fi
	else
		eend 1 "hwclock executable not found"
	fi
}

stop() {
	local myopts=""
	local TBLURB=""
	local errstr=""
	if [ "${CLOCK}" = "UTC" ]
	then
		myopts="--utc"
		TBLURB="[UTC]"
	else
		myopts="--localtime"
		TBLURB="[Local Time]"
	fi

	if [ -x /sbin/hwclock ]
	then
		ebegin "Syncing hardware clock to system clock ${TBLURB}"
		errstr="$(/sbin/hwclock --systohc ${myopts} 2>&1 >/dev/null)"
		if [ -n "${errstr}" ]
		then
			eend 1 "Failed to set hardware clock to system clock"
			return 1
		else
			eend 0
			return 0
		fi
	else
		eend 1 "hwclock executable not found"
	fi
}


# vim:ts=4
