#!/bin/sh
# Install the Supervibe CLI and MCP server.
#
#   curl -fsSL https://get.apps.supersidestaging.com/install.sh | sh
#
# POSIX sh, not bash: this runs on whatever a laptop has, and the one thing worse than no
# install path is one that fails on the user's shell before it has said anything useful.
set -eu

# The host in BASE and in the usage line above is a placeholder, the same literal the
# manifests in platform/ carry: publish:tools rewrites every occurrence to the host it is
# actually publishing to, so the copy people curl always names the platform it came from.
# Editing it here changes nothing about a published script -- change
# SUPERVIBE_DOWNLOADS_HOST instead. (This comment does not spell the literal out, or the
# rewrite would turn it into a claim that the real host is the placeholder.)
BASE="${SUPERVIBE_DOWNLOAD_BASE:-${PLATVIBE_DOWNLOAD_BASE:-https://get.apps.supersidestaging.com}}"
VERSION="${SUPERVIBE_VERSION:-${PLATVIBE_VERSION:-latest}}"
# Per-user by default, so installing never asks for a password: an install that stops at a
# sudo prompt is one an agent cannot finish, and one a person may not have the rights for.
# ~/.local/bin is the XDG location and is already on PATH on most Linux distributions.
BINDIR="${SUPERVIBE_BIN_DIR:-${PLATVIBE_BIN_DIR:-$HOME/.local/bin}}"

# The release signing public key (ECDSA P-256, PEM), pinned here and, byte for byte, in
# tools/release-signing-key.pub. publish:tools signs SHA256SUMS with the matching AWS KMS key
# and uploads SHA256SUMS.sig beside it; when a key is pinned, this script refuses to install
# anything whose checksum file does not verify against it. That is the difference between
# "the download is intact" (what a checksum from the same bucket proves) and "Superside's
# release pipeline produced it" (what a signature proves).
#
# Key alias/supervibe-prod-release-signing (docs/runbooks/release-signing.md). Pinned
# 2026-11-16; before that this was empty and installs verified checksums only. Emptying it
# again is the switch back, and publish:tools keeps signing either way.
#
# SUPERVIBE_RELEASE_PUBKEY_FILE points at a PEM you obtained some other way, such as from a
# checkout of the repository. It replaces the pinned key, so a reader who does not trust the
# bucket that served this script can still check what it installs.
# --- pinned release signing key: begin
RELEASE_PUBKEY='-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE+a3btq3G5WjfRl61buCnTfagPfVY
j/T1yoDB8DNzS71goKfMtkLh67TeRdwnBtUdbqJIcbUiYqNwpr/FJb07bw==
-----END PUBLIC KEY-----'
# --- pinned release signing key: end
PUBKEY_FILE="${SUPERVIBE_RELEASE_PUBKEY_FILE:-}"

die() { echo "install: $*" >&2; exit 1; }

os=$(uname -s | tr '[:upper:]' '[:lower:]')
case "$os" in
  darwin|linux) ;;
  # Git Bash, MSYS2, Cygwin. Windows has its own installer, which needs none of this and
  # puts the binaries where PowerShell, Claude Desktop and Claude Code all find them.
  mingw*|msys*|cygwin*) die "this is Windows: install from PowerShell instead, with
  irm $BASE/install.ps1 | iex" ;;
  *) die "unsupported OS: $os. The CLI is published for macOS, Linux and Windows; build from source with 'cd tools && go install ./cmd/...'." ;;
esac

arch=$(uname -m)
case "$arch" in
  arm64|aarch64) arch=arm64 ;;
  x86_64|amd64)  arch=amd64 ;;
  *) die "unsupported architecture: $arch" ;;
esac

command -v curl >/dev/null 2>&1 || die "curl is required"

# A temporary directory, removed however this exits. Downloading straight into BINDIR
# would leave a half-written binary on the PATH if the transfer dies midway.
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT INT TERM

echo "Installing supervibe ($VERSION) for $os/$arch"

fetch() {
  curl -fsSL --proto '=https' --tlsv1.2 -o "$2" "$1" \
    || die "could not download $1"
}

# The checksum file is fetched from the same immutable prefix as the binaries, so it
# describes exactly these artifacts and not whatever 'latest' pointed at a moment later.
fetch "$BASE/$VERSION/SHA256SUMS" "$tmp/SHA256SUMS"

# Verify who produced the checksum file before trusting anything it says. Fails closed:
# with a key in hand, a missing, malformed or wrong signature, or no openssl to check it
# with, stops the install. There is deliberately no switch to skip this.
if [ -n "$PUBKEY_FILE" ]; then
  [ -r "$PUBKEY_FILE" ] || die "SUPERVIBE_RELEASE_PUBKEY_FILE=$PUBKEY_FILE is not readable"
  cp "$PUBKEY_FILE" "$tmp/release-key.pem"
elif [ -n "$RELEASE_PUBKEY" ]; then
  printf '%s\n' "$RELEASE_PUBKEY" > "$tmp/release-key.pem"
fi
if [ -f "$tmp/release-key.pem" ]; then
  command -v openssl >/dev/null 2>&1 \
    || die "need openssl to verify the release signature; refusing to install unverified binaries"
  fetch "$BASE/$VERSION/SHA256SUMS.sig" "$tmp/SHA256SUMS.sig"
  openssl dgst -sha256 -verify "$tmp/release-key.pem" -signature "$tmp/SHA256SUMS.sig" \
    "$tmp/SHA256SUMS" >/dev/null 2>&1 \
    || die "the signature on SHA256SUMS does not verify against the pinned release key -- not installing"
  echo "Release signature verified."
fi

for bin in supervibe supervibe-mcp; do
  fetch "$BASE/$VERSION/${bin}_${os}_${arch}" "$tmp/${bin}_${os}_${arch}"
done

# Verify before anything is made executable or moved onto the PATH. A publish that was
# interrupted, a truncated transfer and a tampered object all look the same here, and all
# three should stop the install.
if command -v sha256sum >/dev/null 2>&1; then
  SUM="sha256sum -c"
elif command -v shasum >/dev/null 2>&1; then
  SUM="shasum -a 256 -c"
else
  die "need sha256sum or shasum to verify the download; refusing to install unverified binaries"
fi

(
  cd "$tmp"
  # Check only the two files for this platform; SHA256SUMS covers every platform.
  grep -E "_${os}_${arch}\$" SHA256SUMS > wanted.txt \
    || die "SHA256SUMS lists nothing for ${os}/${arch}"
  $SUM wanted.txt >/dev/null 2>&1 || die "checksum mismatch -- not installing"
)

echo "Checksums verified."

chmod +x "$tmp/supervibe_${os}_${arch}" "$tmp/supervibe-mcp_${os}_${arch}"

mkdir -p "$BINDIR" 2>/dev/null || die "could not create $BINDIR; set SUPERVIBE_BIN_DIR to a directory you can write"
[ -w "$BINDIR" ] || die "$BINDIR is not writable; set SUPERVIBE_BIN_DIR to a directory you can write"
mv "$tmp/supervibe_${os}_${arch}"     "$BINDIR/supervibe"
mv "$tmp/supervibe-mcp_${os}_${arch}" "$BINDIR/supervibe-mcp"

# The platform used to be called Platvibe. Agents' MCP configs and people's habits still name
# platvibe and platvibe-mcp, so those keep working as links to the new binaries. Remove once
# nobody runs the old names.
ln -sf supervibe     "$BINDIR/platvibe"
ln -sf supervibe-mcp "$BINDIR/platvibe-mcp"

echo
echo "Installed:"
echo "  $BINDIR/supervibe"
echo "  $BINDIR/supervibe-mcp"
echo "  (and platvibe, platvibe-mcp: the old names, linked to the new ones)"
echo
case ":$PATH:" in
  *":$BINDIR:"*)
    # An older copy earlier on the PATH -- /usr/local/bin, from before this installed
    # per-user -- would keep running instead, and look like the update did nothing.
    found=$(command -v supervibe 2>/dev/null || true)
    if [ -n "$found" ] && [ "$found" != "$BINDIR/supervibe" ]; then
      echo "Note: $found comes first on your PATH and will run instead. Remove it:"
      echo "  rm -f $found $(dirname "$found")/supervibe-mcp"
      echo
    fi
    ;;
  *)
    case "${SHELL:-}" in
      */zsh)  rc="$HOME/.zshrc" ;;
      */bash) rc="$HOME/.bashrc" ;;
      *)      rc="your shell's startup file" ;;
    esac
    echo "Note: $BINDIR is not on your PATH. Add it, then open a new terminal:"
    echo "  echo 'export PATH=\"$BINDIR:\$PATH\"' >> $rc"
    echo
    ;;
esac
echo "Next:"
echo "  supervibe login"
echo
echo "If your platform is not served from apps.supersidestaging.com, set SUPERVIBE_DOMAIN first:"
echo "  export SUPERVIBE_DOMAIN=<your platform domain>"
