#!/usr/bin/env sh
set -e

# CEO Brain CLI installer — pipes through your shell.
#   curl -sSL install.ceobrain.org | sh
#
# Override the version: CEOBRAIN_VERSION=cli-v0.1.0 curl -sSL install.ceobrain.org | sh

VERSION="${CEOBRAIN_VERSION:-latest}"
INSTALL_DIR="${HOME}/.ceo-brain/bin"
RELEASE_REPO="oliverpople/ceobrain-releases"

OS=$(uname -s | tr '[:upper:]' '[:lower:]')
case "$OS" in
  darwin|linux) ;;
  *)
    echo "  error: unsupported OS '$OS' (only darwin and linux are supported today)"
    echo "  windows users: please file an issue at https://github.com/oliverpople/ceobrain-releases/issues"
    exit 1 ;;
esac

ARCH=$(uname -m)
case "$ARCH" in
  arm64|aarch64) ARCH="arm64" ;;
  x86_64|amd64)  ARCH="x64" ;;
  *)
    echo "  error: unsupported architecture '$ARCH' (need arm64 or x64)"
    exit 1 ;;
esac

ASSET="ceo-${OS}-${ARCH}.tar.gz"
if [ "$VERSION" = "latest" ]; then
  URL_BASE="https://github.com/${RELEASE_REPO}/releases/latest/download"
else
  URL_BASE="https://github.com/${RELEASE_REPO}/releases/download/${VERSION}"
fi

echo ""
echo "❯ CEO_BRAIN — installing the CLI"
echo "  ────────────────────────────────"
echo "  Platform: ${OS}/${ARCH}"
echo "  Source:   ${URL_BASE}/${ASSET}"
echo ""

mkdir -p "$INSTALL_DIR"
TMPDIR=$(mktemp -d)
trap 'rm -rf "$TMPDIR"' EXIT

echo "  ▸ Downloading binary…"
if ! curl -fSL --progress-bar "${URL_BASE}/${ASSET}" -o "${TMPDIR}/${ASSET}"; then
  echo ""
  echo "  error: download failed."
  echo "  Releases page: https://github.com/${RELEASE_REPO}/releases"
  exit 1
fi

echo "  ▸ Downloading checksum…"
if curl -fSL "${URL_BASE}/${ASSET}.sha256" -o "${TMPDIR}/${ASSET}.sha256" 2>/dev/null; then
  echo "  ▸ Verifying sha256…"
  cd "$TMPDIR"
  if command -v sha256sum >/dev/null 2>&1; then
    sha256sum -c "${ASSET}.sha256" || { echo "  error: sha256 mismatch — refusing to install"; exit 1; }
  elif command -v shasum >/dev/null 2>&1; then
    shasum -a 256 -c "${ASSET}.sha256" || { echo "  error: sha256 mismatch — refusing to install"; exit 1; }
  else
    echo "  warning: no sha256sum/shasum on this system — skipping verification"
  fi
  cd - >/dev/null
else
  echo "  warning: checksum file not found on the release — skipping verification"
fi

echo "  ▸ Extracting…"
tar -xzf "${TMPDIR}/${ASSET}" -C "$TMPDIR"
mv "${TMPDIR}/ceo" "$INSTALL_DIR/ceo"
chmod +x "$INSTALL_DIR/ceo"

echo ""
echo "  ✓ Installed to $INSTALL_DIR/ceo"
echo ""

# PATH check.
if ! echo ":$PATH:" | grep -q ":$INSTALL_DIR:"; then
  SHELL_RC=""
  case "$(basename "${SHELL:-}")" in
    zsh)  SHELL_RC="${HOME}/.zshrc" ;;
    bash) SHELL_RC="${HOME}/.bashrc" ;;
  esac
  echo "  ⚠ $INSTALL_DIR is not on your PATH. Add this line to your shell rc:"
  echo ""
  echo "      export PATH=\"\$HOME/.ceo-brain/bin:\$PATH\""
  echo ""
  if [ -n "$SHELL_RC" ]; then
    echo "  Or run once now:"
    echo "      echo 'export PATH=\"\$HOME/.ceo-brain/bin:\$PATH\"' >> $SHELL_RC && exec $SHELL"
    echo ""
  fi
fi

echo "  Next:"
echo "    ceo login            — sign in via LinkedIn (~30s)"
echo "    ceo                  — see your status"
echo "    ceo --help           — full CLI reference"
echo ""
echo "  Optional supply-chain check (if you have cosign installed):"
echo "    https://github.com/${RELEASE_REPO}#verifying-releases"
echo ""
echo "  Or paste this into your CLI agent (Claude Code, Cursor, Codex):"
echo "    https://ceobrain.org/install/agent.txt"
echo ""
