#!/bin/zsh
# Sue2 V7 — internal-test installer (Apple Silicon Macs).
# Installs uv (if missing), installs Sue2 V7 from the wheel in this
# folder into an isolated environment, and creates ~/Applications/Sue2.app.
# Nothing touches your system Python. Uninstall: uv tool uninstall sue2
set -e

cd "$(dirname "$0")"

if [[ "$(uname -m)" != "arm64" ]]; then
  echo "Sorry - this internal build is Apple Silicon (arm64) only."; exit 1
fi

WHEEL=$(ls sue2-*.whl 2>/dev/null | tail -1)
if [[ -z "$WHEEL" ]]; then
  echo "No sue2-*.whl found next to install.sh."; exit 1
fi

# 1) uv — the installer/runtime manager (also provides Python itself)
if ! command -v uv >/dev/null 2>&1 && [[ ! -x "$HOME/.local/bin/uv" ]]; then
  echo "==> Installing uv (Python tool manager) ..."
  curl -LsSf https://astral.sh/uv/install.sh | sh
fi
export PATH="$HOME/.local/bin:$PATH"

# 2) Sue2 V7 with the full feature set (waveform viewer + AI/MCP server)
echo "==> Installing Sue2 V7 from $WHEEL ..."
uv tool install --force --python 3.12 "sue2[waveview,mcp] @ ./$WHEEL"

# 3) Launch icon (Dock / Launchpad / Spotlight)
echo "==> Creating ~/Applications/Sue2.app ..."
"$HOME/.local/bin/sue2" --make-launcher

echo ""
echo "Done. Start Sue2 either way:"
echo "  - double-click Sue2 in ~/Applications (add it to your Dock), or"
echo "  - run 'sue2' in a terminal (you may need a new terminal window)."
echo "First launch creates ~/CppSim with the example libraries."
echo "Health check any time:  sue2 --doctor"
