#!/usr/bin/env bash
# Installs the relay CLI into ~/.relay/bin and links it from ~/.local/bin.
set -euo pipefail
BASE="${RELAY_BASE_URL:-https://relay.tinyclaw.online}"
DIR="$HOME/.relay/bin"
BIN="$HOME/.local/bin"

if ! command -v node >/dev/null 2>&1; then
  echo "relay needs Node.js 18 or newer: https://nodejs.org" >&2; exit 1
fi
major=$(node -p 'process.versions.node.split(".")[0]')
if [ "$major" -lt 18 ]; then echo "relay needs Node.js 18+ (found $(node -v))" >&2; exit 1; fi

mkdir -p "$DIR" "$BIN"
curl -fsSL "$BASE/cli/relay.mjs" -o "$DIR/relay.mjs.tmp"
mv "$DIR/relay.mjs.tmp" "$DIR/relay.mjs"
chmod +x "$DIR/relay.mjs"
ln -sf "$DIR/relay.mjs" "$BIN/relay"

echo "✓ relay installed to $BIN/relay"
case ":$PATH:" in *":$BIN:"*) ;; *) echo "  add to your shell profile:  export PATH=\"\$HOME/.local/bin:\$PATH\"";; esac
echo "  next: relay login"
