#!/bin/bash
# /etc/rc.d/aupdated — the AnotherOS software update service (#112).
#
# `aupdate daemon` checks the signed repository for new `coresystem` and
# `kernel` packages every `interval` (default 30 minutes) while the network is
# up, and — as /etc/aupdate.conf's `mode` says — only records what it found
# (notify), downloads and verifies it (download), or also installs it
# (automatic). `mode = off` makes it contact nothing. The mode, the interval and
# a manual "Check Now" live in System Preferences > Software Update, which
# writes the same file through `aupdate config`.
#
# The daemon writes its pid to /run/aupdated.pid and its state to
# /State/lib/aupdate/status; the log is /var/log/aupdate.log.
case "$1" in
    start)
        AUPDATE=$(command -v aupdate 2>/dev/null)
        [ -n "$AUPDATE" ] || { echo "rc: aupdated: no aupdate on PATH — not run"; exit 0; }
        mkdir -p /var/log /run 2>/dev/null
        "$AUPDATE" daemon >> /var/log/aupdate.log 2>&1 &
        ;;
    stop)
        [ -r /run/aupdated.pid ] && kill "$(cat /run/aupdated.pid)" 2>/dev/null
        ;;
    *)
        echo "usage: $0 start|stop" >&2; exit 1 ;;
esac
exit 0
