@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion

:: ==========================================
:: CONFIGURATION (Change version here)
:: ==========================================
set APP_VERSION=1.0.0
set PROGUARD_PATH=C:\proguard-7.9.1\lib\proguard.jar
set ORIGINAL_JAR=SpeedCropPro.jar
set OBFUSCATED_JAR=SpeedCropPro_obfuscated.jar
set APP_NAME=SpeedCropPro
set ICON_PATH=speed-crop-pro.ico
:: ==========================================

echo [1/5] Cleaning old builds...
if exist "%OBFUSCATED_JAR%" del "%OBFUSCATED_JAR%"
if exist "%APP_NAME%" rmdir /s /q "%APP_NAME%"
if exist "%APP_NAME%_v*.zip" del "%APP_NAME%_v*.zip"

echo [2/5] Obfuscating code with ProGuard...
java -jar "%PROGUARD_PATH%" "@config.pro"
if not exist "%OBFUSCATED_JAR%" (
    echo [ERROR] ProGuard failed! Check your config.pro.
    pause
    exit /b
)

echo [3/5] Packaging with jpackage (Creating EXE and JRE)...
jpackage --type app-image --name "%APP_NAME%" --input . --main-jar "%OBFUSCATED_JAR%" --main-class Main --icon "%ICON_PATH%"

echo [4/5] Copying external decoders...
copy "avifdec.exe" "%APP_NAME%\avifdec.exe" >nul
copy "libwebp-1.2.2-windows-x64.zip" "%APP_NAME%\libwebp-1.2.2-windows-x64.zip" >nul

echo [5/5] Compressing to ZIP...
powershell -command "Compress-Archive -Path '%APP_NAME%' -DestinationPath '%APP_NAME%_v%APP_VERSION%.zip' -Force"

echo ========================================================
echo [SUCCESS] Release is ready: %APP_NAME%_v%APP_VERSION%.zip
echo ========================================================
pause