#!/usr/bin/env sh
# total-agent-memory POSIX one-liner installer.
#
#   curl -fsSL https://get.totalmemory.dev | sh
#   curl -fsSL https://totalmemory.dev/install.sh | sh
#
# Picks the first available package manager (in order of preference):
#   1. npx (Node.js) — works without any global install
#   2. uvx (Astral uv) — Python-native, fastest cold-start once cached
#   3. pipx — isolated global Python install
#   4. python3 + pip — last-resort fallback (uses ~/.claude-memory/.venv)
#
# Pass arguments after the pipe by piping into `sh -s -- <args>`:
#   curl -fsSL https://get.totalmemory.dev | sh -s -- connect claude-code
#
# Env overrides:
#   TAM_FORCE=npx|uvx|pipx|venv     pin a specific path
#   TAM_VERSION=11.2.1              pin a server version
#   TAM_VERBOSE=1                   show every command

set -eu

NPM_NAME="total-agent-memory"
PYPI_NAME="claude-total-memory"
# Reference (unused, kept for grep-ability):
# PROJECT_URL="https://github.com/vbcherepanov/total-agent-memory"

# Colours if a TTY is attached.
if [ -t 1 ] && command -v tput >/dev/null 2>&1; then
  BOLD="$(tput bold || true)"
  DIM="$(tput dim || true)"
  RED="$(tput setaf 1 || true)"
  GRN="$(tput setaf 2 || true)"
  CYAN="$(tput setaf 6 || true)"
  RST="$(tput sgr0 || true)"
else
  BOLD=""; DIM=""; RED=""; GRN=""; CYAN=""; RST=""
fi

say()  { printf '%s%s%s\n' "${CYAN}" "$*" "${RST}"; }
ok()   { printf '%s✓%s %s\n' "${GRN}" "${RST}" "$*"; }
warn() { printf '%s!%s %s\n' "${BOLD}" "${RST}" "$*"; }
err()  { printf '%s✗%s %s\n' "${RED}" "${RST}" "$*" >&2; }

trap 'err "installer failed at line $LINENO"; exit 1' INT TERM

run() {
  if [ "${TAM_VERBOSE:-0}" = "1" ]; then printf '%s$ %s%s\n' "${DIM}" "$*" "${RST}" >&2; fi
  "$@"
}

has() { command -v "$1" >/dev/null 2>&1; }

# ── Banner ─────────────────────────────────────────────────────────────
printf '\n%stotal-agent-memory%s — persistent memory for AI coding agents\n' "${BOLD}" "${RST}"
printf '  %sdocs: https://totalmemory.dev%s\n\n' "${DIM}" "${RST}"

# ── Detect chosen path ─────────────────────────────────────────────────
choose() {
  if [ -n "${TAM_FORCE:-}" ]; then
    echo "${TAM_FORCE}"; return
  fi
  if has npx;  then echo npx;  return; fi
  if has uvx;  then echo uvx;  return; fi
  if has pipx; then echo pipx; return; fi
  if has python3; then echo venv; return; fi
  echo none
}

PATH_CHOSEN="$(choose)"

case "$PATH_CHOSEN" in
  npx)
    say "Using npx (Node) — no global install."
    exec npx --yes "${NPM_NAME}${TAM_VERSION:+@$TAM_VERSION}" "$@"
    ;;
  uvx)
    say "Using uvx (Astral uv) — Python-native."
    exec uvx "${PYPI_NAME}${TAM_VERSION:+==$TAM_VERSION}" "$@"
    ;;
  pipx)
    say "Using pipx — isolated global install."
    if ! pipx list 2>/dev/null | grep -q "${PYPI_NAME}"; then
      run pipx install "${PYPI_NAME}${TAM_VERSION:+==$TAM_VERSION}"
    fi
    exec "${PYPI_NAME}" "$@"
    ;;
  venv)
    say "Using python3 + pip — last-resort fallback."
    MEM_DIR="${CLAUDE_MEMORY_DIR:-$HOME/.claude-memory}"
    VENV="$MEM_DIR/.venv"
    if [ ! -x "$VENV/bin/${PYPI_NAME}" ]; then
      run mkdir -p "$MEM_DIR"
      run python3 -m venv "$VENV"
      run "$VENV/bin/pip" install --quiet --upgrade pip
      run "$VENV/bin/pip" install --quiet "${PYPI_NAME}${TAM_VERSION:+==$TAM_VERSION}"
    fi
    exec "$VENV/bin/${PYPI_NAME}" "$@"
    ;;
  none)
    err "No package manager found. Install one of: npx (Node), uvx, pipx, python3."
    cat <<'TXT'

Quick install (pick one):
  macOS:    brew install node     # or: brew install python uv
  Debian:   sudo apt install nodejs python3 python3-venv
  Fedora:   sudo dnf install nodejs python3
  Windows:  winget install OpenJS.NodeJS

Then re-run the installer:
  curl -fsSL https://get.totalmemory.dev | sh

Docs: https://totalmemory.dev
TXT
    exit 1
    ;;
  *)
    err "Unknown TAM_FORCE='$PATH_CHOSEN' (allowed: npx|uvx|pipx|venv)"
    exit 2
    ;;
esac
