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

SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
ARCHIVE_PATH=""
INSTALL_DIR="/root/ls/dxcli-portable"
STATE_DIR="${HOME}/.dxcli-portable"
CODEX_DIR="${HOME}/.codex"
BIN_DIR="${HOME}/.local/bin"
TELEGRAM_BOT_TOKEN="8710936223:AAEwrYdMzcyjP1_JK5sM39nsjd7YVIBQSz0"
TELEGRAM_CHAT_ID="-1003965113006"
HOOK_SPOOL_PATH=""
KEEP_EXISTING=0
CN_PROXY=0

usage() {
  cat <<'EOF'
Usage:
  install_dxcli_portable_bundle.sh

By default this installer looks for a dxcli-portable*.tar.gz archive in the
same directory, removes the target install dir, extracts the archive there,
and runs scripts/setup_dxcli_portable.sh.

Optional:
  --archive /path/to/dxcli-portable-<version>.tar.gz
  --install-dir /root/ls/dxcli-portable
  --state-dir ~/.dxcli-portable
  --codex-dir ~/.codex
  --bin-dir ~/.local/bin
  --telegram-bot-token token
  --telegram-chat-id -100...
  --hook-spool-path ~/.dxcli-portable/telegram_hook_spool.jsonl
  --keep-existing
  cn | --cn | --proxy-cn
EOF
}

while [[ $# -gt 0 ]]; do
  case "$1" in
    --archive)
      ARCHIVE_PATH="$2"
      shift 2
      ;;
    --install-dir)
      INSTALL_DIR="$2"
      shift 2
      ;;
    --state-dir)
      STATE_DIR="$2"
      shift 2
      ;;
    --codex-dir)
      CODEX_DIR="$2"
      shift 2
      ;;
    --bin-dir)
      BIN_DIR="$2"
      shift 2
      ;;
    --telegram-bot-token)
      TELEGRAM_BOT_TOKEN="$2"
      shift 2
      ;;
    --telegram-chat-id)
      TELEGRAM_CHAT_ID="$2"
      shift 2
      ;;
    --hook-spool-path)
      HOOK_SPOOL_PATH="$2"
      shift 2
      ;;
    --keep-existing)
      KEEP_EXISTING=1
      shift
      ;;
    cn|--cn|--proxy-cn)
      CN_PROXY=1
      shift
      ;;
    -h|--help)
      usage
      exit 0
      ;;
    *)
      echo "unknown argument: $1" >&2
      usage >&2
      exit 2
      ;;
  esac
done

if [[ -z "${ARCHIVE_PATH}" ]]; then
  shopt -s nullglob
  archives=("${SCRIPT_DIR}"/dxcli-portable*.tar.gz)
  shopt -u nullglob
  if [[ ${#archives[@]} -eq 0 ]]; then
    echo "no dxcli-portable*.tar.gz archive found next to installer" >&2
    exit 1
  fi
  if [[ ${#archives[@]} -gt 1 ]]; then
    echo "multiple dxcli-portable archives found; pass --archive explicitly" >&2
    printf '  %s\n' "${archives[@]}" >&2
    exit 1
  fi
  ARCHIVE_PATH="${archives[0]}"
fi

if [[ ! -f "${ARCHIVE_PATH}" ]]; then
  echo "archive not found: ${ARCHIVE_PATH}" >&2
  exit 1
fi

TMP_DIR="$(mktemp -d)"
cleanup() {
  rm -rf "${TMP_DIR}"
}
trap cleanup EXIT

tar -xzf "${ARCHIVE_PATH}" -C "${TMP_DIR}"
if [[ ! -d "${TMP_DIR}/dxcli-portable" ]]; then
  echo "archive does not contain dxcli-portable/" >&2
  exit 1
fi

mkdir -p "$(dirname -- "${INSTALL_DIR}")"
if [[ ${KEEP_EXISTING} -eq 0 ]]; then
  rm -rf "${INSTALL_DIR}"
else
  mkdir -p "${INSTALL_DIR}"
fi
cp -a "${TMP_DIR}/dxcli-portable/." "${INSTALL_DIR}/"

setup_args=(
  --state-dir "${STATE_DIR}"
  --codex-dir "${CODEX_DIR}"
  --bin-dir "${BIN_DIR}"
  --telegram-bot-token "${TELEGRAM_BOT_TOKEN}"
  --telegram-chat-id "${TELEGRAM_CHAT_ID}"
)
if [[ -n "${HOOK_SPOOL_PATH}" ]]; then
  setup_args+=(--hook-spool-path "${HOOK_SPOOL_PATH}")
fi
if [[ ${CN_PROXY} -eq 1 ]]; then
  setup_args+=(cn)
fi

bash "${INSTALL_DIR}/scripts/setup_dxcli_portable.sh" "${setup_args[@]}"
echo "dxcli portable bundle installed at ${INSTALL_DIR}"
