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

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

SYNC_ROOT=${SYNC_ROOT:-/data1/dongying/Mono3d/G1Q3/feishu_project}
DEFAULT_INPUT_JSON="${SYNC_ROOT}/exports/dongying_g1q3_issue_list.json"
FALLBACK_INPUT_JSON="${PROJECT_ROOT}/tools/feishu_project/dongying_g1q3_issue_list.json"
INPUT_JSON=${INPUT_JSON:-"${DEFAULT_INPUT_JSON}"}
OUTPUT_ROOT=${OUTPUT_ROOT:-/data1/dongying/Mono3d/G1Q3/feishu_project/downloaded_issue_data}
MANIFEST_PATH=${MANIFEST_PATH:-"${OUTPUT_ROOT}/download_manifest.json"}
PYTHON_BIN=${PYTHON_BIN:-/deeplearning_team/ydong/dongying/miniconda/envs/dev/bin/python}

if [[ ! -f "${INPUT_JSON}" && -f "${FALLBACK_INPUT_JSON}" ]]; then
  INPUT_JSON="${FALLBACK_INPUT_JSON}"
fi

EXTRA_ARGS=()
if [[ "${DRY_RUN:-0}" == "1" ]]; then
  EXTRA_ARGS+=("--dry-run")
fi
if [[ "${SKIP_MDI:-0}" == "1" ]]; then
  EXTRA_ARGS+=("--skip-mdi")
fi
if [[ "${SKIP_COPY:-0}" == "1" ]]; then
  EXTRA_ARGS+=("--skip-copy")
fi
if [[ "${ONLY_REDOWNLOAD_AFFECTED_CASES:-0}" == "1" ]]; then
  EXTRA_ARGS+=("--only-redownload-affected-cases")
fi

"${PYTHON_BIN}" "${PROJECT_ROOT}/tools/feishu_project/download_issue_data.py" \
  --input-json "${INPUT_JSON}" \
  --output-root "${OUTPUT_ROOT}" \
  --manifest-path "${MANIFEST_PATH}" \
  "${EXTRA_ARGS[@]}" \
  "$@"
