#!/usr/bin/env bash
# Quick sanity check for fixture-timing kit on Pi.

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

# Windows/Dropbox copies often arrive CRLF + without +x
for sh in hat_fixture_listen.sh hat_fixture_bridge.sh install.sh br0_link_diag.sh br0_link_recover.sh disable_eth_eee.sh; do
  if [[ -f "${sh}" ]]; then
    sed -i 's/\r$//' "${sh}" 2>/dev/null || true
    chmod +x "${sh}"
  fi
done

if ! command -v python3 >/dev/null 2>&1; then
  echo "FATAL: python3 not found" >&2
  exit 1
fi

python3 -c "import serial" 2>/dev/null || {
  echo "WARN: pyserial not installed — pip install pyserial" >&2
}

for f in pog422_fixture_core.py pog422_fixture_listen.py pog422_fixture_bridge.py pog422_uart_client.py pog422_uart_capture.py fixture_link_watch.py analyze_fixture_timing.py diff_sessions.py; do
  if [[ ! -f "${ROOT}/${f}" ]]; then
    echo "SKIP missing ${f}" >&2
    continue
  fi
  python3 -m py_compile "${ROOT}/${f}"
  echo "OK ${f}"
done

python3 - <<'PY'
from pog422_fixture_core import parser_self_test

errors = parser_self_test()
if errors:
    raise SystemExit("parser_self_test failed:\n  " + "\n  ".join(errors))
print("OK parser_self_test (COMMAND_WIRE_LEN split_packets)")
PY

echo "Fixture timing kit ready."
