#!/bin/bash
# xrsim.app launcher. GUI apps launched by Finder don't inherit the user's
# shell profile PATH, so every check below goes through a login shell
# (`zsh -l`) rather than trusting this process's own PATH.

LOGFILE="/tmp/xrsim-app-launch.log"
: > "$LOGFILE"

HAS_XRSIM="$(/bin/zsh -l -c 'command -v xrsim' 2>>"$LOGFILE")"

if [ -n "$HAS_XRSIM" ]; then
  if ! /bin/zsh -l -c 'xrsim demo' >>"$LOGFILE" 2>&1; then
    /usr/bin/osascript -e "display dialog \"xrsim demo didn't start. Details were saved to $LOGFILE — send that to Alex if this keeps happening.\" buttons {\"OK\"} with icon caution with title \"xrsim\""
  fi
else
  # First run: install xrsim, visibly, in a real Terminal window — no silent
  # multi-second pause with nothing on screen. Reuses the exact same
  # install script the curl one-liner runs (unpkg, not the private repo).
  /usr/bin/osascript <<'OSA_EOF'
tell application "Terminal"
  activate
  do script "clear; echo 'Setting up xrsim — this only happens once...'; echo; curl -fsSL https://unpkg.com/xrsim@latest/install/install-mac.command | bash && xrsim demo"
end tell
OSA_EOF
fi
