#!/bin/sh
#Copyright: 2026 Aitor <aitor@genuen.org>
#License: CC0-1.0
#Comment: oneshot (boot); invoke with: sv once fake-hwclock
#Comment: ends in pause — cleanup (save) runs from ./finish on sv down
#Comment: example of pause+finish (vs networking-style *-stop)

exec 2>&1

ebegin() { [ "${VERBOSE:-}" = no ] || echo "$@"; }
eend() { r=${1:-0}; shift || true; [ "$r" -eq 0 ] || [ "${VERBOSE:-}" = no ] || echo "$*" >&2; return "$r"; }
einfo() { [ "${VERBOSE:-}" = no ] || echo "$@"; }
ewarn() { echo "$@" >&2; }
eerror() { echo "$@" >&2; }

if ! command -v fake-hwclock >/dev/null 2>&1 && \
   [ ! -x /sbin/fake-hwclock ] && [ ! -x /usr/sbin/fake-hwclock ]; then
	exec sv -v pause .
fi

ebegin "Loading system time from fake-hwclock"
fake-hwclock load
eend $? || true

# Stay supervised until shutdown so ./finish can save the clock.
exec sv -v pause .
