#!/usr/bin/env bash # Clutch Agent — macOS / Linux bootstrap (the "lil thing" you run to connect a box). # Served by Clutch at: https://clutch.crutchtech.com/api/collab/cc/connect.sh # One-paste connect (Clutch fills in your token on the Devices page): # BRIDGE_TOKEN=cc_xxx CLUTCH_URL=https://clutch.crutchtech.com/api/collab/cc \ # bash -c "$(curl -fsSL https://clutch.crutchtech.com/api/collab/cc/connect.sh)" # Add CLUTCH_SERVICE=1 to install as a persistent service (systemd/launchd). set -euo pipefail TOKEN="${BRIDGE_TOKEN:-${CLUTCH_TOKEN:-}}" URL="${CLUTCH_URL:-https://clutch.crutchtech.com/api/collab/cc}" SERVICE="${CLUTCH_SERVICE:-0}" [ -n "$TOKEN" ] || { echo "[clutch] Set BRIDGE_TOKEN first (copy the one-liner from Clutch > Devices > Add a device)." >&2; exit 1; } case "$TOKEN" in cc_*) : ;; *) echo "[clutch] That token should start with cc_." >&2; exit 1;; esac command -v node >/dev/null 2>&1 || { echo "[clutch] Node.js 18+ required and not found. Install node, then re-run." >&2; exit 1; } command -v curl >/dev/null 2>&1 || { echo "[clutch] curl required." >&2; exit 1; } echo "[clutch] node $(node -v) OK" DIR="$HOME/.clutch-agent" mkdir -p "$DIR" AGENT="$DIR/clutch-device-agent.mjs" echo "[clutch] downloading agent -> $AGENT" curl -fsSL "$URL/agent.mjs" -o "$AGENT" run_foreground() { echo "[clutch] connecting this machine to Clutch ($URL) ... (Ctrl+C to stop)" exec node "$AGENT" --token "$TOKEN" --url "$URL" } install_systemd() { local unit="$HOME/.config/systemd/user/clutch-agent.service" mkdir -p "$(dirname "$unit")" cat > "$unit" </dev/null 2>&1 || true echo "[clutch] installed systemd user service 'clutch-agent' and started. Reconnects on boot." } install_launchd() { local plist="$HOME/Library/LaunchAgents/com.clutch.agent.plist" mkdir -p "$(dirname "$plist")" cat > "$plist" < Labelcom.clutch.agent ProgramArguments $(command -v node)$AGENT --token$TOKEN --url$URL EnvironmentVariables BRIDGE_TOKEN$TOKEN CLUTCH_URL$URL RunAtLoad KeepAlive EOF launchctl unload "$plist" >/dev/null 2>&1 || true launchctl load "$plist" echo "[clutch] installed launchd agent 'com.clutch.agent' and started. Reconnects on login." } if [ "$SERVICE" = "1" ]; then if command -v systemctl >/dev/null 2>&1; then install_systemd elif [ "$(uname -s)" = "Darwin" ]; then install_launchd else echo "[clutch] no systemd/launchd; falling back to nohup background"; nohup node "$AGENT" --token "$TOKEN" --url "$URL" >"$DIR/agent.log" 2>&1 & echo "[clutch] started in background (pid $!). Log: $DIR/agent.log"; fi else run_foreground fi