#!/usr/sbin/openrc-run

description="Save system clock to hardware on shutdown."

depend()
{
	provide hwclock clock
}

if [ -e /run/systemd/system ]; then
	: # handled elsewhere under systemd
fi

unset TZ

_hwclocksh_stop()
{
	HCTOSYS_DEVICE=rtc0
	[ ! -x /sbin/hwclock ] && return 0
	[ ! -r /etc/default/rcS ] || . /etc/default/rcS
	[ ! -r /etc/default/hwclock ] || . /etc/default/hwclock

	if [ "$(uname -s)" = GNU ]; then
		einfo "Saving the system clock to CMOS"
		/sbin/hwclock --directisa --systohc
	else
		einfo "Saving the system clock to /dev/$HCTOSYS_DEVICE"
		/sbin/hwclock --rtc=/dev/$HCTOSYS_DEVICE --systohc
	fi
	return $?
}

start()
{
	# start is handled by /usr/lib/udev/rules.d/85-hwclock.rules.
	return 0
}

stop()
{
	_hwclocksh_stop
	return $?
}

reload()
{
	stop
	return $?
}
