#!/bin/bash
# /etc/rc.d/laikasd — the AnotherOS time daemon (#745).
#
# laikasd keeps CLOCK_REALTIME right over SNTP (RFC 4330) against the server in
# /etc/laikas.conf, and applies the system timezone from /etc/timezone. It is
# started AFTER network (rc runs /etc/rc.d/* in shell glob order, and `laikasd`
# sorts after `network`), which matters: the daemon's first act is to resolve a
# hostname, and a resolver called before the interface is up just burns the
# first retry.
#
# It never blocks the boot. A failed sync is a status line, not an exit code —
# an unreachable NTP server is an ordinary state on a laptop and must not be
# the reason a desktop does not come up.
case "$1" in
    start)
        # A missing binary exits 1 with a message rather than 0 in silence, for
        # the reason spelled out in rc.d/sshd: "not installed" and "installed
        # and broken" have to look different on the console.
        if [ ! -x /bin/laikasd ]; then
            echo "rc: laikasd: /bin/laikasd missing or not executable" >&2
            exit 1
        fi
        /bin/laikasd &
        ;;
    stop)
        # By PID from the file laikasd writes, not by name: killall would also
        # take out an unrelated process whose name happens to match, and the
        # daemon removes its own pidfile on the way out.
        if [ -r /run/laikasd.pid ]; then
            kill "$(cat /run/laikasd.pid)" 2>/dev/null
        fi
        ;;
    *)
        echo "usage: $0 start|stop" >&2; exit 1 ;;
esac
exit 0
