#!/usr/bin/env bash
# bootstrap/macos.sh — first-run setup on a fresh macOS box.
# Idempotent: safe to run multiple times.
set -euo pipefail
if [[ "$OSTYPE" != darwin* ]]; then
printf 'macos.sh: this script is macOS-only\n' >&2
exit 1
fi
# 1) Xcode CLI tools
if ! xcode-select -p >/dev/null 2>&1; then
printf '== installing xcode command-line tools\n'
xcode-select --install || true
printf ' rerun this script after the installer finishes.\n'
exit 0
fi
# 2) Homebrew
if ! command -v brew >/dev/null 2>&1; then
printf '== installing homebrew\n'
NONINTERACTIVE=1 /bin/bash -c \
"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Apple Silicon default path
[[ -x /opt/homebrew/bin/brew ]] && eval "$(/opt/homebrew/bin/brew shellenv)"
[[ -x /usr/local/bin/brew ]] && eval "$(/usr/local/bin/brew shellenv)"
fi
# 3) Brew bundle
here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
printf '== brew bundle\n'
brew bundle --file="$here/Brewfile"
# 4) GNU stow (installed via Brewfile). Link packages into $HOME.
printf '== stow packages\n'
repo_root="$(cd "$here/.." && pwd)"
for pkg in zsh nvim tmux git bin macos; do
if [[ -d "$repo_root/$pkg" ]]; then
(cd "$repo_root" && stow --no-folding --target="$HOME" --restow "$pkg")
fi
done
# 5) Apply defaults
printf '== macos defaults\n'
for script in "$repo_root"/macos/defaults/*.sh; do
bash "$script"
done
printf '\nDone. Open a new shell to pick up the zsh config.\n'