#!/bin/bash
# StellarFlow desktop launcher wrapper for macOS.
# Invoked by macOS when the user double-clicks StellarFlow.app. Resolves the
# bundled Node runtime under Contents/Resources and hands off to the common
# desktop/launcher/start.mjs script, which spawns the standalone server and
# opens the browser-based activation or application UI.
#
# Expected bundle layout:
#   StellarFlow.app/Contents/MacOS/launcher        (this file)
#   StellarFlow.app/Contents/Resources/node/bin/node
#   StellarFlow.app/Contents/Resources/server/server.js
#   StellarFlow.app/Contents/Resources/desktop/launcher/start.mjs
#   StellarFlow.app/Contents/Resources/bin/*.js
#   StellarFlow.app/Contents/Resources/lib/license.ts

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
RESOURCES_DIR="$(cd "$SCRIPT_DIR/../Resources" && pwd)"

NODE_BIN="$RESOURCES_DIR/node/bin/node"
START_SCRIPT="$RESOURCES_DIR/desktop/launcher/start.mjs"

if [ ! -x "$NODE_BIN" ]; then
  osascript -e 'display alert "StellarFlow" message "The bundled Node.js runtime is missing. The installation may be corrupt. Please reinstall StellarFlow." as critical'
  exit 1
fi

exec "$NODE_BIN" --experimental-strip-types "$START_SCRIPT"
