#!/bin/sh
# Chirp desktop install — https://chirp.dev/docs/agents
# Bootstrap only: installs chirp-host, then hands off to the interactive `chirp setup` wizard.
# This half must NEVER prompt — under `curl | sh`, stdin is the script itself.
set -eu
[ "$(uname)" = "Darwin" ] || { echo "chirp currently supports macOS only"; exit 1; }
command -v node >/dev/null 2>&1 || { echo "Node.js 20+ required — install from https://nodejs.org or 'brew install node'"; exit 1; }
node -e 'process.exit(parseInt(process.versions.node.split(".")[0], 10) >= 20 ? 0 : 1)' || { echo "Node.js 20+ required (found $(node -v))"; exit 1; }
command -v tmux >/dev/null 2>&1 || { echo "tmux required — run: brew install tmux"; exit 1; }
echo "Installing chirp-host…"
npm install -g chirp-host
# `[ -r /dev/tty ]` passes even with no controlling terminal — actually opening it is the test.
# Either way the user must leave this script knowing the next command to type: the wizard prints
# its own next steps on every path it can take, and this half covers the two ways the wizard never
# gets to speak — no terminal to run it in, or a crash before its first line.
next_steps() {
  echo "  chirp login      sign in to your Chirp account"
  echo "  chirp start      start the background daemon"
  echo "  chirp status     check that your Mac is connected"
}
if ( : < /dev/tty ) 2>/dev/null; then
  chirp setup < /dev/tty || {
    echo ""
    echo "Guided setup did not finish. To finish by hand:"
    next_steps
    echo "Or run the guided setup again:  chirp setup"
    exit 1
  }
else
  echo ""
  echo "Installed chirp-host. No terminal available for guided setup — run it yourself:"
  echo "  chirp setup"
  echo "Or step by step:"
  next_steps
fi
