#!/bin/bash
# =========================================================
# FFmpeg Multi-Service Watchdog
# Monitors multiple systemd services for critical FFmpeg errors,
# restarts the affected one automatically (with cooldown),
# and logs hourly restart summaries.
# =========================================================

# List of systemd service names to monitor
SERVICES=(
  "Tv-Caracol-Television-Blu-Radio"
  "Tv-Caracol-Television-Caracol-HD"
  "Tv-Caracol-Television-Caracol-HD2"
  "Tv-Caracol-Television-La-Kalle-HD"
  "Tv-RCN-Television-La-FM"
  "Tv-RCN-Television-La-Mega"
  "Tv-RCN-Television-RCN-HD"
  "Tv-RCN-Television-RCN-HD2"
  "Tv-RCN-Television-RCN-Radio"
  "Tv-RCN-Television-Radio-Uno"
)

# Error patterns to watch for
ERROR_PATTERNS=(
  "Non-monotonic DTS"
  "reference picture missing during reorder"
)

# Restart cooldown per service (seconds)
COOLDOWN=15
# How often to log summary (seconds)
SUMMARY_INTERVAL=$((60 * 60))  # 1 hour

echo "🟢 FFmpeg watchdog started for ${#SERVICES[@]} services."

# Build journalctl command
CMD=(journalctl -f)
declare -A restartMap
declare -A restartCount
for svc in "${SERVICES[@]}"; do
  CMD+=(-u "$svc")
  restartMap["$svc"]=0
  restartCount["$svc"]=0
done

lastSummaryTime=$(date +%s)

# Function to print hourly summary
print_summary() {
  echo "🕒 FFmpeg restart summary ($(date '+%Y-%m-%d %H:%M:%S')):"
  for svc in "${SERVICES[@]}"; do
    echo "  • $svc: ${restartCount[$svc]} restart(s) in the last hour"
    restartCount["$svc"]=0
  done
}

# Monitor logs
"${CMD[@]}" | while IFS= read -r line; do
  now=$(date +%s)

  # Periodically print a summary
  if (( now - lastSummaryTime >= SUMMARY_INTERVAL )); then
    print_summary
    lastSummaryTime=$now
  fi

  for pattern in "${ERROR_PATTERNS[@]}"; do
    if [[ "$line" == *"$pattern"* ]]; then
      SERVICE_TAG=$(echo "$line" | sed -n 's/.*\[\(Tv-[^]]*\)\].*/\1/p')
      if [[ -n "$SERVICE_TAG" ]]; then
        currentTimeStamp=$(date +%s)
        lastRestart=${restartMap["$SERVICE_TAG"]}

        if (( currentTimeStamp - lastRestart > COOLDOWN )); then
          echo "⚠️ FFmpeg error matched ('$pattern') in $SERVICE_TAG — restarting service..."
          systemctl restart "$SERVICE_TAG"
          restartMap["$SERVICE_TAG"]=$currentTimeStamp
          ((restartCount["$SERVICE_TAG"]++))
        else
          echo "⏱️ Restart skipped for $SERVICE_TAG (within cooldown window)"
        fi
      else
        echo "⚠️ FFmpeg error matched ('$pattern') — unknown service source."
      fi
      break
    fi
  done
done
:
