#!/bin/sh
# Agenteria installer.
#
#   curl -fsSL https://agenteria.henrywhittle.com/install.sh | sh
#
# Clones (or updates) the repo under ~/.agenteria and runs install.py, which
# adds the Claude Code hooks, builds the menu bar app, and registers login
# agents. Nothing needs sudo and nothing is written outside your home directory.
#
# Read before you pipe this into a shell. It is short on purpose.
set -eu

REPO="https://github.com/Whittle777/agenteria.git"
DEST="${AGENTERIA_HOME:-$HOME/.agenteria}"

say() { printf '\033[1m%s\033[0m\n' "$*"; }
die() { printf '\033[31m%s\033[0m\n' "$*" >&2; exit 1; }

[ "$(uname -s)" = "Darwin" ] || die "Agenteria is macOS only (found $(uname -s))."
command -v git >/dev/null 2>&1 || die "git not found. Install the Command Line Tools: xcode-select --install"
command -v python3 >/dev/null 2>&1 || die "python3 not found. Install the Command Line Tools: xcode-select --install"
# Being on PATH is not the same as working. On a Mac without the Command Line
# Tools, /usr/bin/python3 is a placeholder that opens an installer and fails,
# and it satisfies the check above. Better to say so here than to fail later
# inside the installer with something cryptic.
python3 -c "" >/dev/null 2>&1 || die "python3 is present but not usable. On a new Mac it is a placeholder until the Command Line Tools are installed:
    xcode-select --install
Then run this again."

if [ ! -x /usr/bin/swiftc ] && ! command -v swiftc >/dev/null 2>&1; then
  say "Note: swiftc not found, so the menu bar app cannot be built."
  say "      Run 'xcode-select --install' first if you want the ovens."
fi

if [ -d "$DEST/.git" ]; then
  say "Updating $DEST"
  git -C "$DEST" pull --ff-only --quiet
else
  say "Cloning into $DEST"
  git clone --quiet --depth 1 "$REPO" "$DEST"
fi

say "Installing"
cd "$DEST"
python3 install.py

printf '\n'
say "Done. Look for the ovens in your menu bar."
say "Uninstall any time:  python3 $DEST/install.py --uninstall"
