#!/usr/bin/env bash
# install-whp-monitor-poll-deps.sh
#
# Ensures the Python dependencies whp-monitor-poll.py needs are installed.
# The original setup-whp-monitor-poll.sh only installed the cron entry —
# servers without urllib3/requests/pymysql had the cron silently failing
# on every minute, leaving the new heartbeat indicator blank in the UI.
#
# Idempotent — safe to run multiple times. pip is a no-op when the
# package is already at the requested version.
set -euo pipefail

if ! command -v pip3 >/dev/null 2>&1; then
    echo "Installing python3-pip..."
    if command -v dnf >/dev/null 2>&1; then
        dnf install -y python3-pip
    elif command -v apt-get >/dev/null 2>&1; then
        apt-get update -qq && apt-get install -y python3-pip
    else
        echo "FATAL: neither dnf nor apt-get available; install python3-pip manually." >&2
        exit 1
    fi
fi

# Install the three deps the poll script imports: urllib3 (TLS warning
# suppression on the localhost health.php call), requests (HTTP client),
# pymysql (MySQL driver — same one used by whp_monitor_core.py).
echo "Ensuring python3 packages: urllib3 requests pymysql"
pip3 install --quiet urllib3 requests pymysql

# Smoke-check: import all three.
if ! python3 -c 'import urllib3, requests, pymysql' 2>/dev/null; then
    echo "FATAL: imports failed after install" >&2
    exit 1
fi

echo "install-whp-monitor-poll-deps.sh: OK"
