#!/bin/bash
# Axon Avenue PLT · Personal AI Agent preflight (macOS)
# READ-ONLY. It checks your Mac and prints what is missing.
# It never installs, changes, uploads or deletes anything.
#   curl -fsSL https://agent.axonavenue.my/preflight.sh | bash

set -u
PASS=0; TODO=0
ok()  { printf '  \033[32m✓\033[0m %s\n' "$1"; PASS=$((PASS+1)); }
no()  { printf '  \033[33m○\033[0m %s\n      \033[2m→ %s\033[0m\n' "$1" "$2"; TODO=$((TODO+1)); }
hdr() { printf '\n\033[1m%s\033[0m\n' "$1"; }

printf '\n\033[1mAxon Avenue · agent preflight\033[0m  \033[2mread-only · nothing is changed\033[0m\n'

if [ "$(uname -s)" != "Darwin" ]; then echo "This preflight checks macOS only."; exit 1; fi

hdr "Machine"
if [ "$(uname -m)" = "arm64" ]; then ok "Apple silicon · $(sysctl -n machdep.cpu.brand_string 2>/dev/null)"
else no "Intel Mac" "Works, but slower. Fine to continue."; fi
VER=$(sw_vers -productVersion); MAJOR=${VER%%.*}
if [ "$MAJOR" -ge 14 ]; then ok "macOS $VER"; else no "macOS $VER" "Update: System Settings > General > Software Update."; fi
ok "$(( $(sysctl -n hw.memsize) / 1073741824 )) GB memory"
FREE=$(df -g "$HOME" | awk 'NR==2{print $4}')
if [ "${FREE:-0}" -ge 20 ]; then ok "${FREE} GB free disk"; else no "${FREE} GB free disk" "Free up about 20 GB first."; fi

hdr "Toolchain"
CLT=0
if xcode-select -p >/dev/null 2>&1; then CLT=1; ok "Command Line Tools"
else no "Command Line Tools missing" "Installing Homebrew (step 02) installs these for you."; fi

# git and python3 in /usr/bin are stubs that open an installer dialog when
# Command Line Tools are missing, so they are only called when CLT exists.
tool() { # name, version-cmd, fix
  local p; p=$(command -v "$1" 2>/dev/null)
  if [ -z "$p" ]; then no "$1 missing" "$3"; return; fi
  if [ "$CLT" = 0 ] && [ "${p#/usr/bin/}" != "$p" ]; then no "$1 not usable yet" "$3"; return; fi
  ok "$1 · $(eval "$2" 2>/dev/null | head -1)"
}
tool brew    "brew --version"     "Step 02: install Homebrew."
tool git     "git --version"      "brew install git"
tool python3 "python3 --version"  "brew install python"
tool node    "node --version"     "brew install node"
tool gh      "gh --version"       "brew install gh"

hdr "Agents"
tool codex   "codex --version"    "npm i -g @openai/codex"
tool claude  "claude --version"   "Optional. npm i -g @anthropic-ai/claude-code"
if [ -d "/Applications/ChatGPT.app" ]; then ok "ChatGPT for Mac"; else no "ChatGPT for Mac not installed" "openai.com/chatgpt/download"; fi

hdr "Workspace and guardrails"
W="$HOME/AI-Work"
if [ -d "$W" ]; then ok "~/AI-Work exists"; else no "~/AI-Work missing" "Step 04."; fi
if [ -d "$W/.git" ]; then ok "Version history on in AI-Work"; else no "No version history in AI-Work" "Step 04: git init."; fi
if [ -f "$W/AGENTS.md" ]; then
  if grep -Eq '\[(YOUR NAME|COMPANY NAME|ONE LINE|WHO)' "$W/AGENTS.md"; then no "AGENTS.md still has [BRACKETS]" "Step 05: fill them in."; else ok "AGENTS.md written"; fi
else no "No AGENTS.md" "Step 05."; fi
C="$HOME/.codex/config.toml"
if [ -f "$C" ] && grep -Eq '^sandbox_mode *= *"workspace-write"' "$C"; then ok "Codex sandbox: workspace-write"
else no "Codex sandbox not set" "Step 06."; fi
if [ -f "$C" ] && grep -Eq '^approval_policy *= *"(on-request|untrusted)"' "$C"; then ok "Codex asks before risky actions"
else no "Codex approvals not set" "Step 06."; fi

hdr "Skills"
V="$HOME/.venvs/ai-work/bin/python"
if [ -x "$V" ] && "$V" -c 'import docx, PIL, pillow_heif' 2>/dev/null; then ok "Word and photo libraries"
else no "Word/photo libraries missing" "Step 07: python3 -m venv ~/.venvs/ai-work, then pip install."; fi
if [ -f "$HOME/.agents/skills/photo-report/SKILL.md" ]; then ok "photo-report skill"; else no "photo-report skill missing" "Step 07."; fi
if [ -d "/Applications/Wispr Flow.app" ]; then ok "Wispr Flow (voice)"; else no "No voice app" "Optional. Dictation works: press Fn twice."; fi

printf '\n\033[1m%s ready · %s to do\033[0m\n' "$PASS" "$TODO"
if [ "$TODO" -eq 0 ]; then printf 'All set. Open Codex in ~/AI-Work and say: "Read AGENTS.md and list your skills."\n\n'
else printf 'Guide: \033[4mhttps://agent.axonavenue.my/guide\033[0m  ·  Or have it built: wa.me/60127478227\n\n'; fi
