#!/usr/bin/env bash
# Build one submission archive: predictions for both blind sets with one fusion, the metadata file, the organisers'
# format check, zip.  Nothing is uploaded.
# Usage: runners/sub_package.sh COMBO     (eq3 -> sub_fuse.py; b3v2 -> sub_b3v2.py; any stem_ensemble combo -> sub_fuse_multi.py)
# Output: test_phase/submissions/<COMBO>/{submission_metadata.yaml, nju/, dlaad/} and test_phase/submissions/<COMBO>.zip
set -euo pipefail
cd "$(dirname "$0")/.."
combo="${1:?combo}"
py=../../.venv/bin/python
TP=$(cd ../../test_phase && pwd)
out="$TP/submissions/$combo"
rm -rf "$out"; mkdir -p "$out"
for s in nju dlaad; do
  S=$(echo "$s" | tr a-z A-Z)
  if [ "$combo" = eq3 ]; then
    GLIMPSE_THR=norm "$py" -u sub_fuse.py predict --set "TEST_$S" --out "$out/$s"
  elif [ "$combo" = b3v2 ]; then
    GLIMPSE_THR=norm "$py" -u sub_b3v2.py predict --set "TEST_$S" --out "$out/$s"
  else
    GLIMPSE_THR=norm "$py" -u sub_fuse_multi.py predict --combo "$combo" --set "TEST_$S" --out "$out/$s"
  fi
done
cat > "$out/submission_metadata.yaml" <<EOF
# $combo (experiments/decoder_lodo: sub_convert -> sub_evidence -> $combo fusion)
label_type: attended-speaker-label
# code_public: must match the online submission form (env CODE_PUBLIC, default true as chosen for submission 1047211)
code_public: ${CODE_PUBLIC:-true}
# total lookahead: evidence windows read <= 0.75 s past their end; each decision labels samples from
# t_end + 0.75 s - 1 s on, so no label uses data more than 1000 ms after its sample
decoder_lookahead_ms: 1000
EOF
( cd "$TP/example_pipeline/TOPUBLISH-test-phase-inference-pipeline-example" && \
  "$OLDPWD/$py" -s -E -m challenge.check_format --submission_dir "$out" --nju_data "$TP/nju/dataCND" --dlaad_data "$TP/dlaad/dataCND" )
( cd "$out" && rm -f "../$combo.zip" && zip -q -r "../$combo.zip" submission_metadata.yaml nju dlaad )
ls -l "$TP/submissions/$combo.zip"
echo "package $combo done"
