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

###############################################################################
# setup-haproxy-crash-monitor.sh
#
# Deployment update script to configure the HAProxy crash monitor daemon:
#   1. Install the systemd service unit (idempotent)
#   2. Create log files with proper permissions
#   3. Set up logrotate for the crash monitor logs
#   4. Enable and start the service
#   5. Ensure the crash monitor script is executable
###############################################################################

echo "=== Setting up HAProxy crash monitor ==="

# ---------------------------------------------------------------------------
# 1. Install systemd service unit
# ---------------------------------------------------------------------------
SERVICE_SRC="/root/whp/configs/haproxy-crash-monitor.service"
SERVICE_DST="/etc/systemd/system/haproxy-crash-monitor.service"

if [ -f "$SERVICE_SRC" ]; then
    echo "[systemd] Installing haproxy-crash-monitor.service..."
    cp "$SERVICE_SRC" "$SERVICE_DST"
    chmod 644 "$SERVICE_DST"
    systemctl daemon-reload
    echo "[systemd] Done."
else
    echo "[systemd] Service file not yet present at $SERVICE_SRC (will be synced by update). Skipping."
    echo "=== HAProxy crash monitor setup deferred ==="
    exit 0
fi

# ---------------------------------------------------------------------------
# 2. Create log files with proper permissions
# ---------------------------------------------------------------------------
echo "[logs] Creating log files..."
touch /var/log/haproxy-crash-monitor.log
chmod 640 /var/log/haproxy-crash-monitor.log

touch /var/log/haproxy-crash-events.json
chmod 640 /var/log/haproxy-crash-events.json

echo "[logs] Done."

# ---------------------------------------------------------------------------
# 3. Set up logrotate
# ---------------------------------------------------------------------------
LOGROTATE_CONF="/etc/logrotate.d/haproxy-crash-monitor"

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

/var/log/haproxy-crash-events.json {
    daily
    rotate 30
    compress
    delaycompress
    missingok
    notifempty
    size 10M
}
EOF
echo "[logrotate] Done."

# ---------------------------------------------------------------------------
# 4. Ensure the crash monitor script is executable
# ---------------------------------------------------------------------------
if [ -f /root/whp/scripts/haproxy-crash-monitor.sh ]; then
    echo "[chmod] Ensuring /root/whp/scripts/haproxy-crash-monitor.sh is executable..."
    chmod +x /root/whp/scripts/haproxy-crash-monitor.sh
    echo "[chmod] Done."
else
    echo "[chmod] /root/whp/scripts/haproxy-crash-monitor.sh not yet present (will be synced by update). Skipping."
fi

# ---------------------------------------------------------------------------
# 5. Enable and start the service
# ---------------------------------------------------------------------------
echo "[systemd] Enabling and starting haproxy-crash-monitor..."
systemctl enable haproxy-crash-monitor
systemctl restart haproxy-crash-monitor

if systemctl is-active --quiet haproxy-crash-monitor; then
    echo "[systemd] haproxy-crash-monitor is running."
else
    echo "[systemd] WARNING: haproxy-crash-monitor failed to start. Check: journalctl -u haproxy-crash-monitor"
fi

echo "=== HAProxy crash monitor setup complete ==="
