#!/bin/bash
# NOQ Purser, server side: one command.
#   sudo purser install --listen <ip> [--port 2222] ...   set up the gate and the nightly job (re-runnable)
#   sudo purser pair                                      pair a box (QR code / line for its page)
#   sudo purser unpair <id>                               the box may no longer connect
#   purser status                                         sources, generations, boxes, last acknowledgement
#   sudo purser restore --disk /dev/sdX [--test <id>]     (another Linux machine) restore, restore drill
#   sudo purser uninstall                                 remove programs and units (data and settings kept aside)
set -euo pipefail
PREFIX=$(cd "$(dirname "$(readlink -f "$0")")/.." && pwd)
SHARE=$PREFIX/share/noq-purser
VERSION=$(cat "$SHARE/version" 2>/dev/null || echo dev)
case ${1:-help} in
  install) shift; exec bash "$SHARE/install.sh" "$@" ;;
  uninstall) exec bash "$SHARE/install.sh" --uninstall ;;
  pair) shift; exec "$PREFIX/sbin/purser-pair" "$@" ;;
  restore)   # also from the unpacked package or the repository, without installing (the restore machine)
    shift; r=$PREFIX/sbin/purser-restore; [ -x "$r" ] || r=$(dirname "$(readlink -f "$0")")/purser-restore
    exec bash "$r" "$@" ;;
  unpair) exec "$PREFIX/sbin/purser-admit" remove "${2:?box id, e.g. A4X7}" ;;
  status)
    # shellcheck source=/dev/null
    . /etc/purser/source.conf
    echo "NOQ Purser $VERSION (server)"
    echo "sources:"; for s in "${SOURCES[@]}"; do p=${s#*:}; p=${p%%:*}; printf '  %-12s %-40s %s\n' "${s%%:*}" "$p" "$(du -sh "$p" 2>/dev/null | cut -f1)"; done
    echo "newest generation: $(cat "$BASE/generation/LATEST" 2>/dev/null || echo none)"
    last=$(find "$BASE/ack" -maxdepth 1 -type f -name '20*' -printf '%f\n' 2>/dev/null | sort | tail -1)
    echo "last acknowledgement: ${last:-none}${last:+ ($(cut -d' ' -f1 "$BASE/ack/$last"))}"
    d=$(grep -E ' drill_ok$' "$BASE/ack/.notes" 2>/dev/null | tail -1)
    if [ -n "$d" ]; then read -r at box _ <<< "$d"; echo "last restore drill reported: $(date -d "@$at" +%F) (box ${box:-?})"
    else echo "last restore drill reported: none (purser restore --test, on another machine)"; fi
    echo "boxes:"; if [ -s /etc/purser/boxes ]; then while read -r id ip _; do echo "  NOQ-PURSER-$id  $ip"; done < /etc/purser/boxes; else echo "  none (sudo purser pair)"; fi
    age=$(( ( $(date +%s) - $(stat -c %Y "$SHARE/version" 2>/dev/null || date +%s) ) / 86400 ))
    [ "$age" -gt 182 ] && echo "this release is $age days old: look for a newer one (nothing is checked online)"
    exit 0 ;;
  version|--version) echo "NOQ Purser $VERSION" ;;
  *) sed -n '3,9p' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;;
esac
