
#!/bin/bash -x

SCRIPTS_DIR=/usr/local/scripts
REF_DIR=/usr/local/scripts # float main should be the same as the scripts folder from now on. Note that the user needs write access to this folder, so it could be a local clone for reproducing test cases
LTV_DIR=/usr/local/ltv

MIN_DATE=2026-01-23
MIN_SHA=fc410247
LEVEL_SCALING=3.162
TESTCASE="tests/codec_be_on_mr_nonselection/test_param_file.py::test_param_file_tests[ltv-1 ISM with metadata at 13.2 kbps, 48 kHz in, 48 kHz out, EXT out]"

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 reference executables from past reference
working_dir=$(pwd)
pushd $REF_DIR
git checkout `git rev-list -1 --before="$MIN_DATE 22:00:00" main`
echo "ivas-codec main, min version: `git rev-parse HEAD`" > $working_dir/versions.txt
make clean
make -j
mv IVAS_cod $working_dir/IVAS_cod_ref_1
mv IVAS_dec $working_dir/IVAS_dec_ref_1
mv IVAS_rend $working_dir/IVAS_rend_ref_1

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

popd # Go back to ivas-basop folder in $working_dir

# Obtain cut executables from past reference
git checkout $MIN_SHA
echo "BASOP 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

git checkout main
git pull
echo "BASOP 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

# 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

