#!/bin/bash
# Writes commands.log: each command published on the page, run on the test file and on one edge input, with exit codes.
# usage: bash mkcmd.sh <workdir with hoverfly.webm, qr/short-M.png and the scripts> ; PY and JSQRLIB as in test-run.sh
cd "$1" || exit 2; PY=${PY:-python3}; export JSQRLIB=${JSQRLIB:-jsqr}; L=https://koldflux.com/tools/qr-code
OV='ffmpeg -v error -ss 3 -t 5 -i hoverfly.webm -i QRFILE -filter_complex "[1:v]scale=240:240:flags=bicubic[q];[0:v][q]overlay=main_w-w-60:main_h-h-60,format=yuv420p" -an -c:v libx264 -preset medium -b:v 6000k -pix_fmt yuv420p OUTFILE'
blk() { # published, run, edge text (empty for the test run)
  echo "published: $1"; echo "run: $2"; out=$(eval "${2/#python3/$PY}" 2>&1); code=$?; echo "$out" | tail -3; echo "exit=$code"; [ -n "$3" ] && echo "edge: $3"; echo; }
rm -f out.mp4 out-noqr.mp4
O1=${OV//OUTFILE/out.mp4}; O1=${O1/QRFILE/qr/short-M.png}
OE=${OV//OUTFILE/out-edge.mp4}; OE=${OE/QRFILE/missing.png}
PUB=${OV/QRFILE/qr.png}; PUB=${PUB/OUTFILE/out.mp4}
ON='ffmpeg -v error -ss 3 -t 5 -i hoverfly.webm -an -c:v libx264 -preset medium -b:v 6000k -pix_fmt yuv420p out-noqr.mp4'
{
blk "$PUB" "$O1" ""
blk "$PUB" "$OE" "missing.png does not exist, so ffmpeg stops with an error and writes no video"
eval "$ON" >/dev/null 2>&1
blk "python3 decode-cv.py out.mp4 5 \"$L\"" "python3 decode-cv.py out.mp4 5 \"$L\"" ""
blk "python3 decode-cv.py out.mp4 5 \"$L\"" "python3 decode-cv.py out-noqr.mp4 5 \"$L\"" "out-noqr.mp4 is the same clip with no code on it, so a correct decoder reports 0 of 31"
blk "node decode-jsqr.cjs out.mp4 5 \"$L\"" "node decode-jsqr.cjs out.mp4 5 \"$L\"" ""
blk "node decode-jsqr.cjs out.mp4 5 \"$L\"" "node decode-jsqr.cjs out-noqr.mp4 5 \"$L\"" "out-noqr.mp4 has no code, so jsQR should report 0 of 31"
} > commands.log
