#!/bin/sh
# /usr/prog/klipper/klipperDaemon -- REPLACED by the mod.
#
# Stock, this forks klippy with start-stop-daemon -b. klippy is an s6-rc
# longrun now, so anything that still calls this would put a second,
# unsupervised klippy next to the supervised one, fighting for /dev/ttyS4 and
# /tmp/uds. So `start` does nothing, and exits 0 so a caller that treats
# failure as fatal is not broken by it.
#
# NOTHING MACHINE-SPECIFIC LIVES HERE, which is what lets anvil-link-prog.sh
# symlink this file like the other two in prog/. KLIPPER_NICENESS used to, and
# the installer seded the stock value in on every flash; it is NICE_KLIPPER in
# anvil.conf now.
MODDIR=/usr/data/anvil
S6RC=$MODDIR/bin/s6-rc

case "$1" in
start)
    echo "klipperDaemon: klipper is supervised -- use '$S6RC -u change klipper'"
    exit 0
    ;;
stop)
    # Redirected rather than reimplemented. Stock semantics would signal a
    # pidfile nothing writes any more, so `stop` would silently do nothing and
    # s6 would keep klippy up regardless. This also stops what depends on
    # klipper: ff-startup, and so the UI.
    [ -x "$S6RC" ] || { echo "klipperDaemon: no $S6RC"; exit 0; }
    # -t: an infinite deadline is EOVERFLOW on a 32-bit time_t.
    exec "$S6RC" -t 60000 -d change klipper
    ;;
restart)
    # `s6-svc -t` and not two s6-rc transitions: it takes no lock on the live
    # directory, so this is safe to call from inside one, and it leaves the
    # WANTED state alone. -wr returns when the supervisor has started it again.
    [ -x "$MODDIR/bin/s6-svc" ] || { echo "klipperDaemon: no $MODDIR/bin/s6-svc"; exit 0; }
    exec "$MODDIR/bin/s6-svc" -wr -T 60000 -t "$MODDIR/etc/s6/klipper"
    ;;
status)
    [ -x "$MODDIR/bin/s6-svstat" ] || { echo "klipperDaemon: no $MODDIR/bin/s6-svstat"; exit 0; }
    exec "$MODDIR/bin/s6-svstat" "$MODDIR/etc/s6/klipper"
    ;;
*)
    echo "usage: $0 {start|stop|restart|status}"
    exit 1
    ;;
esac
