@echo OFF
rem  This uses SoCPUP to extract the Pripyat level file and
rem  an alternate texture to correct the ghost garage problem.
rem  Once the level file is extracted it is patched to use
rem  the renamed texture.

rem  First extract the level if not already available
echo *************************************************
echo *** Pripyat level fix for ghost garages, v1.0 ***
echo *************************************************
if NOT EXIST gamedata\levels\l11_pripyat\level goto ExtractLevel
echo -------------- Pripyat level file already exists
goto CheckTexture

:ExtractLevel
echo ... Extracting Pripyat level file ...
SoCPUP -extractx levels\l11_pripyat\level -dir gamedata gamedata.db2
if ERRORLEVEL 2 goto FixErrorFirst

:CheckTexture
if NOT EXIST gamedata\textures\mtl\fixx_mtl_garage_pryp_02.dds goto CheckSourceTexture
echo -------------- Pripyat texture file already exists
goto PatchLevel

:CheckSourceTexture
if NOT EXIST gamedata\textures\mtl\mtl_garage_pryp_02.dds goto ExtractTexture
echo -------------- Pripyat source texture file already exists
goto RenameTexture

:ExtractTexture
echo ... Extracting garage texture file ...
SoCPUP -extractx textures\mtl\mtl_garage_pryp_02.dds -dir gamedata gamedata.db7
if ERRORLEVEL 2 goto FixErrorFirst

:RenameTexture
echo ... Renaming garage texture file ...
rename gamedata\textures\mtl\mtl_garage_pryp_02.dds fixx_mtl_garage_pryp_02.dds

:PatchLevel
echo ... Patching level file ...
SoCPUP -patchfile _PripyatLevelFix.patch
if ERRORLEVEL 2 goto FixErrorFirst
if ERRORLEVEL 1 goto AlreadyPatched
goto PatchDone

:AlreadyPatched
echo ...
echo The level file is apparently already patched.
echo ...
goto PatchExit

:FixErrorFirst
echo ...
echo Please fix the problem before trying again.
echo ...
goto PatchExit

:PatchDone
echo ...
echo Patch of Pripyat level is complete.

:PatchExit
pause