#!/bin/sh
# Brain installer (macOS / Linux):   curl -fsSL https://cli.theup.io | sh
# Served by ml-app (app/api/install_routes.py). The wheel name and its SHA256 below were pinned when the wheel was published
# (ml-app/scripts/publish_adapter_wheel.sh); the wheel is the adapter-only client: the engine runs on Brain's server.
set -eu
WHEEL_NAME='theup_brain-0.3.6.dev202611210844+5ece88cc96-py3-none-any.whl'
WHEEL_SHA256='c6a826d1066b74217ae43562a4b0f8bed6d37dbec039310f0a893629684884a6'
WHEEL_URL='https://cli.theup.io/dist/theup_brain-0.3.6.dev202611210844+5ece88cc96-py3-none-any.whl'
caller_path=$PATH
umask 077
fail() { printf '%s\n' "brain install: $*" >&2; exit 2; }
say() { printf '%s\n' "$*" >&2; }

os=$(uname -s 2>/dev/null || echo unknown)
arch=$(uname -m 2>/dev/null || echo unknown)
case "$os" in
    Darwin|Linux) ;;
    MINGW*|MSYS*|CYGWIN*|Windows_NT*) fail "on Windows run this in PowerShell instead: irm https://cli.theup.io/install.ps1 | iex" ;;
    *) fail "unsupported system: $os (Brain installs on macOS, Linux and Windows)" ;;
esac
case "$arch" in
    x86_64|amd64|arm64|aarch64) ;;
    *) fail "unsupported processor: $arch (Brain needs a 64-bit x86 or ARM machine)" ;;
esac
case "${HOME:-}" in /*) ;; *) fail "HOME must be an absolute directory" ;; esac
command -v curl >/dev/null 2>&1 || fail "curl is required"
case "$WHEEL_NAME" in *[!A-Za-z0-9._+-]*|'') fail "this installer was not published correctly (wheel name)" ;; esac
[ "${#WHEEL_SHA256}" -eq 64 ] || fail "this installer was not published correctly (checksum)"

mkdir -p "$HOME/.local" "$HOME/.cache/uv"
scratch=$(mktemp -d "$HOME/.local/brain-install.XXXXXX")
trap 'rm -rf "$scratch"' 0
trap 'exit 130' 2
trap 'exit 143' 15

say "[1/3] Downloading Brain ($os $arch)..."
curl --fail --silent --show-error --location "$WHEEL_URL" -o "$scratch/$WHEEL_NAME"
say "[2/3] Checking the download..."
if command -v shasum >/dev/null 2>&1; then actual=$(shasum -a 256 "$scratch/$WHEEL_NAME")
elif command -v sha256sum >/dev/null 2>&1; then actual=$(sha256sum "$scratch/$WHEEL_NAME")
else fail "neither shasum nor sha256sum found; cannot verify the download"; fi
actual=${actual%% *}
[ "$actual" = "$WHEEL_SHA256" ] || fail "SHA256 mismatch (got $actual); nothing was installed"

# Brain's own environment: a uv tool venv under ~/.local/share/uv/tools/theup-brain (no sudo, no rc/profile edits).
export UV_CACHE_DIR="$HOME/.cache/uv"
export UV_TOOL_DIR="$HOME/.local/share/uv/tools"
export UV_TOOL_BIN_DIR="$HOME/.local/bin"
export UV_PYTHON_INSTALL_DIR="$HOME/.local/share/uv/python"
export UV_PYTHON_BIN_DIR="$HOME/.local/bin"
export UV_NO_MODIFY_PATH=1
if ! command -v uv >/dev/null 2>&1; then
    say "      (one-time: fetching uv, then Python 3.12; about 1-3 minutes, no sudo)"
    curl --fail --silent --show-error --location https://astral.sh/uv/install.sh -o "$scratch/uv-installer.sh"
    UV_INSTALL_DIR="$HOME/.local/bin" sh "$scratch/uv-installer.sh" >&2
    PATH="$HOME/.local/bin:$PATH"; export PATH
fi
say "[3/3] Installing Brain into its own environment..."
uv tool install --force --python "${BRAIN_INSTALL_PYTHON:-3.12}" "$scratch/$WHEEL_NAME" >&2
"$HOME/.local/bin/brain" --help >/dev/null || fail "brain did not start after install"

case ":$caller_path:" in
    *":$HOME/.local/bin:"*) b=brain; tip= ;;
    *) b="\"$HOME/.local/bin/brain\""
       case "${SHELL:-}" in */zsh) rc='~/.zshrc' ;; *) rc='~/.bashrc' ;; esac
       tip="tip: to type just brain, run once: echo 'export PATH=\"\$HOME/.local/bin:\$PATH\"' >> $rc  (then open a new terminal)" ;;
esac
printf '%s\n' "Brain is installed. Its checks run on Brain's server: brain init signs you in and asks what Brain may read." \
              "next: cd your-repo && $b init"
[ -z "$tip" ] || printf '%s\n' "$tip"
