@echo off
setlocal

cd /d "%~dp0"

set "APP_NAME=jeu"
set "OUTPUT_DIR=%~dp0.."

echo [INFO] Nettoyage...
if exist build rmdir /s /q build
if exist "%OUTPUT_DIR%\%APP_NAME%" rmdir /s /q "%OUTPUT_DIR%\%APP_NAME%"

mkdir build\classes
mkdir build\jpackage-input

echo [INFO] Compilation Java...
javac --release 24 -cp "lib/*;." -d build\classes linea\*.java
if errorlevel 1 (
    echo [ERREUR] Compilation echouee.
    pause
    exit /b 1
)

echo Main-Class: linea.LineaAppli> build\MANIFEST.MF
echo.>> build\MANIFEST.MF

echo [INFO] Creation du JAR principal...
jar cfm build\jpackage-input\Linea.jar build\MANIFEST.MF -C build\classes .
if errorlevel 1 (
    echo [ERREUR] Creation du JAR echouee.
    pause
    exit /b 1
)

copy /y lib\*.jar build\jpackage-input\ >nul

echo [INFO] Generation de l'executable portable (sans installation)...
jpackage --type app-image --name %APP_NAME% --input build\jpackage-input --main-jar Linea.jar --main-class linea.LineaAppli --dest "%OUTPUT_DIR%" --add-modules java.base,java.desktop,java.sql,java.logging,jdk.unsupported --java-options "--enable-native-access=ALL-UNNAMED"
if errorlevel 1 (
    echo [ERREUR] Echec de la generation.
    pause
    exit /b 1
)

if exist build rmdir /s /q build

echo [OK] Executable pret: ..\%APP_NAME%\%APP_NAME%.exe
echo [OK] Double-clique directement ce fichier (pas d'installation).
pause
endlocal
