@echo off
cd _multi
REM Inject texture files to assets.
REM You can specify the asset path with _export_as_tga.bat or _set_asset_path.bat

REM Define the default file path here
set DEFAULT_ASSET_PATH=asset_120
set DEFAULT_FILE_PATH=_tga
set SOURCE_DIR=PAYDAY3\Content\Mods\AnimatedEscapeVanMod\Texture\asset_120
set DEST_DIR=PAYDAY3\Content\Mods\AnimatedEscapeVanMod\Texture

REM If a file is dragged and dropped, use that file path. Otherwise, use the default file path.
if "%~1"=="" (
    set FILE_PATH=%DEFAULT_FILE_PATH%
) else (
    set FILE_PATH=%~1%
)

del /q "PAYDAY3\Content\Mods\ModMeta"
del /q "PAYDAY3\Content\Mods\AnimatedEscapeVanMod"
del /q "PAYDAY3\Content\Mods\AnimatedEscapeVanMod\Texture"

@pushd %~dp0
..\data\python\python.exe -E ..\data\src\main.py %DEFAULT_ASSET_PATH% "%FILE_PATH%" --save_folder=PAYDAY3\Content\Mods\AnimatedEscapeVanMod\Texture --skip_non_texture --version=4.27 --image_filter=cubic
@popd

move "%SOURCE_DIR%\*" "%DEST_DIR%\

if errorlevel 1 (
    echo Failed to move files from %SOURCE_DIR% to %DEST_DIR%.
    pause
    exit /b
)

REM Remove the SOURCE_DIR
rmdir /s /q "%SOURCE_DIR%"

copy "asset_120_2\AnimatedEscapeVanModMeta.uasset" "PAYDAY3\Content\Mods\ModMeta"
copy "asset_120_2\AnimatedEscapeVanModMeta.uexp" "PAYDAY3\Content\Mods\ModMeta"
copy "asset_120_2\AnimateEscapeVan.uasset" "PAYDAY3\Content\Mods\AnimatedEscapeVanMod"
copy "asset_120_2\AnimateEscapeVan.uexp" "PAYDAY3\Content\Mods\AnimatedEscapeVanMod"

REM Create a temporary file list with full paths
dir /s /b /a-d PAYDAY3\* > temp_filelist.txt

REM Initialize filelist.txt
echo. > filelist.txt

REM Define the general suffix and specific suffixes
set general_suffix="../../../PAYDAY3/Content/Mods/AnimatedEscapeVanMod/Texture/"
set display_suffix="../../../PAYDAY3/Content/Mods/AnimatedEscapeVanMod/"
set meta_suffix="../../../PAYDAY3/Content/Mods/ModMeta/"

REM Read each line from temp_filelist.txt and format it
for /f "delims=" %%i in (temp_filelist.txt) do (
    set "filename=%%~nxi"
    call :process_line "%%i" "%%filename%%"
)

REM Run UnrealPak with the updated filelist.txt
..\data\Engine\Tool\unrealpak\UnrealPak.exe "..\..\..\..\_multi\yourmodname_van_anim.pak" -create=..\..\..\..\_multi\filelist.txt -compress

REM Cleanup temporary file
del temp_filelist.txt
del filelist.txt

move "yourmodname_van_anim.pak" "..\"

if errorlevel 1 (
    echo Failed to move files from %SOURCE_DIR% to %DEST_DIR%.
    pause
    exit /b
)

echo FINISH!
pause
goto :eof

:process_line
setlocal
set "filepath=%~1"
set "filename=%~2"

REM Check for specific filenames and assign the appropriate suffix
if "%filename%"=="AnimateEscapeVan.uasset" set suffix=%display_suffix%
if "%filename%"=="AnimateEscapeVan.uexp" set suffix=%display_suffix%
if "%filename%"=="AnimatedEscapeVanModMeta.uasset" set suffix=%meta_suffix%
if "%filename%"=="AnimatedEscapeVanModMeta.uexp" set suffix=%meta_suffix%
if not defined suffix set suffix=%general_suffix%

REM Write the formatted line to filelist.txt
echo "%filepath%" %suffix% >> filelist.txt

endlocal
goto :eof

