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

# Configurable via environment
VERSION="${COSINE_VERSION:-latest}"
INSTALL_DIR="${COSINE_INSTALL_DIR:-}"
API_BASE_URL="${COSINE_API_BASE_URL:-https://api.cosine.sh}"
MODIFY_PATH=1
FORCE_INSTALL=0
REPO_OWNER="CosineAI"
CLI_REPO_NAME="cli2"
CLI_BINARY_NAME="cos"

case "${COSINE_NO_MODIFY_PATH:-}" in
1 | true | TRUE | yes | YES) MODIFY_PATH=0 ;;
esac

case "${COSINE_MODIFY_PATH:-}" in
0 | false | FALSE | no | NO) MODIFY_PATH=0 ;;
1 | true | TRUE | yes | YES) MODIFY_PATH=1 ;;
esac

case "${COSINE_FORCE_INSTALL:-}" in
1 | true | TRUE | yes | YES) FORCE_INSTALL=1 ;;
esac

print_status() { printf "%s\n" "$1" >&2; }
print_success() { printf "[SUCCESS] %s\n" "$1" >&2; }
print_warning() {
  if [ -z "${NO_COLOR:-}" ]; then
    orange=$(printf '\033[38;5;208m')
    reset=$(printf '\033[0m')
    printf "%s[WARNING] %s%s\n" "$orange" "$1" "$reset" >&2
  else
    printf "[WARNING] %s\n" "$1" >&2
  fi
}

print_error() {
  if [ -z "${NO_COLOR:-}" ]; then
    red=$(printf '\033[0;31m')
    reset=$(printf '\033[0m')
    printf "%s[ERROR] %s%s\n" "$red" "$1" "$reset" >&2
  else
    printf "[ERROR] %s\n" "$1" >&2
  fi
}

err() { print_error "$*"; }

show_logo() {
  printf "\n" >&2

  if [ -z "${NO_COLOR:-}" ]; then
    c1=$(printf '\033[38;5;213m')
    c2=$(printf '\033[38;5;177m')
    c3=$(printf '\033[38;5;141m')
    c4=$(printf '\033[38;5;135m')
    c5=$(printf '\033[38;5;129m')
    reset=$(printf '\033[0m')

    printf "%s   ✬✬✬✫✫✩✧✦☆✦%s\n" "$c1" "$reset" >&2
    printf "%s ✬✬✬✬✬✬ ✪✧☆☆✦%s\n" "$c1" "$reset" >&2
    printf "%s✬✬✬✬✬✬  ✬✧✦✦✦%s%s     ✬✬✬✬✬✬✬   ✬✬✬✬✬✬✬   ✬✬✬✬✬✬✬  ✬✬  ✬✬✬    ✬✬  ✬✬✬✬✬✬✬✬%s\n" "$c1" "$reset" "$c2" "$reset" >&2
    printf "%s✩✪✬✬      ✬✪✪%s%s    ✬✬     ✬✬ ✬✬     ✬✬ ✬✬        ✬✬  ✬✬✬✬✬  ✬✬  ✬✬%s\n" "$c2" "$reset" "$c3" "$reset" >&2
    printf "%s                 ✬✬%s%s        ✬✬     ✬✬  ✬✬✬✬✬✬✬  ✬✬  ✬✬ ✬✬✬ ✬✬  ✬✬✬✬✬✬✬✬%s\n" "$c2" "$reset" "$c3" "$reset" >&2
    printf "%s✩✪✩✩      ✪✪✪%s%s    ✬✬     ✬✬ ✬✬     ✬✬ ✬✬     ✬✬ ✬✬  ✬✬   ✬✬✬✬  ✬✬%s\n" "$c4" "$reset" "$c4" "$reset" >&2
    printf "%s✪✪✪✪✩✩  ✫✫✫✫✫%s%s     ✬✬✬✬✬✬✬   ✬✬✬✬✬✬✬   ✬✬✬✬✬✬✬  ✬✬  ✬✬    ✬✬✬  ✬✬✬✬✬✬✬✬%s\n" "$c4" "$reset" "$c5" "$reset" >&2
    printf "%s ✬✬✬✬✬✬ ✬✪✪✫✫%s\n" "$c5" "$reset" >&2
    printf "%s   ✬✬✬✫✪✩✩✪✪✫%s\n" "$c5" "$reset" >&2
  else
    printf "   ✬✬✬✫✫✩✧✦☆✦\n" >&2
    printf " ✬✬✬✬✬✬ ✪✧☆☆✦\n" >&2
    printf "✬✬✬✬✬✬  ✬✧✦✦✦     ✬✬✬✬✬✬✬   ✬✬✬✬✬✬✬   ✬✬✬✬✬✬✬  ✬✬  ✬✬✬    ✬✬  ✬✬✬✬✬✬✬✬\n" >&2
    printf "✩✪✬✬      ✬✪✪    ✬✬     ✬✬ ✬✬     ✬✬ ✬✬        ✬✬  ✬✬✬✬✬  ✬✬  ✬✬\n" >&2
    printf "                 ✬✬        ✬✬     ✬✬  ✬✬✬✬✬✬✬  ✬✬  ✬✬ ✬✬✬ ✬✬  ✬✬✬✬✬✬✬✬\n" >&2
    printf "✩✪✩✩      ✪✪✪    ✬✬     ✬✬ ✬✬     ✬✬ ✬✬     ✬✬ ✬✬  ✬✬   ✬✬✬✬  ✬✬\n" >&2
    printf "✪✪✪✪✩✩  ✫✫✫✫✫     ✬✬✬✬✬✬✬   ✬✬✬✬✬✬✬   ✬✬✬✬✬✬✬  ✬✬  ✬✬    ✬✬✬  ✬✬✬✬✬✬✬✬\n" >&2
    printf " ✬✬✬✬✬✬ ✬✪✪✫✫\n" >&2
    printf "   ✬✬✬✫✪✩✩✪✪✫\n" >&2
  fi

  printf "\n" >&2
}

need_cmd() { command -v "$1" >/dev/null 2>&1 || {
  err "Required command not found: $1"
  return 1
}; }

curl_with_retries() {
  curl -fsSL --retry 3 --retry-delay 1 "$@"
}

python_cmd() {
  if command -v python3 >/dev/null 2>&1; then
    printf "%s\n" "python3"
  elif command -v python >/dev/null 2>&1; then
    printf "%s\n" "python"
  else
    printf "%s\n" ""
  fi
}

is_root() {
  [ "${EUID:-$(id -u 2>/dev/null || echo 1)}" = "0" ]
}

path_has_dir() {
  local dir="$1" entry
  local IFS=:

  for entry in ${PATH:-}; do
    if [ "$entry" = "$dir" ]; then
      return 0
    fi
    if [ -d "$entry" ] && [ -d "$dir" ] && [ "$entry" -ef "$dir" ] 2>/dev/null; then
      return 0
    fi
  done

  return 1
}

same_file() {
  local a="$1" b="$2"

  if [ "$a" = "$b" ]; then
    return 0
  fi

  [ -e "$a" ] && [ -e "$b" ] && [ "$a" -ef "$b" ] 2>/dev/null
}

shell_quote() {
  local value="$1"
  value="${value//\\/\\\\}"
  value="${value//\"/\\\"}"
  printf '"%s"' "$value"
}

preferred_global_bin_dir() {
  local dir

  for dir in /usr/local/bin /usr/bin /bin; do
    if [ -d "$dir" ] || mkdir -p "$dir" 2>/dev/null; then
      printf "%s\n" "$dir"
      return
    fi
  done

  printf "%s\n" "/usr/local/bin"
}

writable_global_bin_dir() {
  local dir

  for dir in /usr/local/bin /usr/bin /bin; do
    if ([ -d "$dir" ] || mkdir -p "$dir" 2>/dev/null) && [ -w "$dir" ] 2>/dev/null; then
      printf "%s\n" "$dir"
      return
    fi
  done

  printf "\n"
}

current_shell_name() {
  local shell_name="${SHELL:-}"
  shell_name="${shell_name##*/}"

  if [ -n "$shell_name" ]; then
    printf "%s\n" "$shell_name"
    return
  fi

  if [ -n "${ZSH_VERSION:-}" ]; then
    printf "%s\n" "zsh"
  elif [ -n "${BASH_VERSION:-}" ]; then
    printf "%s\n" "bash"
  else
    printf "\n"
  fi
}

shell_rc_file() {
  local shell_name
  shell_name="$(current_shell_name)"

  if [ -z "${HOME:-}" ]; then
    printf "\n"
    return
  fi

  case "$shell_name" in
  zsh)
    printf "%s\n" "$HOME/.zshrc"
    return
    ;;
  bash)
    printf "%s\n" "$HOME/.bashrc"
    return
    ;;
  fish)
    printf "%s\n" "${XDG_CONFIG_HOME:-$HOME/.config}/fish/config.fish"
    return
    ;;
  esac

  printf "\n"
}

path_line_for_shell() {
  local dir="$1" shell_name="$2"

  if [ "$shell_name" = "fish" ]; then
    printf "fish_add_path %s\n" "$(shell_quote "$dir")"
    return
  fi

  printf "export PATH=%s:\"\$PATH\"\n" "$(shell_quote "$dir")"
}

print_path_instructions() {
  local dir="$1" shell_rc="${2:-}"

  echo "Add ${dir} to your PATH, for example:" >&2
  if [ -n "$shell_rc" ]; then
    echo "  echo 'export PATH=\"${dir}:\$PATH\"' >> \"$shell_rc\"" >&2
    echo "  source \"$shell_rc\"" >&2
  else
    echo "  export PATH=\"${dir}:\$PATH\"" >&2
  fi
}

ensure_dir_on_path() {
  local dir="$1" shell_name shell_rc path_line

  if path_has_dir "$dir"; then
    return
  fi

  print_warning "${dir} is not currently in PATH"
  if [ -n "${GITHUB_PATH:-}" ]; then
    echo "$dir" >>"$GITHUB_PATH"
    print_success "Added ${dir} to PATH for the rest of this GitHub Actions job"
    return
  fi

  if [ "$MODIFY_PATH" != "1" ]; then
    print_path_instructions "$dir" "$(shell_rc_file)"
    return
  fi

  shell_name="$(current_shell_name)"
  shell_rc="$(shell_rc_file)"
  if [ -z "$shell_rc" ]; then
    print_path_instructions "$dir" ""
    return
  fi

  path_line="$(path_line_for_shell "$dir" "$shell_name")"
  if [ -f "$shell_rc" ] && grep -F "$dir" "$shell_rc" >/dev/null 2>&1; then
    print_status "${dir} is already listed in ${shell_rc}; open a new terminal or run:"
    echo "  source \"$shell_rc\"" >&2
    return
  fi

  if { [ -e "$shell_rc" ] && [ ! -w "$shell_rc" ]; } || ! mkdir -p "$(dirname "$shell_rc")" 2>/dev/null; then
    print_warning "Could not update ${shell_rc}"
    print_path_instructions "$dir" "$shell_rc"
    return
  fi

  {
    echo
    echo "# Added by Cosine CLI installer"
    echo "$path_line"
  } >>"$shell_rc"

  print_success "Added ${dir} to PATH in ${shell_rc}"
  print_status "Open a new terminal or run: source \"$shell_rc\""
}

binary_version() {
  local path="$1"

  if [ ! -x "$path" ]; then
    printf "\n"
    return
  fi

  "$path" version 2>/dev/null || "$path" --version 2>/dev/null || true
}

maybe_skip_existing_install() {
  local path="$1" resolved_version="$2" current_version

  if [ "$FORCE_INSTALL" = "1" ] || [ "$resolved_version" = "latest" ]; then
    return 1
  fi

  current_version="$(binary_version "$path")"
  current_version="${current_version%%$'\n'*}"

  if [ -z "$current_version" ]; then
    return 1
  fi

  if [ "$current_version" = "$resolved_version" ]; then
    print_success "${CLI_BINARY_NAME} ${resolved_version} is already installed at ${path}"
    ensure_dir_on_path "$(dirname "$path")"
    return 0
  fi

  print_status "Found ${CLI_BINARY_NAME} ${current_version} at ${path}; installing ${resolved_version}."
  return 1
}

fetch_cli_latest_json() {
  local version="$1" os="$2" arch="$3"

  curl_with_retries --get \
    --data-urlencode "version=$version" \
    --data-urlencode "os=$os" \
    --data-urlencode "arch=$arch" \
    "${API_BASE_URL%/}/cli/latest"
}

json_get_string_field() {
  local json="$1" key="$2"
  printf "%s" "$json" | awk -F'"' -v k="$key" '
    {
      for (i = 1; i <= NF; i++) {
        if ($i == k) {
          print $(i + 2)
          exit
        }
      }
    }
  '
}

resolve_cli_latest_and_url() {
  local version="$1" os="$2" arch="$3" json latest url

  json="$(fetch_cli_latest_json "$version" "$os" "$arch")"
  latest="$(json_get_string_field "$json" "latest")"
  url="$(json_get_string_field "$json" "download_url")"

  if [ -z "$latest" ]; then
    err "Unable to resolve latest cli version from Cosine API (${API_BASE_URL%/}/cli/latest)."
    exit 1
  fi

  printf "%s|%s\n" "$latest" "$url"
}

normalize_version() {
  local v="${1-}"
  if [ -z "${v}" ]; then
    printf "%s\n" "$VERSION"
    return
  fi
  case "$v" in
  latest | nightly | nightly-*)
    printf "%s\n" "$v"
    return
    ;;
  esac
  if [[ "$v" == *"-v"* ]]; then
    v="${v#*-v}"
    v="v${v}"
  fi
  printf "%s\n" "$v"
}

detect_os_arch() {
  local os arch
  os="$(uname -s)"
  arch="$(uname -m)"

  case "$os" in
  Linux) os="linux" ;;
  Darwin) os="darwin" ;;
  *)
    err "Unsupported OS: $os"
    exit 1
    ;;
  esac

  case "$arch" in
  x86_64 | amd64) arch="amd64" ;;
  arm64 | aarch64) arch="arm64" ;;
  *)
    err "Unsupported architecture: $arch"
    exit 1
    ;;
  esac

  if [ "$os" = "darwin" ] && [ "$arch" != "arm64" ]; then
    err "macOS amd64 is not supported by prebuilt binaries. Use Rosetta or Homebrew."
    exit 1
  fi

  print_status "Detected platform: ${os}-${arch}"
  printf "%s %s\n" "$os" "$arch"
}

linux_glibc_version() {
  # Returns "major.minor" for glibc if detectable, or empty string otherwise
  if command -v getconf >/dev/null 2>&1; then
    v="$(getconf GNU_LIBC_VERSION 2>/dev/null || true)"
    # Expected format: "glibc 2.35"
    case "$v" in
    *glibc*" "*) v="${v##* }" ;; # take last space-separated token
    esac
    printf "%s\n" "$v"
    return
  fi

  if command -v ldd >/dev/null 2>&1; then
    # ldd --version prints glibc version on first line: "ldd (Ubuntu GLIBC 2.35-0ubuntu3.6) 2.35"
    v="$(ldd --version 2>/dev/null | head -n1)"
    # Try to extract last "X.Y" pattern
    v="$(printf "%s" "$v" | sed -n 's/.* \([0-9]\+\.[0-9]\+\).*/\1/p')"
    printf "%s\n" "$v"
    return
  fi

  printf "\n"
}

extract_zip() {
  local zip="$1" dest="$2" py
  if command -v unzip >/dev/null 2>&1; then
    unzip -q "$zip" -d "$dest"
  else
    py="$(python_cmd)"
    if [ -z "$py" ]; then
      err "unzip is not available and no Python interpreter was found to extract ${zip}."
      err "Install unzip or python3 and re-run."
      exit 1
    fi
    "$py" - "$zip" "$dest" <<'PY'
import sys, zipfile, os
zip_path, dest = sys.argv[1], sys.argv[2]
os.makedirs(dest, exist_ok=True)
with zipfile.ZipFile(zip_path) as z:
    z.extractall(dest)
PY
  fi
}

choose_install_dir() {
  # Respect explicit override
  if [ -n "${INSTALL_DIR:-}" ]; then
    print_status "Using install directory from COSINE_INSTALL_DIR: ${INSTALL_DIR}"
    printf "%s\n" "$INSTALL_DIR"
    return
  fi

  # sudo/root installs should be available globally, not under /root/.local/bin.
  if is_root; then
    local global_dir
    global_dir="$(preferred_global_bin_dir)"
    print_status "Running as root; using global install directory: ${global_dir}"
    printf "%s\n" "$global_dir"
    return
  fi

  local global_dir
  global_dir="$(writable_global_bin_dir)"
  if [ -n "$global_dir" ]; then
    print_status "Using writable global install directory: ${global_dir}"
    printf "%s\n" "$global_dir"
    return
  fi

  if command -v sudo >/dev/null 2>&1 && [ -t 2 ]; then
    global_dir="$(preferred_global_bin_dir)"
    print_status "Using global install directory with sudo: ${global_dir}"
    printf "%s\n" "$global_dir"
    return
  fi

  # Default to user-space install when a global install is not available.
  if [ -n "${HOME:-}" ]; then
    if path_has_dir "$HOME/.local/bin" &&
      ([ -d "$HOME/.local/bin" ] || mkdir -p "$HOME/.local/bin" 2>/dev/null); then
      printf "%s\n" "$HOME/.local/bin"
      return
    fi
    if path_has_dir "$HOME/bin" &&
      ([ -d "$HOME/bin" ] || mkdir -p "$HOME/bin" 2>/dev/null); then
      printf "%s\n" "$HOME/bin"
      return
    fi
    if [ -d "$HOME/.local/bin" ] || mkdir -p "$HOME/.local/bin" 2>/dev/null; then
      printf "%s\n" "$HOME/.local/bin"
      return
    fi
    if [ -d "$HOME/bin" ] || mkdir -p "$HOME/bin" 2>/dev/null; then
      printf "%s\n" "$HOME/bin"
      return
    fi
  fi

  for d in /usr/local/bin /usr/bin; do
    if [ -d "$d" ]; then
      printf "%s\n" "$d"
      return
    fi
  done

  # Last resort
  printf "%s\n" "/usr/local/bin"
}

install_file() {
  local src="$1" dst_dir="$2" name="$3" dst
  dst="$dst_dir/$name"

  if [ -d "$dst_dir" ] || mkdir -p "$dst_dir" 2>/dev/null; then
    if [ -w "$dst_dir" ] 2>/dev/null; then
      if ! install -m 0755 "$src" "$dst" 2>/dev/null; then
        chmod +x "$src"
        mv -f "$src" "$dst"
      fi
      printf "%s\n" "$dst"
      return
    fi
  fi

  if command -v sudo >/dev/null 2>&1; then
    if sudo mkdir -p "$dst_dir" &&
      (sudo install -m 0755 "$src" "$dst" 2>/dev/null ||
        { sudo cp "$src" "$dst" && sudo chmod 0755 "$dst"; }); then
      printf "%s\n" "$dst"
      return
    fi
    print_warning "Unable to install to ${dst_dir} with sudo; falling back to a user install."
  fi

  if [ -z "${HOME:-}" ]; then
    err "Cannot write to ${dst_dir} and sudo is not available."
    err "Set COSINE_INSTALL_DIR to a writable directory and re-run."
    exit 1
  fi

  local user_dir
  user_dir="$HOME/.local/bin"
  if [ "$dst_dir" != "$user_dir" ]; then
    print_warning "Unable to write to ${dst_dir}; installing to ${user_dir} instead."
  fi
  mkdir -p "$user_dir"
  chmod +x "$src"
  mv -f "$src" "$user_dir/$name"
  printf "%s\n" "$user_dir/$name"
}

install_cli() {
  local version="$1" os="$2" arch="$3"
  local resolved_version="$version" url="" asset="" base_url="" dest_dir="" installed_path=""

  case "$version" in
  latest | beta)
    case "$os" in
    darwin) asset="cos-darwin-arm64.zip" ;;
    linux)
      if [ "$arch" = "amd64" ]; then
        glibc_ver="$(linux_glibc_version)"
        if [ -n "$glibc_ver" ]; then
          glibc_major="${glibc_ver%%.*}"
          glibc_minor="${glibc_ver#*.}"
          if [ "$glibc_major" -eq 2 ] && [ "$glibc_minor" -lt 39 ]; then
            asset="cos-linux-amd64-glibc2.35.zip"
          else
            asset="cos-linux-amd64.zip"
          fi
        else
          asset="cos-linux-amd64.zip"
        fi
      else
        asset="cos-linux-arm64.zip"
      fi
      ;;
    esac

    url="https://software.cosine.sh/cli/release/latest/${asset}"
    resolved_version="latest"
    ;;
  nightly)
    IFS="|" read -r resolved_version url < <(resolve_cli_latest_and_url "$version" "$os" "$arch")
    if [ -z "$url" ]; then
      err "Cosine API did not return a download_url for cli ${version} (os=$os arch=$arch)."
      exit 1
    fi
    # The API conservatively returns the glibc2.35 build for Linux amd64.
    # Upgrade to the standard build if the local glibc is >= 2.39.
    if [ "$os" = "linux" ] && [ "$arch" = "amd64" ]; then
      glibc_ver="$(linux_glibc_version)"
      if [ -n "$glibc_ver" ]; then
        glibc_major="${glibc_ver%%.*}"
        glibc_minor="${glibc_ver#*.}"
        if [ "$glibc_major" -eq 2 ] && [ "$glibc_minor" -ge 39 ]; then
          url="${url/cos-linux-amd64-glibc2.35.zip/cos-linux-amd64.zip}"
          url="${url/cos2-linux-amd64-glibc2.35.zip/cos-linux-amd64.zip}"
        fi
      fi
    fi
    if [ "$os" = "linux" ] && [ "$arch" = "arm64" ]; then
      glibc_ver="$(linux_glibc_version)"
      if [ -n "$glibc_ver" ]; then
        glibc_major="${glibc_ver%%.*}"
        glibc_minor="${glibc_ver#*.}"
        if [ "$glibc_major" -eq 2 ] && [ "$glibc_minor" -lt 39 ]; then
          err "The cos2 Linux arm64 binary requires glibc 2.39 or later."
          err "Your system has glibc ${glibc_ver}. The arm64 build does not have a glibc2.35 compatibility variant."
          err "Please upgrade your system's glibc (e.g. upgrade to Ubuntu 24.04 or later)."
          exit 1
        fi
      fi
    fi
    asset="${url##*/}"
    ;;
  *)
    case "$os" in
    darwin) asset="cos-darwin-arm64.zip" ;;
    linux)
      if [ "$arch" = "amd64" ]; then
        glibc_ver="$(linux_glibc_version)"
        if [ -n "$glibc_ver" ]; then
          glibc_major="${glibc_ver%%.*}"
          glibc_minor="${glibc_ver#*.}"
          if [ "$glibc_major" -eq 2 ] && [ "$glibc_minor" -lt 39 ]; then
            asset="cos-linux-amd64-glibc2.35.zip"
          else
            asset="cos-linux-amd64.zip"
          fi
        else
          asset="cos-linux-amd64.zip"
        fi
      else
        glibc_ver="$(linux_glibc_version)"
        if [ -n "$glibc_ver" ]; then
          glibc_major="${glibc_ver%%.*}"
          glibc_minor="${glibc_ver#*.}"
          if [ "$glibc_major" -eq 2 ] && [ "$glibc_minor" -lt 39 ]; then
            err "The cos2 Linux arm64 binary requires glibc 2.39 or later."
            err "Your system has glibc ${glibc_ver}. The arm64 build does not have a glibc2.35 compatibility variant."
            err "Please upgrade your system's glibc (e.g. upgrade to Ubuntu 24.04 or later)."
            exit 1
          fi
        fi
        asset="cos2-linux-arm64.zip"
      fi
      ;;
    esac

    base_url="https://github.com/${REPO_OWNER}/${CLI_REPO_NAME}/releases/download/${resolved_version}"
    url="${base_url}/${asset}"
    ;;
  esac

  dest_dir="$(choose_install_dir)"
  installed_path="${dest_dir}/${CLI_BINARY_NAME}"
  if maybe_skip_existing_install "$installed_path" "$resolved_version"; then
    return
  fi

  tmpdir="$(mktemp -d)"
  chmod 700 "$tmpdir" 2>/dev/null || true
  trap 'rm -rf "$tmpdir"' EXIT INT TERM

  if [ "$version" = "$resolved_version" ]; then
    print_status "Downloading ${asset} from ${resolved_version}..."
  else
    print_status "Downloading ${asset} from ${version} (${resolved_version})..."
  fi
  print_status "Download URL: ${url}"

  curl_with_retries -o "${tmpdir}/${asset}" "$url"

  extract_dir="${tmpdir}/out"
  print_status "Extracting archive..."
  extract_zip "${tmpdir}/${asset}" "$extract_dir"

  archive_binary_name="$CLI_BINARY_NAME"
  if [ ! -f "${extract_dir}/${archive_binary_name}" ] && [ -f "${extract_dir}/cos" ]; then
    archive_binary_name="cos"
  fi

  if [ ! -f "${extract_dir}/${archive_binary_name}" ]; then
    err "Binary '${CLI_BINARY_NAME}' not found in archive"
    exit 1
  fi

  installed_path="$(install_file "${extract_dir}/${archive_binary_name}" "$dest_dir" "${CLI_BINARY_NAME}")"
  installed_dir="$(dirname "$installed_path")"

  print_success "Installed to: ${installed_path}"

  active_path="$(command -v "${CLI_BINARY_NAME}" 2>/dev/null || true)"
  "$installed_path" version || "$installed_path" --version || true

  if path_has_dir "$installed_dir"; then
    if [ -n "$active_path" ] && ! same_file "$active_path" "$installed_path"; then
      print_warning "Another '${CLI_BINARY_NAME}' was found earlier on PATH: ${active_path}"
      print_warning "To use the installed binary, ensure ${installed_dir} appears before it in your PATH."
    fi
  else
    ensure_dir_on_path "$installed_dir"
  fi
}

show_usage() {
  echo "Usage:" >&2
  echo "  curl -fsSL https://cosine.sh/install | bash -s -- [VERSION]" >&2
  echo "  curl -fsSL https://cosine.sh/install | bash -s -- --version VERSION" >&2
  echo "" >&2
  echo "VERSION can be: latest | nightly | nightly-<n> | vX.Y.Z | vX.Y.Z-betaN" >&2
  echo "" >&2
  echo "Options:" >&2
  echo "  --install-dir DIR    Install cos into DIR (same as COSINE_INSTALL_DIR)" >&2
  echo "  --no-modify-path     Do not edit shell startup files; print PATH instructions instead" >&2
  echo "  --force              Reinstall even when the requested version is already installed" >&2
  echo "  -h, --help           Show this help" >&2
}

parse_args() {
  local version_arg=""

  while [ "$#" -gt 0 ]; do
    case "$1" in
    -h | --help)
      show_usage
      exit 0
      ;;
    cos2 | cli2)
      ;;
    --version)
      shift
      if [ "$#" -eq 0 ]; then
        err "--version requires a value"
        exit 1
      fi
      version_arg="$1"
      ;;
    --version=*)
      version_arg="${1#*=}"
      ;;
    --install-dir)
      shift
      if [ "$#" -eq 0 ]; then
        err "--install-dir requires a value"
        exit 1
      fi
      INSTALL_DIR="$1"
      ;;
    --install-dir=*)
      INSTALL_DIR="${1#*=}"
      ;;
    --no-modify-path)
      MODIFY_PATH=0
      ;;
    --modify-path)
      MODIFY_PATH=1
      ;;
    --force)
      FORCE_INSTALL=1
      ;;
    --)
      shift
      if [ "$#" -gt 0 ]; then
        if [ -n "$version_arg" ]; then
          err "Unexpected argument: $1"
          exit 1
        fi
        version_arg="$1"
        shift
      fi
      if [ "$#" -gt 0 ]; then
        err "Unexpected argument: $1"
        exit 1
      fi
      break
      ;;
    -*)
      err "Unknown option: $1"
      show_usage
      exit 1
      ;;
    *)
      if [ -n "$version_arg" ]; then
        err "Unexpected argument: $1"
        show_usage
        exit 1
      fi
      version_arg="$1"
      ;;
    esac
    shift
  done

  VERSION="$(normalize_version "$version_arg")"
}

main() {
  parse_args "$@"

  show_logo
  print_status "Starting Cosine CLI installation..."

  need_cmd uname
  need_cmd curl
  need_cmd awk

  if [ "$VERSION" = "beta" ]; then
    err "The 'beta' flag is no longer supported. Use 'latest' instead."
    exit 1
  fi

  read -r os arch < <(detect_os_arch)
  install_cli "$VERSION" "$os" "$arch"

  print_success "Installation complete"
  echo >&2
  print_status "Next steps:"
  echo "  1. Run 'cos login' to authenticate" >&2
  echo "  2. Start the Cosine UI with 'cos' (or 'cos start')" >&2
  echo >&2
  echo "Cosine CLI quick tips:" >&2
  echo "- 'cos' loads the folder you're currently in." >&2
  echo "- To use a different project, cd into it first, or run: cos --cwd=/path/to/project" >&2
  echo "- Run one-shot tasks without the UI: cos start --prompt \"your prompt\"" >&2
  echo >&2

  prompt_1="Look through my codebase and identify 1 major bottleneck to optimize."
  prompt_2="Find the slowest build step in this repo and suggest concrete speedups."
  prompt_3="Audit this project for expensive DB queries and propose query/index fixes."
  prompt_4="Profile likely hot paths and recommend the highest-ROI optimization first."
  prompt_5="Find unnecessary network calls and suggest batching or caching improvements."
  prompt_6="Identify 3 memory-heavy areas and propose changes to reduce allocations."
  prompt_7="Review test runtime and suggest ways to cut CI time by at least 20%."
  prompt_8="Find frontend rendering bottlenecks and suggest measurable improvements."
  prompt_9="Locate duplicate work across services and propose consolidation opportunities."
  prompt_10="Review startup/init time and suggest one change to improve cold-start performance."

  idx=$((RANDOM % 10 + 1))
  selected_prompt_var="prompt_${idx}"
  selected_prompt="${!selected_prompt_var}"

  echo "Example prompt to try:" >&2
  echo "  ${selected_prompt}" >&2
  echo >&2
  echo "For detailed docs head to: https://cosine.sh/docs/cli" >&2
}

main "$@"
