#!/bin/bash
# xrsim setup for macOS — installs Node.js (if needed) and xrsim itself.
#
# Two ways to run this:
#   1. Double-click it in Finder.
#   2. From Terminal:  curl -fsSL https://unpkg.com/xrsim@latest/install/install-mac.command | bash
#      (Recommended for a classroom — a double-clicked file downloaded from a browser gets
#      quarantined by Gatekeeper and macOS will refuse to run it without an explicit
#      right-click → Open. Piping into bash from Terminal skips that entirely.)
set -e

# Only blocks when stdin is a real interactive terminal. `curl | bash` connects stdin to the
# curl pipe, not the terminal — `read` there hits EOF immediately and returns non-zero, which
# under `set -e` silently kills the whole script (and anything chained after it with `&&`) right
# at this line, even though everything before it succeeded. Found live: an `install | bash &&
# xrsim demo` chain never ran `xrsim demo` because of exactly this — the install genuinely
# succeeded (real exit 1 despite printing "Done"), verified by checking $? directly.
pause_if_interactive() {
  if [ -t 0 ]; then
    pause_if_interactive
  fi
}

echo "=================================="
echo " xrsim setup"
echo "=================================="
echo ""

NODE_OK=false
if command -v node >/dev/null 2>&1; then
  NODE_MAJOR="$(node -p 'process.versions.node.split(".")[0]' 2>/dev/null || echo 0)"
  if [ "$NODE_MAJOR" -ge 18 ] 2>/dev/null; then
    NODE_OK=true
    echo "Node.js $(node -v) found — that's all xrsim needs to install."
  else
    echo "Found Node.js $(node -v), but xrsim needs 18 or newer."
  fi
else
  echo "Node.js not found."
fi

if [ "$NODE_OK" = false ]; then
  echo ""
  echo "Installing Node.js via Homebrew..."
  if ! command -v brew >/dev/null 2>&1; then
    echo "Homebrew not found either — installing it first (this will ask for your password)."
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    # A fresh Homebrew install isn't on PATH in this shell yet — pick up its known prefixes.
    if [ -x /opt/homebrew/bin/brew ]; then eval "$(/opt/homebrew/bin/brew shellenv)"; fi
    if [ -x /usr/local/bin/brew ]; then eval "$(/usr/local/bin/brew shellenv)"; fi
  fi

  if command -v brew >/dev/null 2>&1; then
    brew install node
  else
    echo ""
    echo "Couldn't install Homebrew automatically (no network, or it needs something this"
    echo "script can't handle). Please install Node.js yourself — https://nodejs.org, the"
    echo "LTS version — then run this script again."
    echo ""
    pause_if_interactive
    exit 1
  fi
  echo ""
  echo "Node.js ready: $(node -v)"
fi

echo ""
echo "Installing xrsim..."
# --allow-scripts=xrsim: without it, some npm setups silently skip the postinstall step
# that fetches xrsim's bundled Chromium — xrsim would install fine but `xrsim demo` would
# then fail on first run with no browser to launch. Falls back to a plain install for an
# npm old enough not to recognize the flag (which also predates the gating it works around).
if npm install -g xrsim --allow-scripts=xrsim; then
  echo "xrsim installed."
elif npm install -g xrsim; then
  echo "xrsim installed."
else
  echo "That failed — retrying with elevated permissions..."
  sudo npm install -g xrsim --allow-scripts=xrsim || sudo npm install -g xrsim
fi

echo ""
echo "=================================="
echo " Done"
echo "=================================="
xrsim version 2>/dev/null || echo "xrsim installed"
echo ""
echo "Try it now:"
echo "  xrsim demo"
echo ""
echo "New to xrsim?          xrsim tutorial   — a five-step guided walkthrough"
echo "Testing a real Pico/Android app instead of the demo scene?"
echo "                        xrsim setup      — checks this machine for what that needs"
echo ""
pause_if_interactive
