#!/usr/bin/env bash
# Install ethergold-http.service (+ P3 floor-games refresh) on office Pi or collector.
#
# Serves OHD Win11 on port 31234 without DyzineFrame:
#   GET /EtherGold/progressives.csv
#   GET /EtherGold/progressives-sync.json
#   GET /EtherGold/floor-games.json
#
# Usage:
#   sudo bash install_ethergold_http.sh
#
# DyzineFrame on the same Pi should not bind 31234 — install writes
# /etc/profile.d/ethergold-http.sh exporting ETHERGOLD_HTTP_EXTERNAL=1.

if grep -q $'\r' "${BASH_SOURCE[0]}" 2>/dev/null; then
  sed -i 's/\r$//' "${BASH_SOURCE[0]}"
  exec bash "${BASH_SOURCE[0]}" "$@"
fi

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
if [[ -f "${SCRIPT_DIR}/../sync/ethergold_paths.sh" ]]; then
  # shellcheck source=../sync/ethergold_paths.sh
  source "${SCRIPT_DIR}/../sync/ethergold_paths.sh"
else
  # shellcheck source=/dev/null
  source "${HOME}/EtherGold/sync/ethergold_paths.sh"
fi

HTTP_DIR="${SCRIPT_DIR}"
if [[ ! -f "${HTTP_DIR}/progressives_http.py" ]]; then
  echo "FATAL: progressives_http.py not found in ${HTTP_DIR}" >&2
  exit 1
fi

HTTP_UNIT="ethergold-http.service"
REFRESH_UNIT="ethergold-floor-games-refresh.service"
REFRESH_TIMER="ethergold-floor-games-refresh.timer"
ENV_DIR="/etc/ethergold"
HTTP_ENV="${ENV_DIR}/ethergold-http.env"
PROFILE_SNIPPET="/etc/profile.d/ethergold-http.sh"

sudo mkdir -p "${ENV_DIR}"
{
  echo "ETHERGOLD_ROOT=${ETHERGOLD_ROOT}"
  echo "ETHERGOLD_PROGRESSIVES_CSV=${ETHERGOLD_PROGRESSIVES_CSV}"
  echo "ETHERGOLD_HTTP_PORT=31234"
  echo "ETHERGOLD_HTTP_HOST=0.0.0.0"
  echo "P3_SERIAL_DEV=/dev/ttyUSB0"
} | sudo tee "${HTTP_ENV}" >/dev/null

sudo tee "${PROFILE_SNIPPET}" >/dev/null <<'EOF'
# EtherGold HTTP owns port 31234 — DyzineFrame skips Bottle when set.
export ETHERGOLD_HTTP_EXTERNAL=1
EOF

sudo tee "/etc/systemd/system/${HTTP_UNIT}" >/dev/null <<EOF
[Unit]
Description=EtherGold HTTP (progressives + floor-games for OHD)
After=network-online.target local-fs.target
Wants=network-online.target

[Service]
Type=simple
EnvironmentFile=${HTTP_ENV}
WorkingDirectory=${HTTP_DIR}
ExecStartPre=/usr/bin/python3 ${HTTP_DIR}/p3_floor_games_refresh.py
ExecStart=/usr/bin/python3 ${HTTP_DIR}/progressives_http.py
Restart=always
RestartSec=3

[Install]
WantedBy=multi-user.target
EOF

sudo tee "/etc/systemd/system/${REFRESH_UNIT}" >/dev/null <<EOF
[Unit]
Description=Refresh floor-games.json from P3 GAMESTATS (EtherGold)
After=local-fs.target

[Service]
Type=oneshot
EnvironmentFile=${HTTP_ENV}
WorkingDirectory=${HTTP_DIR}
ExecStart=/usr/bin/python3 ${HTTP_DIR}/p3_floor_games_refresh.py --force
EOF

sudo tee "/etc/systemd/system/${REFRESH_TIMER}" >/dev/null <<EOF
[Unit]
Description=Daily P3 floor-games refresh (EtherGold)

[Timer]
OnCalendar=*-*-* 03:15:00
Persistent=true
RandomizedDelaySec=300

[Install]
WantedBy=timers.target
EOF

sudo systemctl daemon-reload
sudo systemctl enable "${HTTP_UNIT}" "${REFRESH_TIMER}"
# Port 31234 may still be held by a running DyzineFrame until local reboot.
sudo systemctl restart "${HTTP_UNIT}" || true
sudo systemctl start "${REFRESH_TIMER}" || true

echo ""
echo "Installed ${HTTP_UNIT} on port 31234"
echo "  curl -s http://127.0.0.1:31234/EtherGold/progressives.csv | head"
echo "  curl -s http://127.0.0.1:31234/EtherGold/floor-games.json"
echo ""
echo "DyzineFrame: ETHERGOLD_HTTP_EXTERNAL=1 via ${PROFILE_SNIPPET}"
echo "Restart DyzineFrame locally after install so it skips Bottle on 31234."
systemctl status "${HTTP_UNIT}" --no-pager || true
