#!/bin/bash
# vm_motionqa.sh: run the strict motionQA gate (or a review strip) on the bashatest VM instead of the Mac. A, 29 Oct.
#   bash _tools/vm_motionqa.sh <key> [more keys] [--gore 5,0] [--page 58k_x.html] [--pre 'K58.X=false'] [--th k=v] [--lead 1] [--strip 10] [--out dir]
#   bash _tools/vm_motionqa.sh --strip <key> <t0> <t1> [--n 8] [--gore 5] [--page ..] [--pre ..] [--w 480] [--out dir]
#   bash _tools/vm_motionqa.sh --selftest 1   (the gate's own self-test: _tools/motionqa_selftest.js; writes <out>/motionqa_selftest.txt)
# 1. pushes the changed top-level page files (*.js *.html *.json *.css) and _tools/*.cjs to the QA mirror /root/qa58/mirror (rsync, no delete; never the git pages clone), so
#    the run never waits for the 60 s autosync;
# 2. runs _tools/motionqa.cjs (strict faceless on, the page default) there under /root/qa58/vm_slot.sh (max 2 page Chromes on the VM,
#    fewer while its players run, parked over load 10 or under 2.5 GB free memory);
# 3. copies <key>.qa.png / .qa.json / .qa.txt (or the strip png) back to the same local paths motionqa writes: <out>/<key>.qa.* for one
#    gore, <out>/g<G>/<key>.qa.* when --gore lists several. Default out: /Volumes/Workspace/Scratch/qa.
# Prints the reports like the local tool. Exit 1 when any kill FAILS, 2 on a usage / transport error.
VM=root@bashatest.howincloud.com
FB=/Users/basha/StickBrawl-v3/v4/feedback
RP=/root/qa58/mirror   # (coord 1 Nov 16:45) a private QA mirror: NEVER the git pages clone /srv/stickbash-pages (= /root/stickbash/pages), which must stay git-clean
SSH="ssh -o BatchMode=yes -o ConnectTimeout=15 -o ServerAliveInterval=30"
OUT=/Volumes/Workspace/Scratch/qa; GORES=5; MODE=qa; KEYS=(); PASS=(); SARGS=()
if [ "$1" = "--strip" ]; then MODE=strip; shift; SARGS=("$1" "$2" "$3"); shift 3; fi
while [ $# -gt 0 ]; do
  case "$1" in
    --gore) GORES="$2"; shift 2;;
    --out) OUT="$2"; shift 2;;
    --*) PASS+=("$1" "$2"); shift 2;;
    *) KEYS+=("$1"); shift;;
  esac
done
SELF=0; for a in "${PASS[@]}"; do [ "$a" = --selftest ] && SELF=1; done   # (QA-GATE 31 Oct: --selftest 1 runs the gate's own self-test, no key)
if [ "$MODE" = qa ] && [ ${#KEYS[@]} -eq 0 ] && [ $SELF = 0 ]; then sed -n 2,12p "$0"; exit 2; fi
if [ "$MODE" = strip ] && [ -z "${SARGS[2]}" ]; then sed -n 2,12p "$0"; exit 2; fi
mkdir -p "$OUT" || exit 2
# 1. push the changed files (the Mac is the source of truth for the page files; nothing is deleted on the VM)
rsync -azm -e "$SSH" --exclude='_tools/coord/' --include='*/' --include='*.js' --include='*.cjs' --include='*.sh' --include='*.html' --include='*.json' --include='*.css' --exclude='*' "$FB/" "$VM:$RP/" || { echo "vm_motionqa: rsync to the VM failed" >&2; exit 2; }
# the pages load ../kit.js and ../kit.css: on the VM they run from /root/qa58/www/feedback (a link to the mirror) with the two kit files beside it
$SSH "$VM" "mkdir -p /root/qa58/www && { cmp -s $RP/_tools/vm_slot.sh /root/qa58/vm_slot.sh || { cp -f $RP/_tools/vm_slot.sh /root/qa58/vm_slot.sh.new && chmod +x /root/qa58/vm_slot.sh.new && mv -f /root/qa58/vm_slot.sh.new /root/qa58/vm_slot.sh; }; } && { [ -e /root/qa58/www/feedback ] || ln -s $RP /root/qa58/www/feedback; }" || exit 2
rsync -az -e "$SSH" "$FB/../kit.js" "$FB/../kit.css" "$VM:/root/qa58/www/" || exit 2
WD=/root/qa58/www/feedback; NODE="node --preserve-symlinks --preserve-symlinks-main"
RUN="r$(date +%s)$$"; RD=/root/qa58/out/$RUN
q() { [ $# -gt 0 ] && printf '%q ' "$@"; }
ENV="export PATH=/root/bakespike/node/bin:\$PATH NODE_PATH=/root/qa58/node_modules TZ=Asia/Kolkata;"
rc=0
if [ "$MODE" = strip ]; then
  G=$(echo "$GORES" | cut -d, -f1)
  $SSH "$VM" "$ENV mkdir -p $RD && cd $WD && bash /root/qa58/vm_slot.sh $NODE $WD/_tools/qa_strip.cjs $(q "${SARGS[@]}") --gore $G --out $RD $(q "${PASS[@]}")"; rc=$?
  rsync -az -e "$SSH" "$VM:$RD/" "$OUT/" && for f in $($SSH "$VM" "ls $RD"); do echo "  local: $OUT/$f"; done
else
  IFS=',' read -ra GL <<< "$GORES"
  for G in "${GL[@]}"; do
    D="$OUT"; [ ${#GL[@]} -gt 1 ] && D="$OUT/g$G"; mkdir -p "$D"
    [ ${#GL[@]} -gt 1 ] && echo "=== gore $G ==="
    $SSH "$VM" "$ENV mkdir -p $RD/g$G && cd $WD && bash /root/qa58/vm_slot.sh $NODE $WD/_tools/motionqa.cjs $(q "${KEYS[@]}") --gore $G --out $RD/g$G $(q "${PASS[@]}")" | sed "s|$RD/g$G|$D|g"; r=${PIPESTATUS[0]}
    [ $r -gt $rc ] && rc=$r
    rsync -az -e "$SSH" "$VM:$RD/g$G/" "$D/" || { echo "vm_motionqa: copy back failed" >&2; rc=2; }
  done
fi
$SSH "$VM" "python3 -c 'import shutil,sys; shutil.rmtree(sys.argv[1], ignore_errors=True)' $RD" 2>/dev/null
exit $rc
