#!/bin/bash # Review before running. Requires Bash and curl; no Python. ( set +x set -eu TOOL='codex-cli' ROOT='https://cmitoken.xmu.edu.my' set +u doctor=0 dry=0 model='' while [ "$#" -gt 0 ]; do case "$1" in --doctor) doctor=1; shift ;; --dry-run) dry=1; shift ;; --model) [ "$#" -ge 2 ] || { echo 'Missing model ID' >&2; exit 1; }; model="$2"; shift 2 ;; *) echo 'Usage: bash setup.sh [--doctor | --dry-run] [--model MODEL_ID]' >&2; exit 1 ;; esac done if [ "$dry" = 1 ]; then printf 'Tool: %s\nGateway: %s\nNo network, keys or files accessed. Codex: diagnostics only.\n' "$TOOL" "$ROOT" exit 0 fi command -v curl >/dev/null || { echo 'curl is required.' >&2; exit 1; } key="$OGROUTER_API_KEY" if [ -z "$key" ]; then printf 'OGRouter API key (hidden): ' >&2 IFS= read -rs key printf '\n' >&2 fi case "$key" in ''|*[!A-Za-z0-9._-]*) echo 'Invalid key format.' >&2; exit 1 ;; esac # Send the header via stdin, never argv. Do not follow redirects or print bodies. if ! status=$(printf 'header = "Authorization: Bearer %s"\n' "$key" | curl -q --config - --silent --max-time 20 --proto '=https' --max-redirs 0 \ --output /dev/null --write-out '%{http_code}' "$ROOT/v1/models"); then echo 'Gateway check failed. Check your network; no inference was submitted.' >&2; exit 1 fi if [ "$status" != 200 ]; then printf 'Gateway check failed: HTTP %s. No inference submitted.\n' "$status" >&2; exit 1 fi echo 'Gateway model-list request succeeded. This does not verify inference or tool compatibility.' if [ "$doctor" = 1 ] || [ "$TOOL" = codex-cli ]; then [ "$TOOL" != codex-cli ] || echo 'Responses support pending. No Codex settings changed or activated.' exit 0 fi if [ -z "$model" ]; then printf 'Exact model ID from the OGRouter catalog: '; IFS= read -r model; fi model_pattern='^([A-Za-z0-9_./:+-]|\[|\]){1,160}$' if ! [[ "$model" =~ $model_pattern ]]; then echo 'Invalid model ID. Use the exact catalog ID, without spaces or shell characters.' >&2; exit 1 fi if [ "$TOOL" = claude-code ]; then client=claude; else client=dsh; fi command -v "$client" >/dev/null || { echo 'Install the official client first.' >&2; exit 1; } if [ "$TOOL" = claude-code ]; then for flag in "$CLAUDE_CODE_USE_BEDROCK" "$CLAUDE_CODE_USE_VERTEX" "$CLAUDE_CODE_USE_FOUNDRY"; do case "$flag" in ''|0|false) ;; *) echo 'Cloud-provider mode is active. Use a clean terminal.' >&2; exit 1 ;; esac done fi echo 'Starting the client may incur charges for tasks you submit. Model permission must be verified in a short task.' printf 'Start client? [y/N] '; IFS= read -r answer [ "$answer" = y ] || exit 0 if [ "$TOOL" = claude-code ]; then unset ANTHROPIC_API_KEY export ANTHROPIC_BASE_URL="$ROOT" ANTHROPIC_AUTH_TOKEN="$key" ANTHROPIC_MODEL="$model" export ANTHROPIC_DEFAULT_OPUS_MODEL="$model" ANTHROPIC_DEFAULT_SONNET_MODEL="$model" export ANTHROPIC_DEFAULT_HAIKU_MODEL="$model" CLAUDE_CODE_SUBAGENT_MODEL="$model" unset key echo 'Restore: exit this client, then run claude normally. Original settings are unchanged.' claude else umask 077 dsh_home="$HOME/.ogrouter/deepseek-harness" mkdir -p "$dsh_home" patch=$(mktemp "$dsh_home/ogrouter-XXXXXX") trap 'rm -f -- "$patch"' EXIT printf '%s\n' '- id: llm-pi-ai' ' config:' ' providers:' ' ogrouter:' \ ' api: openai-completions' ' apiKeyEnv: OGROUTER_API_KEY' \ " baseURL: \"$ROOT/v1\"" ' models:' " - id: \"$model\"" > "$patch" export OGROUTER_API_KEY="$key" DSH_HOME="$dsh_home" DSH_TELEMETRY_MODE=DISABLED unset key echo 'Select ogrouter / your model in the picker. Review Settings > General > Upload Session Log and disable it.' echo 'Restore: exit and run dsh normally. Original settings are unchanged.' dsh web --patch "$patch" fi )