#!/usr/bin/env bash

: <<'END'
dhgpsmonitor
DigiHub GPS position monitor daemon - wrapper for gpsupdate.py

Version 0.1

Steve de Bode - W0FFS - July 2026

Input:	none - runs continuously under systemd (unit: dhgpsmonitor.service)
	uses DigiHubGPSport, DigiHubGPSbaud, DigiHubvenv,
	DigiHubGPSpoll, DigiHubGPSthreshold (from EnvironmentFile)
Output:	none - updates $HOME/.dhinfo via gpsupdate.py when position changes
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 [ -z "${DigiHubvenv:-}" ] || [ ! -x "$DigiHubvenv/bin/python3" ]; then
  err_section "GPS Monitor"
  err_kv "Message" "DigiHub Python virtual environment not found."
  printf "\n" >&2
  exit 1
fi

GPSUPDATE="/usr/local/bin/gpsupdate.py"

poll="${DigiHubGPSpoll:-30}"
[[ "$poll" =~ ^[0-9]+$ ]] || poll=30

fail_limit=5
fail_count=0

section "GPS Monitor"
kv "Poll interval" "${poll}s"
kv "Threshold"     "${DigiHubGPSthreshold:-20}m"
printf "\n"

while :; do
  "$DigiHubvenv/bin/python3" "$GPSUPDATE"
  rc=$?

  case "$rc" in
    1)
      printf '%s Position updated in .dhinfo\n' "$(date '+%F %T')"
      fail_count=0
      ;;
    2)
      err_kv "Warning" "$(date '+%F %T') .dhinfo missing or malformed"
      ;;
    3)
      fail_count=$((fail_count + 1))
      if (( fail_count % fail_limit == 0 )); then
        err_kv "Warning" "$(date '+%F %T') no GPS fix for $fail_count consecutive checks (device unreachable or no satellite lock)"
      fi
      ;;
    *)
      fail_count=0
      ;;
  esac

  sleep "$poll"
done
