#!/bin/sh
#Copyright: 2026 Aitor <aitor@genuen.org>
#License: CC0-1.0
#Comment: oneshot; invoke with: sv once hwclock.sh-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; }

. /lib/init/vars.sh
HCTOSYS_DEVICE=rtc0
[ ! -x /sbin/hwclock ] && exit 0
[ -r /etc/default/rcS ] && . /etc/default/rcS
[ -r /etc/default/hwclock ] && . /etc/default/hwclock
unset TZ
ebegin "Saving the system clock"
case "$(uname -s)" in
  GNU) /sbin/hwclock --directisa --systohc ;;
  *) /sbin/hwclock --rtc=/dev/$HCTOSYS_DEVICE --systohc ;;
esac
eend $?
exit $?
