#!/usr/bin/env bash
# Apply bench profile for this Pi after pull (one command).
#
# Reads FIXTURE_POS from fixture-bridge.env / hostname. Safe to re-run; most steps
# are idempotent. Restarts the bridge when the profile includes a capture mode.
#
# Usage (typical session — after pull):
#   cd ~/EtherGold/sync
#   bash apply_bench_profile.sh
#
# Override profile (default: auto from FIXTURE_POS):
#   BENCH_PROFILE=lab-keno|debug|skill-research|minimal|skip-bridge bash apply_bench_profile.sh
#
# Profiles:
#   auto            — pos1/2 debug relay, pos3 lab Keno Master, pos4 minimal
#   lab-keno        — force lab Keno synth (Master pos3 only)
#   debug           — jun11-good debug capture (no lab Keno)
#   skill-research  — pos2+3 full VERBOSE capture for skill games (Draw Poker, etc.)
#   minimal         — disable DyzineFrame autostart only; do not touch bridge
#   skip-bridge — same as minimal (alias)
#
# Bench capture always uses FIXTURE_MODE=debug → fixture-timing/data/ (PULL_LAYOUT=legacy).
# jun11-prod (--prod) is disabled; use enable_prod.sh only for field long-run / uartd soak.

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

SYNC="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=ethergold_paths.sh
source "${SYNC}/ethergold_paths.sh"
export FIXTURE_MODE="${FIXTURE_MODE:-debug}"
# shellcheck source=source_fixture_env.sh
source "${SYNC}/source_fixture_env.sh"

PROFILE="${BENCH_PROFILE:-auto}"
POS="${FIXTURE_POS:-}"

if [[ ! "${POS}" =~ ^[1-4]$ ]]; then
  echo "FATAL: apply_bench_profile is for bench FIXTURE_POS=1..4 (got '${POS}')" >&2
  echo "  Office/field topbox5: use pull only; do not run this script." >&2
  exit 1
fi

if [[ ! -f "${ETHERGOLD_ROOT}/fixture-bridge.env" ]]; then
  echo "=== seed fixture-bridge.env ==="
  bash "${SYNC}/install_fixture_bridge_env.sh"
fi

case "${PROFILE}" in
  auto)
    case "${POS}" in
      1|2) PROFILE=debug ;;
      3) PROFILE=lab-keno ;;
      4) PROFILE=minimal ;;
    esac
    ;;
  skip-bridge) PROFILE=minimal ;;
esac

echo "=== apply_bench_profile pos${POS} profile=${PROFILE} ==="

FIXTURE_POS="${POS}" bash "${SYNC}/disable_bench_prod.sh"

if [[ -x "${SYNC}/disable_dyzineframe_autostart.sh" ]]; then
  bash "${SYNC}/disable_dyzineframe_autostart.sh"
else
  echo "WARN: disable_dyzineframe_autostart.sh missing — pull first" >&2
fi

case "${PROFILE}" in
  minimal)
    echo "Bridge unchanged (minimal / pos4 hold)."
    bash "${SYNC}/show_fixture_layout.sh" 2>/dev/null || true
    exit 0
    ;;
  lab-keno)
    if [[ "${POS}" != "3" ]]; then
      echo "FATAL: lab-keno profile is pos3 Master only (pos${POS} use debug)" >&2
      exit 1
    fi
    FIXTURE_POS="${POS}" bash "${SYNC}/switch_lab_keno_jpot_master.sh"
    ;;
  debug)
    FIXTURE_POS="${POS}" bash "${SYNC}/switch_debug_bridge.sh"
    ;;
  skill-research)
    if [[ ! "${POS}" =~ ^(2|3)$ ]]; then
      echo "FATAL: skill-research is pos2 (Slave) or pos3 (Master) only" >&2
      exit 1
    fi
    FIXTURE_POS="${POS}" bash "${SYNC}/prep_bench_skill_research.sh"
    ;;
  *)
    echo "FATAL: unknown BENCH_PROFILE=${PROFILE}" >&2
    exit 1
    ;;
esac

echo ""
echo "=== verify ==="
bash "${SYNC}/show_fixture_layout.sh" 2>/dev/null || true
