#!/bin/sh
set -eu

UCX_BASE_URL="https://claude.kadirr.dev"
UCX_CODEX_DIR="$HOME/.codex"
UCX_CONFIG_PATH="$UCX_CODEX_DIR/config.toml"
UCX_AUTH_PATH="$UCX_CODEX_DIR/auth.json"

cleanup() {
  if command -v stty >/dev/null 2>&1 && [ -t 0 ]; then
    stty echo 2>/dev/null || true
  fi
}

trap cleanup EXIT INT TERM

printf "\n"
printf "unlimitedcodex Codex CLI and IDE setup\n"
printf "--------------------------------------\n"
printf "This file updates only:\n"
printf "  %s\n" "$UCX_CONFIG_PATH"
printf "  %s\n" "$UCX_AUTH_PATH"
printf "\n"
printf "Existing files are backed up before changes.\n"
printf "The API key is stored locally in auth.json. Do not share that file.\n"
printf "Base URL stays: %s\n" "$UCX_BASE_URL"
printf "\n"

mkdir -p "$UCX_CODEX_DIR"

UCX_TS="$(date +%Y%m%d-%H%M%S 2>/dev/null || printf "backup")"

if [ -f "$UCX_CONFIG_PATH" ]; then
  cp "$UCX_CONFIG_PATH" "$UCX_CONFIG_PATH.bak-$UCX_TS"
fi

if [ -f "$UCX_AUTH_PATH" ]; then
  cp "$UCX_AUTH_PATH" "$UCX_AUTH_PATH.bak-$UCX_TS"
fi

printf "Paste your unlimitedcodex API key when prompted.\n"
printf "Input is hidden while typing. Press Enter after pasting.\n"
printf "\n"

if [ -t 0 ]; then
  stty -echo
  printf "unlimitedcodex API key: "
  IFS= read -r UCX_API_KEY
  stty echo
  printf "\n"
else
  printf "unlimitedcodex API key: "
  IFS= read -r UCX_API_KEY
fi

if [ -z "${UCX_API_KEY:-}" ]; then
  printf "API key cannot be empty. No changes were completed.\n"
  exit 1
fi

cat > "$UCX_CONFIG_PATH" <<EOF
model_provider = "OpenAI"
model = "gpt-5.5"
review_model = "gpt-5.5"
model_reasoning_effort = "xhigh"
disable_response_storage = true
network_access = "enabled"

[model_providers.OpenAI]
name = "OpenAI"
base_url = "$UCX_BASE_URL"
wire_api = "responses"
requires_openai_auth = true

[features]
goals = true
EOF

UCX_ESCAPED_KEY="$(printf "%s" "$UCX_API_KEY" | sed 's/\\/\\\\/g; s/"/\\"/g')"
cat > "$UCX_AUTH_PATH" <<EOF
{
  "OPENAI_API_KEY": "$UCX_ESCAPED_KEY"
}
EOF

chmod 600 "$UCX_AUTH_PATH" 2>/dev/null || true

printf "\n"
printf "Setup files were updated:\n"
printf "  %s\n" "$UCX_CONFIG_PATH"
printf "  %s\n" "$UCX_AUTH_PATH"
printf "\n"

printf "Run a tiny /v1/responses test now? Type Y and press Enter, or press Enter to skip: "
IFS= read -r UCX_RUN_TEST

case "$UCX_RUN_TEST" in
  Y|y)
    if ! command -v curl >/dev/null 2>&1; then
      printf "curl was not found, so the local files were written but the API test was skipped.\n"
    else
      printf "\n"
      printf "Running a small API test against %s/v1/responses ...\n" "$UCX_BASE_URL"
      if curl -sS -X POST "$UCX_BASE_URL/v1/responses" \
        -H "Authorization: Bearer $UCX_API_KEY" \
        -H "Content-Type: application/json" \
        -d '{"model":"gpt-5.5","input":[{"role":"user","content":[{"type":"input_text","text":"Hello from unlimitedcodex"}]}],"stream":false}' >/tmp/unlimitedcodex-codex-test.json; then
        printf "API test finished. Restart Codex CLI, Codex IDE, or the Codex app before a real task.\n"
      else
        printf "The local files were written, but the API test failed.\n"
        printf "Check the API key, package status, and network connection, then restart Codex.\n"
      fi
    fi
    ;;
  *)
    printf "API test skipped.\n"
    ;;
esac

UCX_API_KEY=""
UCX_ESCAPED_KEY=""

printf "\n"
printf "Next steps:\n"
printf "1. Close and reopen Codex CLI, Codex IDE, or the Codex app.\n"
printf "2. Start with one small prompt before a long repo task.\n"
printf "3. Keep auth.json private and never paste it into a support message.\n"
printf "\n"
