#!/bin/bash
# Standalone Mac launcher — desktop UI only, never opens a browser.
DIR="$(cd "$(dirname "$0")/../Resources/app" && pwd)"
cd "$DIR"

CANDIDATES=(
  "/Library/Frameworks/Python.framework/Versions/Current/bin/python3"
  "/Library/Frameworks/Python.framework/Versions/3.13/bin/python3"
  "/Library/Frameworks/Python.framework/Versions/3.12/bin/python3"
  "/Library/Frameworks/Python.framework/Versions/3.11/bin/python3"
  "/opt/homebrew/bin/python3"
  "/usr/local/bin/python3"
  "/usr/bin/python3"
  "python3"
)

PY=""
for cand in "${CANDIDATES[@]}"; do
  if command -v "$cand" >/dev/null 2>&1 || [[ -x "$cand" ]]; then
    if "$cand" -c "import tkinter" >/dev/null 2>&1; then
      PY="$cand"
      break
    fi
  fi
done

if [[ -z "$PY" ]]; then
  osascript <<'EOF'
display dialog "PlugSafe QuickReports needs Python 3 with Tcl/Tk.

Install the official package from https://www.python.org/downloads/macos/
(the python.org installer includes the window toolkit).

Homebrew users: brew install python-tk@3"
buttons {"OK"} default button "OK" with title "PlugSafe QuickReports"
EOF
  exit 1
fi

exec "$PY" "$DIR/plugsafe_app.py"
