#!/bin/bash
set -euo pipefail

# Find install root: prefer repo next to .app, else bundle resources
APP_DIR="$(cd "$(dirname "$0")/.." && pwd)"
REPO_DIR="$(cd "$(dirname "$0")/../../.." && pwd)"
BUNDLE_RES="$APP_DIR/Resources/sceneframe"
VENV="/Volumes/All/Dev/Cam-App/.venv"

if [ -x "$VENV/bin/python" ]; then
  PY="$VENV/bin/python"
elif [ -x "/Volumes/All/Dev/Cam-App/.venv/bin/python" ]; then
  PY="/Volumes/All/Dev/Cam-App/.venv/bin/python"
else
  PY="$(command -v python3)"
fi

if [ -f "$REPO_DIR/sceneframe_mac.py" ]; then
  ROOT="$REPO_DIR"
else
  ROOT="$BUNDLE_RES"
fi

export SCENEFRAME_PORT="8787"
cd "$ROOT"
exec "$PY" "$ROOT/sceneframe_mac.py"
