#!/usr/bin/env bash
# Production listen launcher — POG422 bus only, progressive CSV, no Ethernet bridge.
#
# Usage (FIXTURE_POS from ~/EtherGold/fixture-bridge.env — do not prefix unless overriding):
#   bash run_listen_prod.sh

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)"
# Flat layout: runtime files live beside this launcher.
if [[ -f "${SCRIPT_DIR}/hat_listen.sh" ]] || [[ -f "${SCRIPT_DIR}/hat_fixture_listen.sh" ]]; then
  BRIDGE_ROOT="${SCRIPT_DIR}"
else
  echo "FATAL: hat_listen.sh not found in ${SCRIPT_DIR}" >&2
  exit 1
fi
if [[ -f "${BRIDGE_ROOT}/../sync/ethergold_paths.sh" ]]; then
  # shellcheck source=../sync/ethergold_paths.sh
  source "${BRIDGE_ROOT}/../sync/ethergold_paths.sh"
elif [[ -f "${BRIDGE_ROOT}/../../sync/ethergold_paths.sh" ]]; then
  # shellcheck source=../../sync/ethergold_paths.sh
  source "${BRIDGE_ROOT}/../../sync/ethergold_paths.sh"
else
  # shellcheck source=/dev/null
  source "${HOME}/EtherGold/sync/ethergold_paths.sh"
fi
GOOD="${BRIDGE_ROOT}"
cd "${BRIDGE_ROOT}"

# CLI override wins; otherwise load fixture-bridge.env / hostname detect.
_CLI_POS="${FIXTURE_POS-}"
_CLI_LABEL="${PI_LABEL-}"
_CLI_CHANNEL="${CHANNEL-}"
if [[ -f "${ETHERGOLD_SYNC:-${HOME}/EtherGold/sync}/source_ethergold_env.sh" ]]; then
  # shellcheck source=/dev/null
  source "${ETHERGOLD_SYNC:-${HOME}/EtherGold/sync}/source_ethergold_env.sh"
elif [[ -f "${HOME}/EtherGold/sync/source_fixture_env.sh" ]]; then
  # shellcheck source=/dev/null
  source "${HOME}/EtherGold/sync/source_fixture_env.sh"
fi
if [[ -n "${_CLI_POS}" ]]; then
  FIXTURE_POS="${_CLI_POS}"
fi
if [[ -n "${_CLI_LABEL}" ]]; then
  PI_LABEL="${_CLI_LABEL}"
fi
if [[ -n "${_CLI_CHANNEL}" ]]; then
  CHANNEL="${_CLI_CHANNEL}"
fi
unset _CLI_POS _CLI_LABEL _CLI_CHANNEL

FIXTURE_POS="${FIXTURE_POS:-1}"
if ! [[ "${FIXTURE_POS}" =~ ^[1-9][0-9]?$ ]]; then
  echo "WARN: FIXTURE_POS invalid; using UART socket 1 (not a bank seat)" >&2
  FIXTURE_POS=1
fi

export FIXTURE_POS
export PI_LABEL="${PI_LABEL:-$(hostname 2>/dev/null || echo topbox)}"
export CHANNEL="${CHANNEL:-machine}"
export FIXTURE_PROD=1
export CLEAR_CAPTURE=0
export VERBOSE=0
export STATS_INTERVAL=0
export NO_PROGRESSIVE_CSV=0
export ETHERGOLD_ROOT="${ETHERGOLD_ROOT:-/home/topbox/EtherGold}"
export ETHERGOLD_PROGRESSIVES_CSV="${ETHERGOLD_PROGRESSIVES_CSV:-${ETHERGOLD_ROOT}/progressives.csv}"
export OUTPUT_DIR="${OUTPUT_DIR:-${ETHERGOLD_LOGS}}"
export LOG_DIR="${LOG_DIR:-${ETHERGOLD_LOGS}}"
mkdir -p "${OUTPUT_DIR}" "${LOG_DIR}"

export POG422_UARTD="${POG422_UARTD:-1}"
export POG422_UARTD_INJECT=0
export ETHERGOLD_CURRENT_GAME_IPC="${ETHERGOLD_CURRENT_GAME_IPC:-write}"

# install.sh is a kit compile check — do not run it on every supervisor respawn
# (it delays python long enough that the supervisor starts a second copy).
if [[ "${LISTEN_RUN_INSTALL:-0}" == "1" ]] || [[ ! -f "${GOOD}/pog422_listen.py" ]]; then
  if [[ -x "${GOOD}/install.sh" ]]; then
    bash "${GOOD}/install.sh"
  fi
fi
bash "${BRIDGE_ROOT}/ensure_tty_xrusb.sh"

echo "=== prod listen pos=${FIXTURE_POS} (POG422 UARTD, inject=0) ==="
echo "  progressives.csv → ${ETHERGOLD_PROGRESSIVES_CSV}"
cd "${GOOD}"
export OUTPUT_DIR LOG_DIR
HAT="${GOOD}/hat_listen.sh"
if [[ ! -f "${HAT}" ]]; then
  HAT="${GOOD}/hat_fixture_listen.sh"
fi
exec bash "${HAT}"
