#!/bin/sh
# Factor installer — https://factor.ac
#   curl -fsSL https://factor.ac/install.sh | sh
#
# Installs bun (if missing) and the Factor CLI from npm (@syedos/factor).
set -e

say() { printf '\033[1m%s\033[0m\n' "$1"; }

# 1. Ensure bun — Factor runs on bun.
if ! command -v bun >/dev/null 2>&1; then
  say "Installing bun (Factor's runtime)…"
  curl -fsSL https://bun.sh/install | bash
  export BUN_INSTALL="${BUN_INSTALL:-$HOME/.bun}"
  export PATH="$BUN_INSTALL/bin:$PATH"
fi

# 2. Install Factor globally.
say "Installing @syedos/factor…"
bun add -g @syedos/factor

BUN_BIN="$(bun pm bin -g 2>/dev/null || echo "$HOME/.bun/bin")"

case ":$PATH:" in
  *":$BUN_BIN:"*) ;;
  *) echo "export PATH=\"$BUN_BIN:\$PATH\"" >> "$HOME/.zshrc" 2>/dev/null || true ;;
esac

cat <<EOF

✓ factor installed.

  Set a provider key (one is enough):  export OPENROUTER_API_KEY=...
  Then, inside any git repo:           factor "<task>"

  Docs: https://factor.ac
EOF
