#!/usr/bin/env bash
# Check / extract Touch6 0x20 from pos2 full-Dev packets.raw
set -euo pipefail
RAW="${1:-$HOME/EtherGold/dev/data/packets.raw}"
OUT="$HOME/EtherGold/dev/lab_gameplay_templates/touch6_wager.hex"
echo "=== packets.raw ==="
ls -la "$RAW" 2>/dev/null || { echo "missing: $RAW"; exit 1; }
wc -l "$RAW"
python3 <<PY
from pathlib import Path
p = Path("$RAW")
hits = []
for i, line in enumerate(p.read_text(errors="replace").splitlines(), 1):
    s = line.strip()
    if not s or s.startswith("#") or s.startswith("{"):
        continue
    parts = [x.strip() for x in s.replace(" ", "").split(",") if x.strip()]
    if len(parts) >= 8 and parts[0].upper() == "05" and parts[1].upper() == "20":
        # game id LE at bytes 5-6: expect 07,91 for Touch6 (0x9107)
        gid = (parts[5] + parts[6]).upper() if len(parts) > 6 else "?"
        hits.append((i, ",".join(parts), gid))
print(f"0x20_frames={len(hits)}")
for i, hexline, gid in hits[:12]:
    print(f"line={i} gid_bytes={gid} head={hexline[:80]}...")
if hits:
    # Prefer game 9107 (07,91)
    pick = None
    for i, hexline, gid in hits:
        if gid == "0791":
            pick = (i, hexline)
            break
    if pick is None:
        pick = (hits[0][0], hits[0][1])
    print(f"PICK line={pick[0]}")
PY
if [[ -f "$HOME/EtherGold/dev/lab_gameplay_extract_touch6.py" ]]; then
  echo "=== extract ==="
  mkdir -p "$(dirname "$OUT")"
  python3 "$HOME/EtherGold/dev/lab_gameplay_extract_touch6.py" "$RAW" -o "$OUT" || true
  echo "=== template ==="
  cat "$OUT" 2>/dev/null || true
fi
