#!/bin/sh
# Install the Platvibe 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

# get.apps.supersidestaging.com is a placeholder, the same literal the manifests in platform/
# carry: publish:tools rewrites it 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 PLATVIBE_DOWNLOADS_HOST instead.
BASE="${PLATVIBE_DOWNLOAD_BASE:-https://get.apps.supersidestaging.com}"
VERSION="${PLATVIBE_VERSION:-latest}"
BINDIR="${PLATVIBE_BIN_DIR:-/usr/local/bin}"

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

os=$(uname -s | tr '[:upper:]' '[:lower:]')
case "$os" in
  darwin|linux) ;;
  *) die "unsupported OS: $os. The CLI is published for macOS and Linux; 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 platvibe ($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"

for bin in platvibe platvibe-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 all eight.
  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/platvibe_${os}_${arch}" "$tmp/platvibe-mcp_${os}_${arch}"

install_one() {
  src="$1"; dest="$2"
  if [ -w "$BINDIR" ]; then
    mv "$src" "$dest"
  else
    echo "$BINDIR is not writable; using sudo"
    sudo mv "$src" "$dest"
  fi
}

mkdir -p "$BINDIR" 2>/dev/null || true
install_one "$tmp/platvibe_${os}_${arch}"     "$BINDIR/platvibe"
install_one "$tmp/platvibe-mcp_${os}_${arch}" "$BINDIR/platvibe-mcp"

echo
echo "Installed:"
echo "  $BINDIR/platvibe"
echo "  $BINDIR/platvibe-mcp"
echo
case ":$PATH:" in
  *":$BINDIR:"*) ;;
  *) echo "Note: $BINDIR is not on your PATH." ; echo ;;
esac
echo "Next:"
echo "  platvibe login"
echo
echo "If your platform is not served from apps.superside.com, set PLATVIBE_DOMAIN first:"
echo "  export PLATVIBE_DOMAIN=apps.supersidestaging.com"
