#!/usr/bin/env bash
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
MODEL_INFERENCE_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)"
PROJECT_ROOT="$(cd "${MODEL_INFERENCE_DIR}/../.." && pwd)"

# Launcher for PDCL clip-list batch inference.

PYTHON_BIN="${PYTHON_BIN:-/deeplearning_team/ydong/dongying/miniconda/envs/dev/bin/python}"
CLIP_LIST_FILE="${CLIP_LIST_FILE:-${MODEL_INFERENCE_DIR}/examples/clip_lists/clips_aeb.txt}"
EXPORT_ROOT="${EXPORT_ROOT:-/data1/dongying/Mono3d/G1Q3/model_inference/test/clips_aeb_export}"
OUTPUT_PREFIX="${OUTPUT_PREFIX:-clip_export}"
CAMERA_TOPIC="${CAMERA_TOPIC:-camera4}"
MAX_FRAMES_PER_CLIP="${MAX_FRAMES_PER_CLIP:-0}"
LIMIT_CLIPS="${LIMIT_CLIPS:-0}"
SKIP_DONE="${SKIP_DONE:-1}"
EXPORTED_MODEL="${EXPORTED_MODEL:-${PROJECT_ROOT}/runs/export/train_mono3d_two_roi_20260413-raw/merged_model.torchscript}"
OUTPUT_DIR="${OUTPUT_DIR:-/data1/dongying/Mono3d/G1Q3/model_inference/test/clips_aeb_no_cls}"
ENABLE_ATTR="${ENABLE_ATTR:-0}"
ENABLE_CROSS_CLASS_MERGE_PRIOR="${ENABLE_CROSS_CLASS_MERGE_PRIOR:-1}"
ENABLE_VRU_MERGE="${ENABLE_VRU_MERGE:-1}"
VIS_CLASSES="${VIS_CLASSES:-9 10 11 12}"
VIS_CLASS_NAMES="${VIS_CLASS_NAMES:-}"
EXTRA_ARGS="${EXTRA_ARGS:-}"

CMD=(
  "${PYTHON_BIN}" "${MODEL_INFERENCE_DIR}/core/run_two_roi_exported_onnx_infer.py"
  --clip-list-file "${CLIP_LIST_FILE}"
  --export-root "${EXPORT_ROOT}"
  --output-prefix "${OUTPUT_PREFIX}"
  --camera-topic "${CAMERA_TOPIC}"
  --max-frames-per-clip "${MAX_FRAMES_PER_CLIP}"
  --exported-model "${EXPORTED_MODEL}"
  --output-dir "${OUTPUT_DIR}"
)

if [[ "${ENABLE_ATTR}" == "1" ]]; then
  CMD+=(--enable-attr)
fi

if [[ "${ENABLE_CROSS_CLASS_MERGE_PRIOR}" == "1" ]]; then
  CMD+=(--enable-cross-class-merge-prior)
fi

if [[ "${ENABLE_VRU_MERGE}" == "1" ]]; then
  CMD+=(--enable-vru-merge)
fi

if [[ -n "${VIS_CLASSES}" ]]; then
  # shellcheck disable=SC2206
  VIS_CLASSES_ARR=(${VIS_CLASSES})
  CMD+=(--vis-classes "${VIS_CLASSES_ARR[@]}")
fi

if [[ -n "${VIS_CLASS_NAMES}" ]]; then
  # shellcheck disable=SC2206
  VIS_CLASS_NAMES_ARR=(${VIS_CLASS_NAMES})
  CMD+=(--vis-class-names "${VIS_CLASS_NAMES_ARR[@]}")
fi

if [[ "${LIMIT_CLIPS}" != "0" ]]; then
  CMD+=(--limit-clips "${LIMIT_CLIPS}")
fi

if [[ "${SKIP_DONE}" == "1" ]]; then
  CMD+=(--skip-done)
fi

if [[ -n "${EXTRA_ARGS}" ]]; then
  # shellcheck disable=SC2206
  EXTRA_ARR=(${EXTRA_ARGS})
  CMD+=("${EXTRA_ARR[@]}")
fi

CMD+=("$@")
"${CMD[@]}"
