#!/bin/sh
#

# PROVIDE: rtc
# REQUIRE: FILESYSTEMS
# BEFORE: netif
# KEYWORD: nojail shutdown

. /etc/rc.subr

name="fakertc"
desc="Restore RTC date and time"
rcvar="fakertc_enable"
start_cmd="fakertc_start"
stop_cmd="fakertc_stop"

extra_commands="savertc"
savertc_cmd="${name}_stop"

# Set some defaults
fakertc_enable=${fakertc_enable:-"NO"}
fakertc_file=${fakertc_file:-"/var/db/fakertc"} # File to store RTC on shutdown

_fakertc_format="+%Y%m%d%H%M.%S"

fakertc_start()
{

	if [ ! -s ${fakertc_file} ] ; then
		fakertc_save
		return 0
	fi

	echo -n "Set RTC from ${fakertc_file}: "

	if [ ! -r ${fakertc_file} ] ; then
		warn "${fakertc_file} is not readable"
		return 1
	fi

	_fakertc_date=$( date -u $( cat "${fakertc_file}" ) )
	echo -n "${_fakertc_date}"

	echo '.'
}

fakertc_save()
{

	echo -n "Writing RTC file: ${fakertc_file}"

	oumask=`umask`
	umask 077
	date -u "${_fakertc_format}" > "${fakertc_file}" || warn 'write failed (read-only fs?)'
	umask ${oumask}

	echo '.'
}

fakertc_stop()
{

	fakertc_save
}

load_rc_config $name
run_rc_command "$1"
