#!/bin/sh
# Boat House: put the bh command on this machine, log it in, and teach Claude Code how to use it.
#   curl -fsSL https://boathousecloud.com/install.sh | sh                          install only
#   curl -fsSL https://boathousecloud.com/install.sh | sh -s -- bh_your_project_key  install and log in
set -e
P="boathousecloud.com"; KEY="$1"
PY=""
for c in python3 python; do
  if command -v "$c" >/dev/null 2>&1 && "$c" -c 'import sys; raise SystemExit(0 if sys.version_info >= (3, 9) else 1)' >/dev/null 2>&1; then PY="$c"; break; fi
done
if [ -z "$PY" ]; then
  if [ "$(uname 2>/dev/null)" = "Darwin" ]; then
    echo "This Mac has no working Python yet (it needs Apple's command line tools). Run:  xcode-select --install" >&2
    echo "Click Install in the dialog, wait a few minutes, then run this line again." >&2
  else
    echo "bh needs Python 3.9 or newer. Install it (for example: sudo apt install python3), then run this line again." >&2
  fi
  exit 1
fi
if command -v curl >/dev/null 2>&1; then GET="curl -fsSL"; elif command -v wget >/dev/null 2>&1; then GET="wget -qO-"; else echo "bh needs curl or wget to download itself." >&2; exit 1; fi
DIR="$HOME/.local/bin"; mkdir -p "$DIR"
$GET "https://$P/bh" > "$DIR/bh.tmp"
if ! head -c 40 "$DIR/bh.tmp" | grep -q '^#!/usr/bin/env python3'; then
  rm -f "$DIR/bh.tmp"; echo "the download did not look like the bh command (a captive portal or a proxy in the way?). Try again on a normal connection." >&2; exit 1
fi
if [ "$PY" != "python3" ]; then sed "1s|^#!/usr/bin/env python3|#!/usr/bin/env $PY|" "$DIR/bh.tmp" > "$DIR/bh.tmp2" && mv "$DIR/bh.tmp2" "$DIR/bh.tmp"; fi
mv "$DIR/bh.tmp" "$DIR/bh" && chmod +x "$DIR/bh"
echo "installed  $DIR/bh"
case ":$PATH:" in
  *":$DIR:"*) ;;
  *) touched=""
     for rc in "$HOME/.zshrc" "$HOME/.bashrc" "$HOME/.profile"; do
       if [ -f "$rc" ] && ! grep -q '.local/bin' "$rc" 2>/dev/null; then printf '
export PATH="$HOME/.local/bin:$PATH"
' >> "$rc"; touched="$touched $rc"; fi
     done
     if [ -z "$touched" ] && [ ! -f "$HOME/.zshrc" ] && [ ! -f "$HOME/.bashrc" ] && [ ! -f "$HOME/.profile" ]; then
       printf 'export PATH="$HOME/.local/bin:$PATH"
' > "$HOME/.zshrc"; cp "$HOME/.zshrc" "$HOME/.profile"; touched=" ~/.zshrc ~/.profile"
     fi
     echo "bh is in ~/.local/bin, which is not on your PATH yet. Added it to$touched."
     echo "Open a new terminal, or run now:  export PATH="\$HOME/.local/bin:\$PATH"" ;;
esac
set +e
mkdir -p "$HOME/.claude/skills/boathouse" 2>/dev/null
if $GET "https://$P/skill.md" > "$HOME/.claude/skills/boathouse/SKILL.md" 2>/dev/null; then
  echo "Claude Code skill installed (~/.claude/skills/boathouse); Claude Code reads it the next time it starts."
else
  echo "could not install the skill; later, run: bh skill" >&2
fi
if "$DIR/bh" trust on --quiet 2>/dev/null; then
  echo "Claude Code will run bh's everyday commands without asking (deploy, share, pull, logs); buying, paying, deleting and restoring still ask you. Undo: bh trust off"
fi
OK=1
if [ -n "$KEY" ]; then
  case "$KEY" in
    bh_*) "$DIR/bh" login "https://api.$P" "$KEY" || OK=0 ;;
    *)    "$DIR/bh" claim --api "https://api.$P" "$KEY" || OK=0 ;;
  esac
fi
echo
if [ -n "$KEY" ] && [ "$OK" = 1 ]; then
  echo "Done. Your agent can put things online now: 'bh deploy' in a tool's folder, 'bh ls' to see what is running."
  echo "Other agents (Cursor, Codex, anything that speaks MCP): https://mcp.$P/mcp with header  Authorization: Bearer <your project key, from your account page>"
elif [ -n "$KEY" ]; then
  echo "bh is installed but the sign-in did not stick (see the message above). Mint a fresh code on https://$P/account and run:" >&2
  echo "  bh claim <code>" >&2
else
  echo "Next: bh claim <code>   (the code is on your account page, https://$P/account; a raw key works too: bh login https://api.$P <key>)"
fi
echo "Everything bh can do, on one page: https://$P/docs"
