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

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

INPUT_JSON=${INPUT_JSON:-"${PROJECT_ROOT}/tools/feishu_project/dongying_d4q2_zhibao_issue_list.json"}
DOWNLOAD_ROOT=${DOWNLOAD_ROOT:-/data1/dongying/Mono3d/D4Q2/feishu_project/downloaded_issue_data}
OUTPUT_ROOT=${OUTPUT_ROOT:-/data1/dongying/Mono3d/D4Q2/feishu_project/decoded_issue_frame_windows_v2}
MANIFEST_PATH=${MANIFEST_PATH:-"${OUTPUT_ROOT}/decode_manifest.json"}
PYTHON_BIN=${PYTHON_BIN:-/deeplearning_team/ydong/dongying/miniconda/envs/dev/bin/python}
WINDOW_RADIUS=${WINDOW_RADIUS:-30}
JPG_QUALITY=${JPG_QUALITY:-95}
DRY_RUN=${DRY_RUN:-0}
SKIP_EXISTING=${SKIP_EXISTING:-0}
DECODE_ALL_ISSUE_IDS=${DECODE_ALL_ISSUE_IDS:-}

CMD=(
  "${PYTHON_BIN}" "${PROJECT_ROOT}/tools/feishu_project/decode_issue_frame_window.py"
  --input-json "${INPUT_JSON}"
  --download-root "${DOWNLOAD_ROOT}"
  --output-root "${OUTPUT_ROOT}"
  --manifest-path "${MANIFEST_PATH}"
  --window-radius "${WINDOW_RADIUS}"
  --jpg-quality "${JPG_QUALITY}"
)

if [[ "${DRY_RUN}" == "1" ]]; then
  CMD+=(--dry-run)
fi

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

if [[ -n "${DECODE_ALL_ISSUE_IDS}" ]]; then
  # shellcheck disable=SC2206
  ISSUE_ARR=(${DECODE_ALL_ISSUE_IDS})
  for issue_id in "${ISSUE_ARR[@]}"; do
    CMD+=(--decode-all-issue-id "${issue_id}")
  done
fi

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