#!/bin/bash
# Bernard pre-check: free, read-only, changes nothing. Tells you in plain words whether Bernard can run on this Mac
# BEFORE you buy. Run it in Terminal:   bash precheck.sh
ok=0; bad=0
yes() { printf '  ✅ %s\n' "$1"; ok=$((ok+1)); }
no()  { printf '  ❌ %s\n     → %s\n' "$1" "$2"; bad=$((bad+1)); }
echo "Bernard pre-check"
echo

[ "$(uname)" = "Darwin" ] && yes "This is a Mac" || no "This isn't a Mac" "Bernard runs on macOS only (for now)."
V=$(sw_vers -productVersion 2>/dev/null || echo 0); MAJ=${V%%.*}
[ "${MAJ:-0}" -ge 13 ] && yes "macOS $V" || no "macOS $V is too old" "Bernard needs macOS 13 (Ventura) or newer."
FREE=$(df -g "$HOME" | awk 'NR==2{print $4}')
[ "${FREE:-0}" -ge 3 ] && yes "${FREE} GB free" || no "Only ${FREE} GB free" "Bernard needs about 3 GB."
curl -s -m 8 -o /dev/null https://www.google.com && yes "Internet works" || no "No internet" "Connect to the internet and run this again."

CLAUDE=$(command -v claude || ls "$HOME/.local/bin/claude" 2>/dev/null)
if [ -z "$CLAUDE" ]; then
  no "Claude Code isn't installed" "Install it from https://claude.com/claude-code (Bernard's hands; needs a paid Claude plan)."
else
  ST=$("$CLAUDE" auth status 2>/dev/null)
  if echo "$ST" | grep -q '"loggedIn": *true'; then
    if echo "$ST" | grep -qE '"subscriptionType": *"(pro|max|team|enterprise)"|"authMethod": *"api'; then
      yes "Claude Code is signed in on a paid plan"
    else
      no "Claude Code is signed in, but not on a paid plan" "Bernard needs Claude Pro or Max (or an API key)."
    fi
  else
    no "Claude Code isn't signed in" "Run: claude   and sign in, then run this check again."
  fi
fi
system_profiler SPAudioDataType 2>/dev/null | grep -q "Input Source\|Input Channels" && yes "A microphone is available" \
  || no "No microphone found" "Bernard listens to you: plug in or allow a microphone."

echo
echo "Two things this check can't do for you (both free, both in setup):"
echo "  • a Google Gemini key for Bernard's voice: https://aistudio.google.com/apikey"
echo "  • signing in to your school's Canvas once, in a normal browser window"
echo
if [ $bad -eq 0 ]; then echo "RESULT: Bernard will run on this Mac."; else echo "RESULT: $bad thing(s) to fix first (above)."; fi
