#!/bin/sh
# PLOMID-MCP source installer.
#
# Builds PLOMID-MCP from source with the Rust toolchain, then installs the
# binary for the current user. Read this file before you run it — it is served
# from the PLOMID website so it can be inspected without cloning the project.
#
#   curl -fsSL https://plomid.in/plomid-mcp-install.sh | sh
#
# What it does:
#   1. Detects macOS or Linux and the host architecture.
#   2. Clones github.com/PLOMID/plomid-mcp at the requested ref (main by default).
#   3. Builds with `cargo build --release --locked`.
#   4. Installs `plomid-mcp` to PLOMID_MCP_PREFIX, or ~/.local/bin.
#   5. Prints the configuration and launch commands. It never starts the server.
#
# Requirements:
#   - git
#   - a Rust toolchain installed with rustup (https://rustup.rs)
#
# What it does NOT do:
#   - No system packages, sudo, shell-profile edits or PATH changes.
#   - No Windows support. Use the Windows package on the GitHub releases page,
#     or build from source in a native shell.
#   - No tagged-release download. This is a source build; review the ref first.
#
# Options:
#   --ref REF       Branch or tag to build (default: main).
#   --prefix DIR    Install directory (default: $HOME/.local/bin).
#   --dry-run       Print the plan without cloning or building.
#   --help          Show this help.
#
# Environment:
#   PLOMID_MCP_REPO=owner/repo   Override the repository.
#   PLOMID_MCP_REF=REF           Same as --ref.
#   PLOMID_MCP_PREFIX=DIR        Same as --prefix.
#
# Exit codes: 0 installed (or dry-run), 1 usage/error, 2 unsupported platform.

set -eu

REPO="${PLOMID_MCP_REPO:-PLOMID/plomid-mcp}"
REF="${PLOMID_MCP_REF:-main}"
PREFIX="${PLOMID_MCP_PREFIX:-$HOME/.local/bin}"
DRY_RUN=0

log() { printf '%s\n' "$*"; }
step() { printf '▸ %s\n' "$*"; }
ok() { printf '✓ %s\n' "$*"; }
warn() { printf 'plomid-mcp-install: %s\n' "$*" >&2; }
die() { printf '✗ plomid-mcp-install: %s\n' "$*" >&2; exit 1; }

usage() {
  cat <<'EOF'
PLOMID-MCP source installer

Usage:
  sh plomid-mcp-install.sh [options]

Options:
  --ref REF       Branch or tag to build (default: main)
  --prefix DIR    Install directory (default: $HOME/.local/bin)
  --dry-run       Print the plan without cloning or building
  -h, --help      Show this help

Environment:
  PLOMID_MCP_REPO=owner/repo
  PLOMID_MCP_REF=REF
  PLOMID_MCP_PREFIX=DIR

The script builds from source and installs one binary. It never starts the
server and never edits your shell profile. A running PLOMID database is still
required; set PLOMID_DATABASE_URL after installation.
EOF
}

while [ "$#" -gt 0 ]; do
  case "$1" in
    -h|--help) usage; exit 0 ;;
    --dry-run) DRY_RUN=1; shift ;;
    --ref) REF="${2:?--ref needs a value}"; shift 2 ;;
    --ref=*) REF="${1#--ref=}"; shift ;;
    --prefix) PREFIX="${2:?--prefix needs a value}"; shift 2 ;;
    --prefix=*) PREFIX="${1#--prefix=}"; shift ;;
    --) shift; break ;;
    -*) die "unknown flag: $1 (see --help)" ;;
    *) die "unexpected argument: $1 (see --help)" ;;
  esac
done

[ -n "$REF" ] || die "ref must not be empty"
[ -n "$PREFIX" ] || die "prefix must not be empty"

OS="$(uname -s 2>/dev/null || echo unknown)"
ARCH="$(uname -m 2>/dev/null || echo unknown)"

case "$OS" in
  Darwin)
    case "$ARCH" in
      arm64|aarch64) TARGET="aarch64-apple-darwin" ;;
      x86_64|amd64) TARGET="x86_64-apple-darwin" ;;
      *) die "unsupported macOS architecture: $ARCH" ;;
    esac
    ;;
  Linux)
    case "$ARCH" in
      x86_64|amd64) TARGET="x86_64-unknown-linux-gnu" ;;
      arm64|aarch64) TARGET="aarch64-unknown-linux-gnu" ;;
      *) die "unsupported Linux architecture: $ARCH" ;;
    esac
    ;;
  MINGW*|MSYS*|CYGWIN*|Windows*)
    warn "this script does not install on Windows."
    warn "Use the Windows package on https://github.com/${REPO}/releases"
    warn "or build from source in a native Windows shell."
    exit 2
    ;;
  *)
    die "unsupported OS: $OS (macOS and Linux only)"
    ;;
esac

if [ "$DRY_RUN" = "1" ]; then
  log "PLOMID-MCP source installer · dry run"
  log "  repository: https://github.com/${REPO}"
  log "  ref:        ${REF}"
  log "  platform:   ${OS} / ${ARCH} (${TARGET})"
  log "  install:    ${PREFIX}/plomid-mcp"
  log "  commands:   git clone --depth 1 --branch ${REF} …"
  log "              cargo build --release --locked"
  log "              install -m 0755 target/release/plomid-mcp ${PREFIX}/plomid-mcp"
  exit 0
fi

command -v git >/dev/null 2>&1 || die "git is required but was not found"
command -v cargo >/dev/null 2>&1 || die "cargo is required but was not found; install Rust from https://rustup.rs"

TMP_DIR="$(mktemp -d "${TMPDIR:-/tmp}/plomid-mcp.XXXXXX")"
cleanup() { rm -rf "$TMP_DIR"; }
trap cleanup EXIT HUP INT TERM

log "PLOMID-MCP source installer"
log "  ${OS} / ${ARCH} · ${TARGET}"
log "  ${REPO} @ ${REF}"
log ""

step "Cloning source"
git clone --depth 1 --branch "$REF" "https://github.com/${REPO}.git" "$TMP_DIR/src" \
  || die "could not clone ${REPO} at ${REF}"

step "Building release binary"
(
  cd "$TMP_DIR/src"
  cargo build --release --locked
) || die "cargo build failed; review the output above"

BINARY="$TMP_DIR/src/target/release/plomid-mcp"
[ -x "$BINARY" ] || die "build did not produce $BINARY"

step "Installing to $PREFIX"
mkdir -p "$PREFIX" || die "could not create $PREFIX"
install -m 0755 "$BINARY" "$PREFIX/plomid-mcp" || die "could not install to $PREFIX"

ok "installed $PREFIX/plomid-mcp"
"$PREFIX/plomid-mcp" --version || warn "installed binary did not report a version"

case ":$PATH:" in
  *":$PREFIX:"*) ;;
  *) warn "$PREFIX is not on PATH; add it with: export PATH=\"$PREFIX:\$PATH\"" ;;
esac

cat <<EOF

Next:
  export PLOMID_DATABASE_URL='postgresql://USER:PASSWORD@127.0.0.1:5432/DATABASE'
  plomid-mcp validate
  plomid-mcp stdio

For Streamable HTTP:
  plomid-mcp http --bind 127.0.0.1:8080

The database must already be running and reachable. This script installs the
adapter only; it does not bundle or start PLOMID.
EOF
