#!/usr/bin/env bash
# Narada source-bootstrap installer for macOS and Linux.
#
#   curl -fsSL https://narada.systems/install-source.sh | bash
#
# What it does, in order:
#   1. Verifies git, Node.js (>= 18; 22+ recommended), and pnpm (via corepack if missing).
#   2. Clones https://github.com/narada-core/narada plus the four sibling repos
#      its pnpm workspace spans (narada-core, mcp-surfaces, agent-cli, agent-tui)
#      — a lone clone of narada cannot install (ERR_PNPM_WORKSPACE_PKG_NOT_FOUND).
#      Repos already present are pulled, not re-cloned.
#   3. Runs pnpm install and pnpm build.
#   4. Installs the `narada` and `narada-mcp` shims into ~/.local/bin.
#
# Override the narada checkout location with NARADA_SRC (siblings land next to
# it). This script is plain text served from narada.systems — read it before
# you pipe it.

set -euo pipefail

SRC_DIR="${NARADA_SRC:-$HOME/src/narada}"
PARENT_DIR="$(dirname "$SRC_DIR")"

step() { printf '\n==> %s\n' "$1"; }

sync_repo() {
  # sync_repo <git-url> <dir>
  if [ -d "$2/.git" ]; then
    git -C "$2" pull --ff-only
  else
    git clone "$1" "$2"
  fi
}

step 'Checking prerequisites'

command -v git >/dev/null 2>&1 || {
  echo 'git is required but was not found on PATH.' >&2
  exit 1
}

command -v node >/dev/null 2>&1 || {
  echo 'Node.js is required but was not found on PATH. Install Node.js 22 LTS: https://nodejs.org' >&2
  exit 1
}
NODE_MAJOR="$(node --version | sed 's/^v//' | cut -d. -f1)"
if [ "$NODE_MAJOR" -lt 18 ]; then
  echo "Node.js >= 18 is required; found v$NODE_MAJOR. Install Node.js 22 LTS: https://nodejs.org" >&2
  exit 1
fi
if [ "$NODE_MAJOR" -lt 22 ]; then
  echo "    note: Node.js v$NODE_MAJOR works for the CLI; the User Site path expects 22+."
fi

if ! command -v pnpm >/dev/null 2>&1; then
  step 'Installing pnpm via corepack'
  command -v corepack >/dev/null 2>&1 || {
    echo 'pnpm is missing and corepack is unavailable. Install pnpm manually: https://pnpm.io/installation' >&2
    exit 1
  }
  corepack enable
  corepack prepare pnpm@latest --activate
fi

step "Fetching Narada and sibling workspace repos into $PARENT_DIR"
if [ "$(basename "$SRC_DIR")" != "narada" ]; then
  echo "    warning: NARADA_SRC should end in 'narada' — some sibling builds"
  echo "    (agent-tui) resolve contracts via a sibling dir literally named 'narada'."
fi
sync_repo https://github.com/narada-core/narada.git "$SRC_DIR"
sync_repo https://github.com/narada-core/narada-core.git "$PARENT_DIR/narada-core"
sync_repo https://github.com/narada-core/mcp-surfaces.git "$PARENT_DIR/mcp-surfaces"
sync_repo https://github.com/narada-core/agent-cli.git "$PARENT_DIR/agent-cli"
sync_repo https://github.com/narada-core/agent-tui.git "$PARENT_DIR/agent-tui"

step 'Installing workspace dependencies (this takes a while on first run)'
pnpm --dir "$SRC_DIR" install

step 'Building Narada'
pnpm --dir "$SRC_DIR" build

step 'Installing shims into ~/.local/bin'
pnpm --dir "$SRC_DIR" narada:install-shim

case ":$PATH:" in
  *":$HOME/.local/bin:"*) ;;
  *) echo "    note: add ~/.local/bin to your PATH (e.g. in ~/.profile or ~/.bashrc)." ;;
esac

step 'Done'
cat <<EOF
Narada is installed from source at $SRC_DIR.

Try the zero-setup demo:
  narada demo

Or declare your first governed operation:
  narada init-repo ~/src/my-ops

To update later, re-run this installer — it pulls every repo and rebuilds.
EOF
