#!/usr/bin/env bash
# One-shot runner: RPC node health suite
#
# Runs:
# - Proxmox storage restriction audit
# - Besu heap sizing audit
# - Idempotent remediation script (dry-run by default)
# - Full RPC JSON-RPC test matrix against all nodes
#
# Usage:
#   PROXMOX_HOST=192.168.11.10 ./scripts/run-rpc-node-suite.sh
#   PROXMOX_HOST=192.168.11.10 ./scripts/run-rpc-node-suite.sh --apply --restart-besu
#
# Notes:
# - Remediation is dry-run unless you pass --apply.
# - --restart-besu only works with --apply.

set -euo pipefail

PROXMOX_HOST="${PROXMOX_HOST:-192.168.11.10}"

APPLY_ARGS=()
while [[ $# -gt 0 ]]; do
  case "$1" in
    --apply) APPLY_ARGS+=("--apply"); shift ;;
    --restart-besu) APPLY_ARGS+=("--restart-besu"); shift ;;
    -h|--help)
      sed -n '1,80p' "$0" | sed 's/^# \{0,1\}//'
      exit 0
      ;;
    *) echo "Unknown arg: $1" >&2; exit 2 ;;
  esac
done

echo "=== RPC Node Health Suite ==="
echo "PROXMOX_HOST=${PROXMOX_HOST}"
echo "Remediation: ${APPLY_ARGS[*]:-(dry-run)}"
echo

echo "== 1) Storage audit =="
PROXMOX_HOST="${PROXMOX_HOST}" ./scripts/audit-proxmox-rpc-storage.sh
echo

echo "== 2) Besu heap audit =="
PROXMOX_HOST="${PROXMOX_HOST}" ./scripts/audit-proxmox-rpc-besu-heap.sh
echo

echo "== 3) Remediation (idempotent) =="
PROXMOX_HOST="${PROXMOX_HOST}" ./scripts/remediate-proxmox-rpc-stability.sh "${APPLY_ARGS[@]}"
echo

echo "== 4) Full RPC matrix test (writes reports/*) =="
python3 ./scripts/test-all-rpc-nodes.py --timeout 4 --threads 12
echo

echo "Done."

