#!/usr/bin/env bash
# install.sh — one-command installer for the Amparo agent by EL AI Intelligence.
#
#     curl -fsSL https://downloads.ellmstack.dev/amparo/install.sh | bash
#
# Downloads the build for your platform, verifies its SHA-256 checksum, and
# installs the binary into ~/.local/bin (override with INSTALL_DIR).
# Override the download base with AMPARO_RELEASE_BASE.
#
# Supported: linux (x86_64, arm64), darwin (x86_64, arm64). darwin verifies
# checksums with `shasum -a 256` (no sha256sum by default). Our darwin
# binaries are UNSIGNED, so after install this script strips the quarantine
# attribute and applies an ad-hoc codesign — without that, Gatekeeper would
# refuse to run them. Windows prints a pointer to install.ps1.
#
# AMPARO_FORCE_PLATFORM=<os>-<arch> is TEST-ONLY: simulate another platform
# from any dev box to exercise the download/verify/extract paths.
#
# No sudo, no secrets, no telemetry.
set -euo pipefail

BASE="${AMPARO_RELEASE_BASE:-https://downloads.ellmstack.dev/amparo}"
INSTALL_DIR="${INSTALL_DIR:-$HOME/.local/bin}"

# ── Platform detection ──────────────────────────────────────────────────────
if [[ -n "${AMPARO_FORCE_PLATFORM:-}" ]]; then
  os="${AMPARO_FORCE_PLATFORM%%-*}"
  arch="${AMPARO_FORCE_PLATFORM##*-}"
  echo "NOTE: AMPARO_FORCE_PLATFORM=${AMPARO_FORCE_PLATFORM} — simulating ${os}/${arch} (test-only)." >&2
else
  os="$(uname -s)"
  arch="$(uname -m)"
fi
os="$(printf '%s' "$os" | tr '[:upper:]' '[:lower:]')"
arch="$(printf '%s' "$arch" | tr '[:upper:]' '[:lower:]')"
case "$arch" in
  x86_64|amd64) arch="x86_64" ;;
  aarch64|arm64) arch="arm64" ;;
esac

case "${os}-${arch}" in
  windows-*|mingw*-*|msys*-*|cygwin*-*)
    echo "[install] windows: run install.ps1" >&2
    echo "[install]   irm https://downloads.ellmstack.dev/amparo/install.ps1 | iex" >&2
    exit 2
    ;;
  linux-x86_64|linux-arm64)
    asset="amparo-${os}-${arch}.tar.gz"
    hasher="sha256sum"
    ;;
  darwin-x86_64|darwin-arm64)
    asset="amparo-${os}-${arch}.tar.gz"
    hasher="shasum -a 256"
    ;;
  *)
    echo "[install] unsupported platform: ${os}/${arch}" >&2
    echo "[install] prebuilt binaries ship for: linux (x86_64, arm64) and macOS (x86_64, arm64)." >&2
    exit 1
    ;;
esac

echo "[install] fetching ${BASE}/${asset}"
tmp="$(mktemp -d)"
trap 'rm -rf "$tmp"' EXIT
curl -fsSL "$BASE/$asset" -o "$tmp/$asset"
curl -fsSL "$BASE/$asset.sha256" -o "$tmp/$asset.sha256"
(cd "$tmp" && $hasher -c "$asset.sha256")
tar -xzf "$tmp/$asset" -C "$tmp"
mkdir -p "$INSTALL_DIR"
install -m 0755 "$tmp/amparo" "$INSTALL_DIR/amparo"

# Our darwin binaries are unsigned. Strip the quarantine attribute the
# browser sets on downloads and apply an ad-hoc codesign so Gatekeeper
# lets it run.
if [[ "$os" == "darwin" ]]; then
  xattr -dr com.apple.quarantine "$INSTALL_DIR/amparo" 2>/dev/null || true
  codesign --force --deep -s - "$INSTALL_DIR/amparo" 2>/dev/null || true
  echo "[install] macOS: quarantine cleared + ad-hoc signed."
fi

echo "[install] $INSTALL_DIR/amparo"
echo "[install] run: amparo"
echo "[install] it starts the one-time setup wizard, then the interactive terminal"
