#!/bin/bash
# packs + checks + cards every finished vehicle job (rc.txt present); loops until the queue is done
cd ~/forge-build/forge-assets
while true; do
  for s in $(cat /tmp/veh-20261010/order.txt); do
    J=jobs/$s-v1
    [ -f $J/rc.txt ] || continue
    [ -f $J/.finished ] && continue
    if [ ! -f $J/$s.glb ]; then printf '%s NO GLB rc=%s\n' $s "$(cat $J/rc.txt)" >> /tmp/veh-20261010/finish.log; touch $J/.finished; continue; fi
    # a failed pack must stop here: 15 Oct 2026 a cleanup had removed the pack tools' node_modules and the loop carded the
    # unpacked 7 MB files (fix: npm ci in forge-game-engine/tools/pack-glb)
    if ! tools/vehicles/pack.sh $J >> /tmp/veh-20261010/finish.log 2>&1; then printf '%s PACK FAILED\n' $s >> /tmp/veh-20261010/finish.log; touch $J/.finished; continue; fi
    python3 tools/vehicles/check_vehicle.py $J > $J/check.json 2>&1; ok=$?
    if [ $ok = 0 ]; then python3 tools/vehicles/import_vehicle.py $J >> /tmp/veh-20261010/finish.log 2>&1 && python3 tools/vehicles/manifest.py >> /tmp/veh-20261010/finish.log 2>&1; fi
    printf '%s check=%s %s\n' $s $ok "$(date -u +%H:%M)" >> /tmp/veh-20261010/finish.log
    touch $J/.finished
  done
  grep -q 'ALL DONE' /tmp/veh-20261010/queue.log 2>/dev/null && [ $(ls jobs/*-v1/.finished 2>/dev/null | wc -l) -ge 20 ] && break
  sleep 30
done
printf 'FINISHER DONE %s\n' "$(date -u +%H:%M)" >> /tmp/veh-20261010/finish.log
