#!/usr/bin/env bash
# Walk down a weapon-type list N rows; capture ONLY rows that show a DATA SHEET
# (locked rows show a "Conditions to Develop" panel instead — detected by the
# brightness of the "Range Class" label box, ~5 when absent, >>20 when present).
set -u
export HOME=/sylph-home/re
OUT=/sylph-home/re/caps; mkdir -p "$OUT"
pfx=$1; n=${2:-8}
for ((i=1;i<=n;i++)); do
  vgamepad dpad down; sleep 0.20; vgamepad dpad center; sleep 3.0
  screenshot /tmp/s.png >/dev/null 2>&1
  m=$(convert /tmp/s.png -crop 150x24+730+116 +repage -colorspace Gray -format "%[fx:int(255*mean)]" info:)
  if [ "$m" -gt 45 ]; then
    convert /tmp/s.png -crop 500x420+150+180 +repage /tmp/_l.png
    convert /tmp/s.png -crop 500x200+700+100 +repage /tmp/_s.png
    convert /tmp/_l.png /tmp/_s.png -background black -append "$OUT/$pfx-r$i.png"
    echo "row $i: DATA SHEET -> $OUT/$pfx-r$i.png"
  else
    echo "row $i: locked (mean $m)"
  fi
done
