#!/usr/bin/env bash
# gateway-watchdog-vps.sh — alert-only watchdog for a systemd-managed Hermes gateway.
#
# Cron contract (no_agent delivery): EMPTY stdout = silent. Non-empty stdout
# = user gets a message. So:
#   - healthy                        -> silent
#   - down inside planned window     -> silent (default 03:00-03:10 UTC — match
#                                       whatever maintenance cron bounces the gateway)
#   - down unexpectedly, 1st detect  -> print alert (once, until resolved)
#   - back up after an alert         -> print resolved (once)
#
# systemd (Restart=on-failure) owns restarts; this script never restarts
# anything — it only tells the user when reality diverges from plan.
#
# Wire-up: cronjob(action=create, no_agent=true, schedule="*/5 * * * *",
# script="gateway-watchdog-vps.sh", deliver="origin"). Note the cron tool
# rejects absolute script paths — pass just the filename; it resolves under
# ~/.hermes/scripts/. And no_agent jobs default to deliver="local" (saved but
# never sent) — set deliver explicitly or alerts vanish into the job log.
set -uo pipefail

STATE="/root/.hermes/logs/gateway-watchdog.state"   # "up" | "down"
LOG="/root/.hermes/logs/gateway-watchdog.log"
UNIT="hermes-gateway.service"

now_min=$(date -u +%H%M)     # e.g. 0305
now_min=${now_min#0}         # strip leading zero for numeric compare

in_window() {
  # Planned maintenance window: 03:00-03:10 UTC (adjust to match the
  # maintenance cron that legitimately stops the gateway, e.g. gbrain-refresh)
  [ "$now_min" -ge 300 ] && [ "$now_min" -le 310 ]
}

is_active() {
  systemctl is-active --quiet "$UNIT"
}

prev="up"
[ -f "$STATE" ] && prev=$(cat "$STATE" 2>/dev/null || echo up)

ts() { date -u +%Y-%m-%dT%H:%M:%SZ; }

if is_active; then
  if [ "$prev" = "down" ]; then
    echo "✅ Gateway is back up ($(ts) UTC)."
    echo "up" > "$STATE"
    echo "[$(ts)] resolved: gateway active again" >> "$LOG"
  fi
  exit 0
fi

# gateway is NOT active
if in_window; then
  exit 0   # expected — maintenance cron owns this window
fi

if [ "$prev" != "down" ]; then
  echo "down" > "$STATE"
  echo "[$(ts)] ALERT: gateway inactive outside maintenance window" >> "$LOG"
  status=$(systemctl is-active "$UNIT" 2>&1)
  lastlog=$(journalctl -u "$UNIT" -n 3 --no-pager 2>/dev/null | tail -3)
  printf "🚨 Gateway is DOWN outside the planned window ($(ts) UTC).\nsystemctl says: %s\nLast log lines:\n%s\nsystemd should auto-restart it — I'll confirm when it's back.\n" "$status" "$lastlog"
fi
exit 0
