#!/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

# Which `chirp` will the user's shell actually run? npm installs into `npm prefix -g`, but that
# bin dir is not always on PATH (Homebrew's node@20 keg lives under /opt/homebrew/Cellar), and an
# older chirp-host from a second prefix can sit in front of it — that copy then answers every
# `chirp` command with stale behaviour (0.2.x printed "Signed in" without saving credentials, and
# `chirp start` then said "Not signed in"; a clean Mac hit exactly this on 2026-09-16). So run the
# wizard through the copy just installed, and say so when the shell would pick another.
chirp_real() {  # canonical path of the script behind a chirp binary
  node -e 'try { console.log(require("fs").realpathSync(process.argv[1])) } catch { console.log(process.argv[1]) }' "$1"
}
chirp_version() {  # the chirp-host version behind a chirp binary (package.json sits above bin/)
  node -e '
    const fs = require("fs"), path = require("path");
    try { console.log(JSON.parse(fs.readFileSync(path.join(path.dirname(fs.realpathSync(process.argv[1])), "..", "package.json"), "utf8")).version) }
    catch { console.log("unknown") }
  ' "$1"
}
chirp_prefix() {  # the npm prefix a chirp binary was installed into (<prefix>/lib/node_modules/chirp-host/bin/chirp.js)
  node -e 'console.log(require("path").resolve(require("path").dirname(require("fs").realpathSync(process.argv[1])), "../../../.."))' "$1"
}
PREFIX="$(npm prefix -g)"
INSTALLED="$PREFIX/bin/chirp"
INSTALLED_VERSION="$(chirp_version "$INSTALLED")"
FOUND="$(command -v chirp || true)"
CHIRP="chirp"  # how the manual commands below name the binary
if [ -z "$FOUND" ]; then
  CHIRP="$INSTALLED"
  echo ""
  echo "Installed chirp-host $INSTALLED_VERSION — but $PREFIX/bin is not on your PATH, so plain \`chirp\` will not work yet."
  echo "Add it, then open a new terminal:"
  echo "  echo 'export PATH=\"$PREFIX/bin:\$PATH\"' >> ~/.zshrc"
elif [ "$(chirp_real "$FOUND")" != "$(chirp_real "$INSTALLED")" ]; then
  CHIRP="$INSTALLED"
  OLD_PREFIX="$(chirp_prefix "$FOUND")"
  echo ""
  echo "WARNING: your shell runs a different chirp than the one just installed."
  echo "  chirp                   → $FOUND (chirp-host $(chirp_version "$FOUND"))"
  echo "  the copy just installed → $INSTALLED (chirp-host $INSTALLED_VERSION)"
  echo "Until this is fixed, \`chirp login\` and \`chirp start\` in your shell use the old copy. Either:"
  echo "  npm uninstall -g --prefix \"$OLD_PREFIX\" chirp-host      # remove the old copy"
  echo "  echo 'export PATH=\"$PREFIX/bin:\$PATH\"' >> ~/.zshrc     # or put the new one first"
else
  echo "Installed chirp-host $INSTALLED_VERSION at $INSTALLED"
fi

# `[ -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
  "$INSTALLED" 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 "No terminal available for guided setup — run it yourself:"
  echo "  $CHIRP setup"
  echo "Or step by step:"
  next_steps
fi
