#!/bin/sh /etc/rc.common
#
# clockguard - repair an impossible boot clock before anything depends on it.
#
# START=01 puts this immediately after /etc/init.d/sysfixtime (START=00), which
# reads the RTC and therefore adopts the bad value on this board, and well before
# logd, dropbear and uhttpd start stamping logs or minting certificates against
# it. See /usr/sbin/clockguard for the whole story.
#
# The reason this is a separate service rather than a patch to sysfixtime:
# sysfixtime is upstream base-files and its behaviour is correct for the boards
# it was written for. Patching it would mean carrying a base-files diff forever
# and re-rebasing it on every OpenWrt bump.

START=01
STOP=99

boot() {
	start
}

start() {
	# Exits 2 (no reference time) is not a failure - clockguard logged why and
	# left the clock alone. Only a clock it decided to fix and could not fix
	# comes back non-zero, and that is worth rc.common reporting.
	/usr/sbin/clockguard --fix
}

stop() {
	# Shutdown is the one moment the clock has been running long enough to be
	# worth remembering for the next cold start. Best effort.
	/usr/sbin/clockguard --ratchet --quiet
}
