@echo off
setlocal enabledelayedexpansion

:: Initialize variables
set "FLAGS=--no-node --quiet"
set "OUT_JS="
set "FILE_ARGS="

:parse_args
if "%~1"=="" goto :run_check

set "arg=%~1"
set "prefix=!arg:~0,2!"

:: 1. Check if it's an option (starts with --)
if "%prefix%"=="--" (
    set "FLAGS=%FLAGS% %arg%"
    shift
    goto :parse_args
)

:: 2. If OUT_JS is empty, this must be the output filename
if "%OUT_JS%"=="" (
    set "OUT_JS=%~1"
    shift
    goto :parse_args
)

:: 3. Everything else is an input file to --embed
set "FILE_ARGS=%FILE_ARGS% --embed "%1""
shift
goto :parse_args

:run_check
if "%OUT_JS%"=="" (
    echo Usage: pack_assets.bat [--option] output.js input1 [input2@virtual] ...
    echo Example: pack_assets.bat --lz4 --use-preload-plugins bundle.js data.bin image.png@/tex/bg.png
    exit /b 1
)

:run
echo [Config] Flags: %FLAGS%
echo [Config] Output: %OUT_JS%

call "%EMSDK%\upstream\emscripten\tools\file_packager.bat" "%OUT_JS%.data" %FILE_ARGS% --js-output="%OUT_JS%" %FLAGS%

echo.
echo [Build Complete]
endlocal