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

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
LAUNCHER="${SCRIPT_DIR}/run_two_roi_exported_onnx_infer_case.sh"

# 用途：重跑最后 4 个未处理的 case，且不保存 visualizations。
# 前置条件：
#   1. 已停止旧的推理进程
#   2. 已按步骤 3 清理目标 case 的旧输出目录
# 区间语义：CASE_INDEX_START/END 使用 0-based 且为 [start, end)
# 当前默认区间对应 2026-04-28 的运行快照：
#   - GPU0: 第 70 到第 71 个 case -> [69, 71)
#   - GPU1: 第 72 到第 73 个 case -> [71, 73)

INPUT_DIR="${INPUT_DIR:-/data1/dongying/Mono3d/G1Q3/dataset_for_evaluation/OP_KPI_SCENE}"
OUTPUT_DIR="${OUTPUT_DIR:-/data1/dongying/Mono3d/G1Q3/model_inference/KPI/OP_KPI_SCENE/model_20260427-raw_no_edge}"
EXPORTED_MODEL="${EXPORTED_MODEL:-/deeplearning_team/ydong/dongying/projects/yolo26-3d/runs/export/train_mono3d_two_roi_20260427-raw_no_edge/merged_model.torchscript}"
PARALLEL_LOG_DIR="${PARALLEL_LOG_DIR:-${OUTPUT_DIR%/}/parallel_logs}"

GPU0_ID="${GPU0_ID:-0}"
GPU0_CASE_INDEX_START="${GPU0_CASE_INDEX_START:-69}"
GPU0_CASE_INDEX_END="${GPU0_CASE_INDEX_END:-71}"
GPU0_LOG="${GPU0_LOG:-${PARALLEL_LOG_DIR}/rerun_gpu${GPU0_ID}_${GPU0_CASE_INDEX_START}_${GPU0_CASE_INDEX_END}.log}"

GPU1_ID="${GPU1_ID:-1}"
GPU1_CASE_INDEX_START="${GPU1_CASE_INDEX_START:-71}"
GPU1_CASE_INDEX_END="${GPU1_CASE_INDEX_END:-73}"
GPU1_LOG="${GPU1_LOG:-${PARALLEL_LOG_DIR}/rerun_gpu${GPU1_ID}_${GPU1_CASE_INDEX_START}_${GPU1_CASE_INDEX_END}.log}"

mkdir -p "${PARALLEL_LOG_DIR}"

echo "Starting rerun workers with SAVE_VISUALIZATION=0"
echo "Launcher        : ${LAUNCHER}"
echo "Input dir       : ${INPUT_DIR}"
echo "Output dir      : ${OUTPUT_DIR}"
echo "Exported model  : ${EXPORTED_MODEL}"
echo "GPU${GPU0_ID} slice : [${GPU0_CASE_INDEX_START}, ${GPU0_CASE_INDEX_END}) -> ${GPU0_LOG}"
echo "GPU${GPU1_ID} slice : [${GPU1_CASE_INDEX_START}, ${GPU1_CASE_INDEX_END}) -> ${GPU1_LOG}"

nohup env \
  SAVE_VISUALIZATION=0 \
  ENABLE_TRACKING=0 \
  ENABLE_CONVERT=0 \
  DEVICE="cuda:${GPU0_ID}" \
  ATTR_DEVICE="cuda:${GPU0_ID}" \
  INPUT_DIR="${INPUT_DIR}" \
  OUTPUT_DIR="${OUTPUT_DIR}" \
  EXPORTED_MODEL="${EXPORTED_MODEL}" \
  CASE_INDEX_START="${GPU0_CASE_INDEX_START}" \
  CASE_INDEX_END="${GPU0_CASE_INDEX_END}" \
  bash "${LAUNCHER}" \
  >"${GPU0_LOG}" 2>&1 &
gpu0_pid=$!

nohup env \
  SAVE_VISUALIZATION=0 \
  ENABLE_TRACKING=0 \
  ENABLE_CONVERT=0 \
  DEVICE="cuda:${GPU1_ID}" \
  ATTR_DEVICE="cuda:${GPU1_ID}" \
  INPUT_DIR="${INPUT_DIR}" \
  OUTPUT_DIR="${OUTPUT_DIR}" \
  EXPORTED_MODEL="${EXPORTED_MODEL}" \
  CASE_INDEX_START="${GPU1_CASE_INDEX_START}" \
  CASE_INDEX_END="${GPU1_CASE_INDEX_END}" \
  bash "${LAUNCHER}" \
  >"${GPU1_LOG}" 2>&1 &
gpu1_pid=$!

echo "Started GPU${GPU0_ID} worker, PID=${gpu0_pid}"
echo "Started GPU${GPU1_ID} worker, PID=${gpu1_pid}"
echo "Use 'tail -f ${GPU0_LOG}' or 'tail -f ${GPU1_LOG}' to monitor progress."