#!/bin/sh
#
# BeagleLathe installer
#   curl -LsSf https://beaglelathe.dev/install.sh | sh
#
# What it does:
#   1. Picks the best Python installer available (uv > pipx > pip)
#   2. Installs `beaglelathe` into an isolated venv (no PEP 668 fights)
#   3. Confirms the Claude Code CLI is on PATH
#   4. Registers the plugin with Claude Code and signs you in
#
# Flags:
#   --no-login   register the plugin but skip the magic-link sign-in
#   --no-color   disable color output
#   -h, --help   show this help and exit
#
# Source: https://github.com/BeagleLathe/lathe
# Issues: https://github.com/BeagleLathe/lathe/issues
#
set -eu

NO_LOGIN=0
USE_COLOR=auto

for arg in "$@"; do
  case "$arg" in
    --no-login) NO_LOGIN=1 ;;
    --no-color) USE_COLOR=0 ;;
    -h|--help)
      sed -n '2,16p' "$0" 2>/dev/null || cat <<'USAGE'
Usage: install.sh [--no-login] [--no-color]
USAGE
      exit 0
      ;;
    *)
      printf 'unknown flag: %s\n' "$arg" >&2
      exit 2
      ;;
  esac
done

if [ "$USE_COLOR" = auto ]; then
  if [ -t 1 ] && [ -t 2 ]; then USE_COLOR=1; else USE_COLOR=0; fi
fi
if [ "$USE_COLOR" = 1 ]; then
  B=$(printf '\033[1m'); G=$(printf '\033[32m'); Y=$(printf '\033[33m')
  R=$(printf '\033[31m'); Z=$(printf '\033[0m')
else
  B=""; G=""; Y=""; R=""; Z=""
fi

info() { printf '%s==>%s %s\n' "$B$G" "$Z" "$*"; }
warn() { printf '%s!!%s  %s\n' "$B$Y" "$Z" "$*" >&2; }
fail() { printf '%s!!%s  %s\n' "$B$R" "$Z" "$*" >&2; exit 1; }
have() { command -v "$1" >/dev/null 2>&1; }

case "$(uname -s 2>/dev/null || echo unknown)" in
  Linux|Darwin) ;;
  *)
    fail "Unsupported platform: $(uname -s).
   Windows users: install Python from https://python.org, then run
     pip install beaglelathe && beaglelathe login
   in a fresh terminal. Docs: https://beaglelathe.dev/docs.html#install"
    ;;
esac

have claude || fail "Claude Code CLI ('claude') not on PATH.
   Install: https://claude.com/code
   Then re-run this installer."

# ── Pick a Python installer ─────────────────────────────────────────────────
if   have uv;   then INSTALLER=uv
elif have pipx; then INSTALLER=pipx
elif have pip3; then INSTALLER=pip
elif have pip;  then INSTALLER=pip
else
  fail "No Python installer found (uv, pipx, or pip).
   Install uv (recommended, fastest):
     curl -LsSf https://astral.sh/uv/install.sh | sh
   Then re-run this installer."
fi

info "Installing beaglelathe via $B$INSTALLER$Z (this can take ~30s on first run)..."

case "$INSTALLER" in
  uv)
    # --force upgrades in place if already installed
    uv tool install --force beaglelathe
    ;;
  pipx)
    pipx install --force beaglelathe
    ;;
  pip)
    PIP_CMD=pip3; have pip3 || PIP_CMD=pip
    PIP_ERR=$(mktemp)
    if ! "$PIP_CMD" install --user --upgrade beaglelathe 2>"$PIP_ERR"; then
      if grep -q externally-managed-environment "$PIP_ERR" 2>/dev/null; then
        cat "$PIP_ERR" >&2
        rm -f "$PIP_ERR"
        fail "Your Python is externally managed (PEP 668).
   Install uv first, then re-run this installer:
     curl -LsSf https://astral.sh/uv/install.sh | sh"
      fi
      cat "$PIP_ERR" >&2
      rm -f "$PIP_ERR"
      fail "pip install failed (see output above)."
    fi
    rm -f "$PIP_ERR"
    ;;
esac

# ── Make beaglelathe reachable in this shell ────────────────────────────────
# uv tool / pipx put scripts in ~/.local/bin; if that's not on PATH yet, our
# next step (`beaglelathe install`) can't find the binary.
if ! have beaglelathe; then
  if [ -x "$HOME/.local/bin/beaglelathe" ]; then
    PATH="$HOME/.local/bin:$PATH"
    export PATH
    warn "Added \$HOME/.local/bin to PATH for this session.
   To make it permanent, add this line to your shell rc (~/.bashrc, ~/.zshrc):
     export PATH=\"\$HOME/.local/bin:\$PATH\""
  else
    fail "Installed beaglelathe, but it isn't reachable on PATH.
   See https://beaglelathe.dev/docs.html#install for help."
  fi
fi

# ── Register with Claude Code (+ optional sign-in) ──────────────────────────
if [ "$NO_LOGIN" = 1 ]; then
  beaglelathe install
  printf '\n%sDone.%s Sign in any time with %sbeaglelathe login%s.\n' \
    "$B$G" "$Z" "$B" "$Z"
else
  beaglelathe login
fi
