#!/bin/sh
###############################################################################
#                                                                             #
# IPFire.org - A linux based firewall                                         #
# Copyright (C) 2007-2022  IPFire Team  <info@ipfire.org>                     #
#                                                                             #
# This program is free software: you can redistribute it and/or modify        #
# it under the terms of the GNU General Public License as published by        #
# the Free Software Foundation, either version 3 of the License, or           #
# (at your option) any later version.                                         #
#                                                                             #
# This program is distributed in the hope that it will be useful,             #
# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
# GNU General Public License for more details.                                #
#                                                                             #
# You should have received a copy of the GNU General Public License           #
# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
#                                                                             #
###############################################################################

. /etc/sysconfig/rc
. ${rc_functions}

CLOCKPARAMS=

# Restore log time
# This is a fallback mechanism for systems without or with broken RTCs which
# will reset the system clock to at least the timestamp of the last log file
# modification date.
restore_log_time() {
	local file="/var/log/messages"

	# Cannot restore the log time if the file does not exist
	if [ ! -e "${file}" ]; then
		return 0
	fi

	# Fetch the log timestamp
	local t_log="$(stat --format="%Y" "${file}")"

	# Fetch the current system time
	local t_sys="$(date "+%s")"

	# If the log time greater than the system time, we update the system time
	if [ -n "${t_log}" -a -n "${t_sys}" -a "${t_log}" -gt "${t_sys}" ]; then
		boot_mesg "The clock has been reset to the last log access" "${WARNING}"

		# Set the time
		date -s "@${t_log}" >/dev/null
		evaluate_retval
	fi

	return 0
}

case ${1} in
	start)

		boot_mesg "Setting system clock..."

		FDT_COMPAT_FILE="/sys/firmware/devicetree/base/compatible"
		# RTC may not be automatically loaded on some
		# non-x86 machines
		if [ -f "${FDT_COMPAT_FILE}" ] && \
			( grep -q "traverse,ten64" "${FDT_COMPAT_FILE}" ); then
			modprobe rtc-rx8025
		fi

		# udev not create the rtc symlink if rtc is in the kernel
		if [ ! -e /dev/rtc ]; then
			if [ -e /dev/rtc0 ]; then
				ln -s rtc0 /dev/rtc
			fi
		fi

		hwclock --hctosys ${CLOCKPARAMS} &>/dev/null

		# Restore the log time if the system clock is behind time
		restore_log_time
		;;

	stop)
		boot_mesg "Setting hardware clock..."
		hwclock --systohc ${CLOCKPARAMS} &>/dev/null
		evaluate_retval
		;;

	*)
		echo "Usage: ${0} {start} {stop}"
		;;

esac
