#!/bin/bash
# NOQ Purser: box core. Bash and distribution packages only.
#
# Nightly and internal:  run [--first-fill] | tick | boot | firstboot | apply-config
# At the box (code):     clear <id> | pin <id> | good <id> | ungood <id> | disk-on | first-fill | selftest | new-code | shell | setup
# Without a code:        status | disk-off | notify-test | setup-code (only if none set) | reset-code (always notified)
# Page and switch:       pair <token> | check-code (code on stdin) | selftest-nocode | disk-on-nocode | notify-maint on|off|still-on | light <state>
set -euo pipefail
umask 022
PURSER_LIB=${PURSER_LIB:-/usr/local/lib/purser}
# shellcheck source=lib/common.sh
. "$PURSER_LIB/common.sh"
# shellcheck source=lib/msg.sh
. "$PURSER_LIB/msg.sh"
# shellcheck source=lib/notify.sh
. "$PURSER_LIB/notify.sh"
# shellcheck source=lib/hw.sh
. "$PURSER_LIB/hw.sh"
# shellcheck source=lib/sandbox.sh
. "$PURSER_LIB/sandbox.sh"
# shellcheck source=lib/disk.sh
. "$PURSER_LIB/disk.sh"
# shellcheck source=lib/signals.sh
. "$PURSER_LIB/signals.sh"
# shellcheck source=lib/retention.sh
. "$PURSER_LIB/retention.sh"
# shellcheck source=lib/maint.sh
. "$PURSER_LIB/maint.sh"
# shellcheck source=lib/night.sh
. "$PURSER_LIB/night.sh"
# shellcheck source=lib/code.sh
. "$PURSER_LIB/code.sh"
# shellcheck source=lib/status.sh
. "$PURSER_LIB/status.sh"
# shellcheck source=lib/selftest.sh
. "$PURSER_LIB/selftest.sh"
# shellcheck source=lib/setup.sh
. "$PURSER_LIB/setup.sh"
# shellcheck source=lib/pair.sh
. "$PURSER_LIB/pair.sh"

load_config
load_messages
install -d -m 0700 "$STATE" 2>/dev/null || true

# Boot: the board has no battery clock, so chrony may step the clock during its first updates only; after that
# it refuses any change larger than CLOCK_JUMP_MAX (and the core reports it). The LED shows the
# last known state. After a reboot the disk is locked anyway (no mapper, no mount).
cmd_boot() {
  install -d -m 0755 "$RUN_DIR/chrony.d"
  printf 'makestep 1 3\nmaxchange %s 3 -1\n' "$CLOCK_JUMP_MAX" > "$RUN_DIR/chrony.d/step.conf"
  rtc_boot
  led_update
}

case ${1:-} in
  run)
    # the first backup has no time limit: until one generation is published, every run is a first fill
    if [ "${2:-}" = --first-fill ] || [ ! -s "$ST_LAST_PUBLISHED" ]; then
      night_run first-fill
      if [ -s "$ST_LAST_PUBLISHED" ]; then
        setup_mark first_fill "done"
        if systemd_up; then systemctl enable --now purser.timer >/dev/null 2>&1 || true; fi
      fi
    else night_run; fi ;;
  tick) tick ;;
  light-refresh)   # udev: the disk came or went; not while a job owns the light
    if systemd_up && systemctl -q is-active purser.service purser-first-fill.service 2>/dev/null; then :; else led_update; fi ;;
  boot) cmd_boot ;;
  firstboot) cmd_firstboot ;;
  apply-config) apply_config ;;
  status) cmd_status ;;
  clear) cmd_clear "${2:-}" ;;
  pin) cmd_pin "${2:-}" ;;
  good) cmd_good "${2:-}" ;;
  ungood) cmd_ungood "${2:-}" ;;
  disk-on) cmd_disk_on ;;
  disk-off) cmd_disk_off ;;
  first-fill) cmd_first_fill ;;
  selftest) if code_is_set; then code_require; fi; cmd_selftest ;;
  setup) cmd_setup ;;
  setup-code) cmd_setup_code ;;
  new-code) cmd_new_code ;;
  reset-code) cmd_reset_code ;;
  shell) cmd_shell ;;
  notify-test) notify test default N_TEST ;;
  pair) cmd_pair "${2:-}" "${3:-}" ;;
  check-code) cmd_check_code ;;
  selftest-nocode) cmd_selftest ;;
  disk-on-nocode) cmd_disk_on_open ;;
  clear-nocode) cmd_clear_open "${2:-}" ;;
  notify-maint) cmd_notify_maint "${2:-}" "${3:-}" ;;
  light) light_set "${2:-off}" "${3:-}" ;;
  rtc-set) cmd_rtc_set "${2:-}" "${3:-}" ;;
  clock-source) clock_source ;;
  version) say "NOQ Purser $PURSER_VERSION" ;;
  *) say "$(msg USAGE)"; exit 2 ;;
esac
