#!/bin/bash
# 1) Locate JDK 24
JAVA_PATH=$(/usr/libexec/java_home -v 24 2>/dev/null)
if [ -z "$JAVA_PATH" ]; then
  osascript -e 'display dialog "JDK 24 not detected. Please install JDK 24 first." buttons {"OK"}'
  exit 1
fi

# 2) Determine the .app bundle root
APP_ROOT="$(cd "$(dirname "$0")/../.." && pwd)"

# 3) Paths inside the bundle
JAR_PATH="$APP_ROOT/Contents/Resources/P-ATHINA.jar"
JFX_LIB="$APP_ROOT/Contents/Resources/javafx-sdk-24.0.1/lib"

# 4) Verify JAR
if [ ! -f "$JAR_PATH" ]; then
  osascript -e 'display dialog "P-ATHINA.jar not found inside the app bundle." buttons {"OK"}'
  exit 1
fi

# 5) Verify JavaFX SDK
if [ ! -d "$JFX_LIB" ]; then
  osascript -e 'display dialog "JavaFX SDK not found. Please embed javafx-sdk-24.0.1 under Resources." buttons {"OK"}'
  exit 1
fi

# 6) Launch with JavaFX modules and native-access enabled
exec "$JAVA_PATH/bin/java" \
  --enable-native-access=ALL-UNNAMED \
  --module-path "$JFX_LIB" \
  --add-modules javafx.controls,javafx.fxml \
  -jar "$JAR_PATH"
