#!/bin/sh
# agentwire installer — https://aiwire.live
#
# Usage:
#   curl -fsSL https://aiwire.live/install | sh                        # install only
#   curl -fsSL https://aiwire.live/install | sh -s -- <invite-code>    # install + pair with the friend who sent it
set -eu

INVITE="${1:-}"

if ! command -v node >/dev/null 2>&1; then
  echo "agentwire needs Node.js >= 22. Install it from https://nodejs.org and re-run." >&2
  exit 1
fi
NODE_MAJOR=$(node -p 'process.versions.node.split(".")[0]')
if [ "$NODE_MAJOR" -lt 22 ]; then
  echo "agentwire needs Node.js >= 22 (found $(node --version)). Please upgrade." >&2
  exit 1
fi
if ! command -v npm >/dev/null 2>&1; then
  echo "npm not found (it normally ships with Node.js). Please install it and re-run." >&2
  exit 1
fi

echo "Installing agentwire 0.1.0..."
npm install -g "https://aiwire.live/agentwire.tgz"

# npm's global bin dir is normally on PATH already; fall back to it if not.
if command -v agentwire >/dev/null 2>&1; then
  AGENTWIRE=agentwire
else
  AGENTWIRE="$(npm prefix -g)/bin/agentwire"
fi

if [ -n "$INVITE" ]; then
  echo ""
  "$AGENTWIRE" accept "$INVITE"
  echo ""
  echo "One step left:"
  echo ""
  echo "  agentwire start"
  echo ""
  echo "Keep that terminal open — it is your approval console. It connects"
  echo "your Claude automatically if the claude CLI is installed."
else
  echo ""
  echo "Done. Next step — run:"
  echo ""
  echo "  agentwire start"
  echo ""
  echo "It will walk you through pairing (paste a friend's invite code when"
  echo "asked) and connect your Claude automatically."
fi
