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

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

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=""
CN_PROXY=0

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

Optional:
  --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
  cn | --cn | --proxy-cn
EOF
}

while [[ $# -gt 0 ]]; do
  case "$1" in
    --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
      ;;
    cn|--cn|--proxy-cn)
      CN_PROXY=1
      shift
      ;;
    -h|--help)
      usage
      exit 0
      ;;
    *)
      echo "unknown argument: $1" >&2
      usage >&2
      exit 2
      ;;
  esac
done

mkdir -p "${STATE_DIR}"

if [[ -z "${HOOK_SPOOL_PATH}" ]]; then
  HOOK_SPOOL_PATH="${STATE_DIR}/telegram_hook_spool.jsonl"
fi

PORTABLE_CONFIG_PATH="${STATE_DIR}/portable_config.json"

python3 - <<PY
import json
from pathlib import Path

payload = {
    "telegram_bot_token": ${TELEGRAM_BOT_TOKEN@Q},
    "telegram_chat_id": ${TELEGRAM_CHAT_ID@Q},
    "hook_spool_path": ${HOOK_SPOOL_PATH@Q},
}
path = Path(${PORTABLE_CONFIG_PATH@Q}).expanduser()
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(json.dumps(payload, indent=2, ensure_ascii=False) + "\n", encoding="utf-8")
print(path)
PY

python3 "${DXCLI_DIR}/scripts/install_codex_hook.py" \
  --codex-dir "${CODEX_DIR}" \
  --hook-script "${DXCLI_DIR}/dxcli_codex_hook.py" \
  --portable-config-path "${PORTABLE_CONFIG_PATH}" \
  --hook-spool-path "${HOOK_SPOOL_PATH}"

chmod +x "${DXCLI_DIR}/dxcli.py" "${DXCLI_DIR}/dxcli_codex_hook.py"
mkdir -p "${BIN_DIR}"
write_dxcli_wrapper() {
  local name="$1"
  local proxy_exports=""
  if [[ ${CN_PROXY} -eq 1 ]]; then
    proxy_exports=$(cat <<'EOF_PROXY'
export DXCLI_CN_PROXY_URL="${DXCLI_CN_PROXY_URL:-http://127.0.0.1:7890}"
export http_proxy="${DXCLI_CN_PROXY_URL}"
export https_proxy="${DXCLI_CN_PROXY_URL}"
export all_proxy="${DXCLI_CN_PROXY_URL}"
export HTTP_PROXY="${DXCLI_CN_PROXY_URL}"
export HTTPS_PROXY="${DXCLI_CN_PROXY_URL}"
export ALL_PROXY="${DXCLI_CN_PROXY_URL}"
export no_proxy="localhost,127.0.0.1,::1"
export NO_PROXY="${no_proxy}"
EOF_PROXY
)
  fi
  cat > "${BIN_DIR}/${name}" <<EOF
#!/usr/bin/env bash
set -euo pipefail
export DXCLI_INVOKED_NAME=${name@Q}
export DXCLI_STATE_DIR=${STATE_DIR@Q}
export DXCLI_PORTABLE_CONFIG_PATH=${PORTABLE_CONFIG_PATH@Q}
export DXCLI_HOOK_SPOOL_PATH=${HOOK_SPOOL_PATH@Q}
${proxy_exports}
exec python3 "${DXCLI_DIR}/dxcli.py" "\$@"
EOF
  chmod 755 "${BIN_DIR}/${name}"
}

write_dxcli_wrapper dx
write_dxcli_wrapper dxs
write_dxcli_wrapper dxsa
write_dxcli_wrapper dxsd
write_dxcli_wrapper dxsda
write_dxcli_wrapper dxa
write_dxcli_wrapper dxaa

echo "dxcli portable setup complete"
