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

trim() {
  local s="${1:-}"
  s="${s#"${s%%[![:space:]]*}"}"
  s="${s%"${s##*[![:space:]]}"}"
  printf "%s" "$s"
}

to_lower() {
  printf "%s" "$1" | tr '[:upper:]' '[:lower:]'
}

bool_from_env() {
  local raw="${1:-}"
  local name="${2:-}"
  local default="${3:-false}"

  raw="$(trim "$raw")"
  if [[ -z "$raw" ]]; then
    [[ "$default" == "true" ]]
    return $?
  fi

  local lowered
  lowered="$(to_lower "$raw")"
  case "$lowered" in
    true) return 0 ;;
    false) return 1 ;;
    *)
      echo "curl-stub: warning: ${name} must be true|false (got: ${raw}); treating as false" >&2
      return 1
      ;;
  esac
}

if ! bool_from_env "${CODEX_CURL_STUB_MODE_ENABLED:-}" "CODEX_CURL_STUB_MODE_ENABLED" "false"; then
  echo "error: curl is blocked by script regression tests" >&2
  exit 90
fi

log_dir="${CODEX_STUB_LOG_DIR:-}"
if [[ -n "$log_dir" ]]; then
  mkdir -p "$log_dir"
  printf "%s\n" "curl $*" >>"${log_dir%/}/curl.calls.txt"
fi

status="${CODEX_CURL_STUB_STATUS:-200}"
body_file="${CODEX_CURL_STUB_BODY_FILE:-}"
body="${CODEX_CURL_STUB_BODY:-}"
if [[ -z "$body" ]]; then
  body='{"ok":true}'
fi

out_file=""
write_fmt=""
url=""

args=("$@")
for ((i=0; i<${#args[@]}; i++)); do
  a="${args[$i]}"
  case "$a" in
    -o)
      out_file="${args[$((i + 1))]:-}"
      i=$((i + 1))
      ;;
    -w)
      write_fmt="${args[$((i + 1))]:-}"
      i=$((i + 1))
      ;;
    -d|--data|--data-raw|--data-binary|--data-urlencode)
      i=$((i + 1))
      ;;
    -H|--header|--max-time|--connect-timeout|--request)
      i=$((i + 1))
      ;;
    -*)
      ;;
    *)
      url="$a"
      ;;
  esac
done

content="$body"
if [[ -n "$body_file" ]]; then
  if [[ -f "$body_file" ]]; then
    content="$(cat "$body_file")"
  fi
fi

if [[ -n "$out_file" ]]; then
  if [[ "$out_file" != "/dev/null" ]]; then
    mkdir -p "$(dirname "$out_file")" 2>/dev/null || true
    printf "%s" "$content" >"$out_file"
  fi
else
  printf "%s" "$content"
fi

if [[ -n "$write_fmt" && "$write_fmt" == *"%{http_code}"* ]]; then
  printf "%s" "$status"
fi

exit 0
