
#!/bin/bash -x

SCRIPTS_DIR=/usr/local/scripts
LTV_DIR=/usr/local/ltv

MIN_DATE=2025-08-03
MIN_SHA=7f9d82ab
LEVEL_SCALING=0.3162
TESTCASE="tests/codec_be_on_mr_nonselection/test_param_file.py::test_param_file_tests[ltv-SBA 3OA at 96 kbps, 48kHz in, 48kHz out, BINAURAL_ROOM_REVERB out default configuration]"

REF_ENC1=IVAS_cod_ref_1
REF_DEC1=IVAS_dec_ref_1
DUT_ENC1=IVAS_cod_ref_1
DUT_DEC1=IVAS_dec_1

REF_ENC2=IVAS_cod_ref_2
REF_DEC2=IVAS_dec_ref_2
DUT_ENC2=IVAS_cod_ref_2
DUT_DEC2=IVAS_dec_2

INV_LEVEL_SCALING=$(awk "BEGIN {print 1.0 / $LEVEL_SCALING}")

# Obtain executables from past reference
git checkout `git rev-list -1 --before="$MIN_DATE 22:00:00" ivas-float-update`
echo "ivas_float_update, min version: `git rev-parse HEAD`" > versions.txt
make clean
make -j
mv IVAS_cod IVAS_cod_ref_1
mv IVAS_dec IVAS_dec_ref_1
mv IVAS_rend IVAS_rend_ref_1

git checkout $MIN_SHA
echo "main, min version: `git rev-parse HEAD`" >> versions.txt
make clean
make -j
mv IVAS_cod IVAS_cod_1
mv IVAS_dec IVAS_dec_1
mv IVAS_rend IVAS_rend_1

# Obtain latest executables
git checkout ivas-float-update
git pull
echo "ivas-float-update, current version: `git rev-parse HEAD`" >> versions.txt
make clean
make -j
mv IVAS_cod IVAS_cod_ref_2
mv IVAS_dec IVAS_dec_ref_2
mv IVAS_rend IVAS_rend_ref_2

git checkout main
git pull
echo "main, current version: `git rev-parse HEAD`" >> versions.txt
make clean
make -j
mv IVAS_cod IVAS_cod_2
mv IVAS_dec IVAS_dec_2
mv IVAS_rend IVAS_rend_2

# Get fresh copy of scripts, tests and ci
cp -r $SCRIPTS_DIR/{scripts,tests,ci,pytest.ini} .
rm -rf tests/ref tests/dut tests/renderer/ref tests/renderer/cut
python3 ci/remove_unsupported_testcases.py scripts/config/self_test.prm scripts/config/self_test_ltv.prm

# Get LTVs
cp $LTV_DIR/* scripts/testv

# Apply level scaling
tests/scale_pcm.py ./scripts/testv/ "$LEVEL_SCALING"

# Run tests
cp IVAS_rend_ref_1 IVAS_rend_ref
cp IVAS_rend_1 IVAS_rend
python3 -m pytest "$TESTCASE" -n 1 --update_ref 1 --create_ref --param_file scripts/config/self_test_ltv.prm --use_ltv --ref_encoder_path $REF_ENC1 --ref_decoder_path $REF_DEC1 
python3 -m pytest "$TESTCASE" -n 1                --create_cut --param_file scripts/config/self_test_ltv.prm --use_ltv --dut_encoder_path $DUT_ENC1 --dut_decoder_path $DUT_DEC1 --mld --ssnr --odg --scalefac $INV_LEVEL_SCALING --junit-xml=report1.xml --html=report1.html --self-contained-html
python3 scripts/parse_xml_report.py report1.xml report1.csv

# Store results from first run
mkdir -p tests1/renderer
cp -r tests/ref tests/dut tests1
cp -r tests/renderer/ref tests1/renderer
cp -r tests/renderer/cut tests1/renderer

cp IVAS_rend_ref_2 IVAS_rend_ref
cp IVAS_rend_2 IVAS_rend
python3 -m pytest "$TESTCASE" -n 1 --update_ref 1 --create_ref --param_file scripts/config/self_test_ltv.prm --use_ltv --ref_encoder_path $REF_ENC2 --ref_decoder_path $REF_DEC2
python3 -m pytest "$TESTCASE" -n 1                --create_cut --param_file scripts/config/self_test_ltv.prm --use_ltv --dut_encoder_path $DUT_ENC2 --dut_decoder_path $DUT_DEC2 --mld --ssnr --odg --scalefac $INV_LEVEL_SCALING --junit-xml=report2.xml --html=report2.html --self-contained-html
python3 scripts/parse_xml_report.py report2.xml report2.csv

