#!/usr/bin/env bash
# Restart an active emergency encoder if FFmpeg output progress stops advancing.
set -euo pipefail

SERVICE="${SERVICE:-emergency-standby.service}"
CHECKER="${CHECKER:-/opt/viewregatta/TRAP/v3/broadcast/deploy/check_emergency_standby.sh}"

# An intentionally stopped service must stay stopped during maintenance.
if ! systemctl is-active --quiet "$SERVICE"; then
    exit 0
fi
if "$CHECKER"; then
    exit 0
fi

logger -t emergency-standby-watchdog "encoder progress is missing or stale; restarting $SERVICE"
systemctl restart "$SERVICE"
