#!/bin/bash
# NexusOS installer, macOS. One painless command:
#
#   git clone <repo> nexus-core && cd nexus-core && ./install-macos.sh
#
# Mirrors install.sh's philosophy: every portable step - git pull, venv, pip
# with the right overlay, npm build - lives in bin/sync.py, shared with Linux
# and Windows. This script only does what sync.py can't do for itself on a
# bare Mac: install the Homebrew packages needed before Python even exists to
# run sync.py with. Re-run any time to update; --check dry-runs it.
#
# Ollama itself is *not* fetched here - bin/fetch-ollama.sh only ships a Linux
# x86-64 binary, and linux_stage() in bin/sync.py already no-ops on macOS, so
# that stage is skipped entirely. The Homebrew `ollama` installed below is
# picked up automatically instead: synapse/ollama_manager.py prefers the
# bundled Linux binary and falls back to whatever `ollama` it finds on PATH,
# which on macOS is this one - with full Metal GPU acceleration, no flags
# needed. The XFCE desktop branding (theme/panel/splash) is Linux-only and
# already gated off macOS the same way; nothing to install for it here.
set -euo pipefail
cd "$(dirname "$0")"

if ! command -v brew >/dev/null; then
    echo "Homebrew is required (it installs Python/Node/git/Ollama)." >&2
    echo "Install it, then re-run this script:  https://brew.sh" >&2
    exit 1
fi

echo "Installing/checking system packages (python@3.12, node, git, ollama)..."
brew install python@3.12 node git ollama

py="$(brew --prefix python@3.12)/bin/python3.12"
if [ ! -x "$py" ]; then
    echo "python3.12 not found at $py after brew install - check 'brew doctor'." >&2
    exit 1
fi

# Prefer the venv interpreter once it exists, same as install.sh; the brewed
# interpreter above is only the bootstrap case on a fresh clone. sync.py is
# stdlib-only either way.
[ -x "Promethean/bin/python" ] && py="Promethean/bin/python"

exec "$py" bin/sync.py restore "$@"
