#!/bin/bash
# DeepBlueish bootstrap — sets up a fresh Mac from nothing.
# Double-clicking this file opens Terminal and runs it.
# First run on a new Mac: right-click the file → Open (it's an unsigned script,
# so macOS warns once).
set -e

echo "🐋 DeepBlueish setup"
echo

# 1. Homebrew (the package manager everything else comes from)
if ! command -v brew >/dev/null 2>&1 && [ ! -x /opt/homebrew/bin/brew ]; then
  echo "→ Installing Homebrew (you may be asked for your Mac password)…"
  /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi
eval "$(/opt/homebrew/bin/brew shellenv 2>/dev/null || /usr/local/bin/brew shellenv)"

# 2. The three tools: Node (runs the bridge), gh (GitHub login), Stockfish (the engine)
echo "→ Installing node, gh, stockfish…"
brew install node gh stockfish

# 3. Sign in to GitHub (the repo is private — sign in as jeffreyashton)
if ! gh auth status >/dev/null 2>&1; then
  echo "→ Signing in to GitHub — follow the prompts (browser login is easiest)…"
  gh auth login
fi

# 4. Get (or update) DeepBlueish
if [ -d "$HOME/DeepBlueish/.git" ]; then
  echo "→ Updating existing copy…"
  git -C "$HOME/DeepBlueish" pull --rebase
else
  echo "→ Cloning DeepBlueish…"
  gh repo clone jeffreyashton/DeepBlueish "$HOME/DeepBlueish"
fi

# 5. Start the bridge and open the dashboard
echo
echo "✅ All set. Starting the bridge — leave this window open while you use it."
( sleep 2 && open "http://localhost:4002" ) &
cd "$HOME/DeepBlueish" && node bridge/server.js
