#!/usr/bin/env bash
set -euo pipefail

###############################################################################
# setup-haproxy-watchdog.sh
#
# Deployment update script to configure the HAProxy watchdog:
#   1. Install the watchdog cron job (idempotent)
#   2. Set up logrotate for the watchdog log
#   3. Recreate haproxy-manager container if it lacks a HEALTHCHECK
#   4. Ensure the watchdog script is executable
###############################################################################

echo "=== Setting up HAProxy watchdog ==="

# ---------------------------------------------------------------------------
# 1. Install the watchdog cron job
# ---------------------------------------------------------------------------
CRON_JOB="* * * * * /root/whp/scripts/haproxy-watchdog.sh >> /var/log/haproxy-watchdog.log 2>&1"

if crontab -l 2>/dev/null | grep -q "haproxy-watchdog"; then
    echo "[cron] Watchdog cron job already installed, skipping."
else
    echo "[cron] Installing watchdog cron job..."
    (crontab -l 2>/dev/null || true; echo "$CRON_JOB") | crontab -
    echo "[cron] Done."
fi

# ---------------------------------------------------------------------------
# 2. Set up logrotate for /var/log/haproxy-watchdog.log
# ---------------------------------------------------------------------------
LOGROTATE_CONF="/etc/logrotate.d/haproxy-watchdog"

echo "[logrotate] Writing ${LOGROTATE_CONF}..."
cat > "$LOGROTATE_CONF" <<'EOF'
/var/log/haproxy-watchdog.log {
    daily
    rotate 7
    compress
    delaycompress
    missingok
    notifempty
    size 10M
}
EOF
echo "[logrotate] Done."

# ---------------------------------------------------------------------------
# 3. Recreate haproxy-manager container if it lacks a HEALTHCHECK
# ---------------------------------------------------------------------------
CONTAINER_NAME="haproxy-manager"

if docker inspect "$CONTAINER_NAME" &>/dev/null; then
    HEALTHCHECK=$(docker inspect --format='{{json .Config.Healthcheck}}' "$CONTAINER_NAME" 2>/dev/null || echo "null")

    if [ "$HEALTHCHECK" = "null" ] || [ "$HEALTHCHECK" = "{}" ] || [ -z "$HEALTHCHECK" ]; then
        echo "[docker] Container '$CONTAINER_NAME' has no HEALTHCHECK configured. Recreating..."
        /root/whp/scripts/container-manager.sh recreate "$CONTAINER_NAME"
        echo "[docker] Done."
    else
        echo "[docker] Container '$CONTAINER_NAME' already has a HEALTHCHECK, skipping recreate."
    fi
else
    echo "[docker] Container '$CONTAINER_NAME' is not running, skipping healthcheck check."
fi

# ---------------------------------------------------------------------------
# 4. Ensure the watchdog script is executable
# ---------------------------------------------------------------------------
# Note: update scripts run BEFORE rsync copies files to /root/whp/, so the
# watchdog script may not exist yet. Make this a best-effort operation.
if [ -f /root/whp/scripts/haproxy-watchdog.sh ]; then
    echo "[chmod] Ensuring /root/whp/scripts/haproxy-watchdog.sh is executable..."
    chmod +x /root/whp/scripts/haproxy-watchdog.sh
    echo "[chmod] Done."
else
    echo "[chmod] /root/whp/scripts/haproxy-watchdog.sh not yet present (will be synced by update). Skipping."
fi

echo "=== HAProxy watchdog setup complete ==="
