#!/usr/bin/env bash
# Start fixture listen-only in background (for PC SSH orchestrator).
#
# Options live in ~/EtherGold/fixture-bridge.env and /etc/ethergold/ethergold.env
# — not on the command line.
#   cd ~/EtherGold/dev && bash start_listen_background.sh
#
# Passive POG422 witness — no ETH bridge. Listen launchers force inject=0.

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

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

for _sync in \
  "${ETHERGOLD_SYNC:-}" \
  "${ROOT}/../sync" \
  "/home/topbox/EtherGold/sync"; do
  if [[ -f "${_sync}/source_ethergold_env.sh" ]]; then
    # shellcheck source=/dev/null
    source "${_sync}/source_ethergold_env.sh"
    break
  fi
done
unset _sync

FIXTURE_POS="${FIXTURE_POS:-}"
PID_FILE="${ROOT}/.listen.pid"
LOG="${ROOT}/logs/orchestrator-listen.log"

if [[ -z "${FIXTURE_POS}" ]] || ! [[ "${FIXTURE_POS}" =~ ^[1-9][0-9]?$ ]]; then
  echo "FATAL: FIXTURE_POS=1..99 required in ~/EtherGold/fixture-bridge.env (or /etc/ethergold/ethergold.env)" >&2
  exit 1
fi

mkdir -p "${ROOT}/logs"

CLEAR_CAPTURE="${CLEAR_CAPTURE:-1}"
POG422_UARTD="${POG422_UARTD:-0}"

if [[ "${CLEAR_CAPTURE}" == "1" ]]; then
  : > "${LOG}"
fi

if [[ -f "${PID_FILE}" ]]; then
  old_pid="$(cat "${PID_FILE}" 2>/dev/null || true)"
  if [[ -n "${old_pid}" ]] && kill -0 "${old_pid}" 2>/dev/null; then
    echo "Listen already running (pid ${old_pid}). Stop first: bash stop_fixture_session.sh" >&2
    exit 1
  fi
  rm -f "${PID_FILE}"
fi

if pgrep -f 'pog422_listen\.py|pog422_fixture_listen|hat_listen\.sh' >/dev/null 2>&1; then
  echo "FATAL: listen already running (holds Pi HS BCM9)." >&2
  pgrep -af 'pog422_listen\.py|hat_listen\.sh|ethergold_listen_supervisor' >&2 || true
  if systemctl is-active --quiet ethergold-collector.service 2>/dev/null; then
    echo "  systemd ethergold-collector owns GPIO HS. Stop it first:" >&2
    echo "    sudo systemctl stop ethergold-collector.service" >&2
  else
    echo "  Collector is already stopped. Leftover children still hold BCM9" >&2
    echo "  (KillMode=process used to leave listen up with uartd persist)." >&2
    echo "  Kill listen only — do not pkill pog422_uartd:" >&2
    echo "    pkill -TERM -f pog422_listen.py" >&2
    echo "    pkill -TERM -f hat_listen.sh" >&2
    echo "  Then run start_listen_background.sh again." >&2
  fi
  exit 1
fi

export FIXTURE_POS
export CLEAR_CAPTURE="${CLEAR_CAPTURE:-1}"
export POG422_UARTD="${POG422_UARTD:-0}"
export POG422_UARTD_INJECT=0
export VERBOSE="${VERBOSE:-0}"

# run_listen.sh was removed; env + wipe + ~/EtherGold/dev/data live here.
nohup bash "${ROOT}/run_listen_jun11_good.sh" >> "${LOG}" 2>&1 &
listen_pid=$!
echo "${listen_pid}" > "${PID_FILE}"

sleep 5
if ! kill -0 "${listen_pid}" 2>/dev/null; then
  echo "FATAL: listen exited early — tail ${LOG}" >&2
  tail -n 40 "${LOG}" >&2 || true
  rm -f "${PID_FILE}"
  exit 1
fi

echo "pos${FIXTURE_POS} listen started pid=${listen_pid} log=${LOG}"
