#!/usr/bin/env bash

: <<'END'
dhpatd
DigiHub Winlink (Pat) service wrapper

Version 0.1

Steve de Bode - W0FFS - July 2026

Input:	none - runs continuously under systemd (unit: dhpat.service)
	reads $HOME/.config/pat/config.json, generated by patconf.py
Output:	none - runs Pat's web interface until stopped
END

set -uo pipefail

UI_LIB="/usr/local/lib/digihub/dh.lib"
[ -r "$UI_LIB" ] && source "$UI_LIB" || {
  printf "\nERROR: Missing UI library: %s\n\n" "$UI_LIB" >&2
  exit 1
}

if ! command -v pat >/dev/null 2>&1; then
  err_section "DigiHub Winlink"
  err_kv "Message" "pat is not installed."
  err_kv "Action" "Install it from https://www.plotlost.com/static/downloads/dependencies/pat/"
  printf "\n" >&2
  exit 1
fi

CONF="$HOME/.config/pat/config.json"
if [ ! -f "$CONF" ]; then
  err_section "DigiHub Winlink"
  err_kv "Message" "Configuration not found: $CONF"
  err_kv "Action" "Run: dhpat on"
  printf "\n" >&2
  exit 1
fi

exec pat http
