#!/bin/bash
# Synthetic end-to-end self test (no laser, no scanner). ~1 min, writes ~120 MB to /tmp/mkcal_selftest.
# Usage: ./selftest.sh [python]   (laptop: ./selftest.sh /home/hank/.venvs/mk40t/bin/python)
set -e
PY=${1:-python3}
cd "$(dirname "$0")"
T=/tmp/mkcal_selftest; rm -rf "$T"; mkdir -p "$T"
STOCK=${STOCK:-ref/jcz110.cor}
$PY mk_cal_synth.py --out $T/s1.png --lens 131.5 --cor $STOCK --stock $STOCK
$PY mk_cal_analyze.py $T/s1.png --lens 131.5 --cor-in $STOCK --tag s1 --out $T | tail -25
L=$($PY -c "import json;print(json.load(open('$T/s1_result.json'))['recommend']['lens_size_mm'])")
COR=$(ls $T/s1_corrected_L*.cor | grep -v half)
$PY mk_cal_synth.py --out $T/s2.png --lens $L --cor $COR --stock $STOCK --seed 2
$PY mk_cal_analyze.py $T/s2.png --lens $L --cor-in $COR --prev $T/s1_result.json --tag s2 --out $T | tail -25
echo "SELFTEST DONE - expect s2 worst point < 0.1 mm and gamma ~1"
rm -f $T/*.png
