#!/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 Excel/CSV column extraction.
#
# Usage:
#   bash tools/model_inference/scripts/run_extract_excel_column.sh
#
# Common env overrides:
#   INPUT_FILE, COLUMN_NAME, SHEET_NAME, OUTPUT_FILE, JSON_FILE,
#   DEDUPE, LIST_COLUMNS, EXTRA_ARGS

PYTHON_BIN="${PYTHON_BIN:-/deeplearning_team/ydong/dongying/miniconda/envs/dev/bin/python}"
INPUT_FILE="${INPUT_FILE:-${MODEL_INFERENCE_DIR}/examples/cncap/G1M3_AFS1616_CNCAP-2024_11月_0306.xlsx}"
COLUMN_NAME="${COLUMN_NAME:-原始数据地址}"
SHEET_NAME="${SHEET_NAME:-}"
OUTPUT_FILE="${OUTPUT_FILE:-}"
JSON_FILE="${JSON_FILE:-}"
DEDUPE="${DEDUPE:-0}"
LIST_COLUMNS="${LIST_COLUMNS:-0}"
EXTRA_ARGS="${EXTRA_ARGS:-}"

CMD=(
  "${PYTHON_BIN}" "${MODEL_INFERENCE_DIR}/data_tools/extract_excel_column.py"
  --input-file "${INPUT_FILE}"
  --column-name "${COLUMN_NAME}"
)

if [[ -n "${SHEET_NAME}" ]]; then
  CMD+=(--sheet-name "${SHEET_NAME}")
fi

if [[ -n "${OUTPUT_FILE}" ]]; then
  CMD+=(--output-file "${OUTPUT_FILE}")
fi

if [[ -n "${JSON_FILE}" ]]; then
  CMD+=(--json-file "${JSON_FILE}")
fi

if [[ "${DEDUPE}" == "1" ]]; then
  CMD+=(--dedupe)
fi

if [[ "${LIST_COLUMNS}" == "1" ]]; then
  CMD+=(--list-columns)
fi

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

CMD+=("$@")

"${CMD[@]}"
