#!/bin/sh
# Installs the Meridian connector (meridian-mcp) for your coding agent.
#
#   curl -fsSL https://resolvingarchitecture.io/meridian/install.sh | sh
#
# What it does, and nothing else:
#   1. picks the build for this machine (Linux x86_64, macOS Apple silicon or Intel);
#   2. downloads it and checks it against the published SHA256SUMS, and stops if they differ;
#   3. installs it for your user only (no sudo): ~/.local/share/meridian/meridian-mcp, linked from ~/.local/bin;
#   4. runs `meridian-mcp install`, which adds it to the agents it finds (Claude Code, Claude Desktop, Cursor,
#      VS Code). Only the "meridian" entry is added; the original file is saved once as <file>.meridian-backup.
#
# Settings (environment variables, all optional):
#   MERIDIAN_INSTALL_DIR  where the program goes        (default ~/.local/share/meridian)
#   MERIDIAN_BIN_DIR      where the link goes           (default ~/.local/bin)
#   MERIDIAN_NO_CONFIG=1  install the program but do not touch any agent's configuration
#   MERIDIAN_BASE_URL     where to download from        (default https://resolvingarchitecture.io)
#
# To remove it: meridian-mcp install --remove, then delete the install folder and the link.
set -eu

BASE="${MERIDIAN_BASE_URL:-https://resolvingarchitecture.io}"
DEST="${MERIDIAN_INSTALL_DIR:-$HOME/.local/share/meridian}"
BIN_DIR="${MERIDIAN_BIN_DIR:-$HOME/.local/bin}"

say() { printf '%s\n' "$*"; }
die() { printf 'meridian install: %s\n' "$*" >&2; exit 1; }

fetch() { # fetch URL FILE
  if command -v curl >/dev/null 2>&1; then
    curl -fsSL -o "$2" "$1" || die "could not download $1"
  elif command -v wget >/dev/null 2>&1; then
    wget -qO "$2" "$1" || die "could not download $1"
  else
    die "needs curl or wget"
  fi
}

sha256() {
  if command -v sha256sum >/dev/null 2>&1; then
    sha256sum "$1" | awk '{print $1}'
  elif command -v shasum >/dev/null 2>&1; then
    shasum -a 256 "$1" | awk '{print $1}'
  else
    die "needs sha256sum or shasum to check the download"
  fi
}

case "$(uname -s)-$(uname -m)" in
  Linux-x86_64)                label="linux-x86_64" ;;
  Darwin-arm64)                label="macos-arm64" ;;
  Darwin-x86_64)               label="macos-x86_64" ;;
  *) die "no build for $(uname -s) $(uname -m). Windows: use install.ps1. Other systems: see $BASE/meridian/#download" ;;
esac

TMP="$(mktemp -d)"
trap 'rm -rf "$TMP"' EXIT

fetch "$BASE/meridian/downloads/mcp/LATEST" "$TMP/LATEST"
VERSION="$(tr -d '[:space:]' < "$TMP/LATEST")"
case "$VERSION" in
  ""|*[!0-9A-Za-z.+-]*) die "the published version is not usable: '$VERSION'" ;;
esac
NAME="meridian-mcp-$label.tar.gz"
URL="$BASE/meridian/downloads/mcp/$VERSION"

say "Meridian connector $VERSION for $label"
fetch "$URL/SHA256SUMS" "$TMP/SHA256SUMS"
fetch "$URL/$NAME" "$TMP/$NAME"

EXPECTED="$(awk -v n="$NAME" '$2 == n {print $1}' "$TMP/SHA256SUMS")"
[ -n "$EXPECTED" ] || die "$NAME is not in SHA256SUMS"
ACTUAL="$(sha256 "$TMP/$NAME")"
[ "$EXPECTED" = "$ACTUAL" ] || die "the download does not match SHA256SUMS, so nothing was installed"
say "Checked against SHA256SUMS: ok"

tar -xzf "$TMP/$NAME" -C "$TMP"
BIN="$(find "$TMP" -type f -name meridian-mcp | head -n 1)"
[ -n "$BIN" ] || die "the package does not contain meridian-mcp"

mkdir -p "$DEST" "$BIN_DIR"
cp "$BIN" "$DEST/meridian-mcp.new"
chmod +x "$DEST/meridian-mcp.new"
# A file fetched with curl is not quarantined; clear the flag anyway in case a browser or another tool added it.
if [ "$(uname -s)" = "Darwin" ]; then xattr -d com.apple.quarantine "$DEST/meridian-mcp.new" 2>/dev/null || true; fi
mv -f "$DEST/meridian-mcp.new" "$DEST/meridian-mcp"
ln -sf "$DEST/meridian-mcp" "$BIN_DIR/meridian-mcp"
say "Installed: $DEST/meridian-mcp (linked from $BIN_DIR/meridian-mcp)"

if [ "${MERIDIAN_NO_CONFIG:-}" = "1" ]; then
  say "Skipped your agent's configuration (MERIDIAN_NO_CONFIG=1). To add it later: $DEST/meridian-mcp install"
else
  say ""
  "$DEST/meridian-mcp" install || say "Could not set up your agent automatically. Run: $DEST/meridian-mcp install"
fi

case ":$PATH:" in
  *":$BIN_DIR:"*) ;;
  *) say ""; say "Note: $BIN_DIR is not on your PATH. The agent uses the full path, so it still works; add it to run meridian-mcp yourself." ;;
esac
