@echo off
@REM Common 7zSfxMod build for GFN PFW. Called by build7zSfx.bat, build7zSfx_x64.bat, build7zSfx_arm64.bat.
@REM Usage: call build7zSfx_common.bat <platform>
@REM   platform: Win32, x64, or ARM64

set _PLATFORM=%1
if "%_PLATFORM%"=="" (
    echo ERROR: Platform not specified. Use Win32, x64, or ARM64.
    exit /b 1
)

cd /d "%~dp0"

echo.
echo ===============================================================================
echo ========================= Preparing build environment ==========================
echo ===============================================================================

if not exist "%BUILD_TOOLS_DIR%" goto :error_no_tools_dir
echo Using tools from %BUILD_TOOLS_DIR%

set VCVERSION=16.10.3
set VSINSTALLDIR=%BUILD_TOOLS_DIR%\win32\msvc\%VCVERSION%
if not exist %VSINSTALLDIR% goto :error_no_vs
echo Visual Studio path: %VSINSTALLDIR%
set VCINSTALLDIR=%VSINSTALLDIR%\VC
if not exist %VCINSTALLDIR% goto :error_no_vc
echo VisualC path: %VCINSTALLDIR%

echo Locating MSBuild with %VSINSTALLDIR%\Common7\Tools\VsMSBuildCmd.bat
call %VSINSTALLDIR%\Common7\Tools\VsMSBuildCmd.bat

SET VCTargetsPath=%BUILD_TOOLS_DIR%\win32\msvc\%VCVERSION%\MSBuild\Microsoft\VC\v160\

SET DisableRegistryUse=true

SET VCInstallDir_160=%VCINSTALLDIR%

set WINSDK_DIR=%BUILD_TOOLS_DIR%\sdk\WinSDK\10
set WindowsSdkDir_10=%BUILD_TOOLS_DIR%\sdk\WinSDK\10
set WINSDK_VERSION=10.0.14393.0
set WINSDK_INCLUDE_ROOT=%WindowsSdkDir_10%\Include\%WINSDK_VERSION%
if not exist "%WINSDK_DIR%" goto :nosdk_error
echo Using WinSDK from %WINSDK_DIR%

@REM FeatureLogging.props pulls in FeatureBoostLatest.props; MSBuild must receive BOOST_DIR or link gets /LIBPATH:"\lib-msvc-14.2" and LNK1104 on libboost_thread.
set BOOST_DIR=%BUILD_TOOLS_DIR%\boost\boost_1_79_0
if not exist "%BOOST_DIR%" goto :error_no_boost
if not exist "%BOOST_DIR%\lib-msvc-14.2" goto :error_no_boost_lib
echo Using Boost from %BOOST_DIR%

set _PATH=%PATH%

set GENERAL_BUILD_OPTIONS=/nologo ^
    /p:VCToolsVersion="14.29" ^
    /p:VCToolsInstallDir="%VCINSTALLDIR%\\" ^
    /p:WindowsSDKDir="%WindowsSdkDir_10%" ^
    /p:WindowsSDK_IncludePath="%WINSDK_INCLUDE_ROOT%\um;%WINSDK_INCLUDE_ROOT%\shared;%WINSDK_INCLUDE_ROOT%\winrt;%WINSDK_INCLUDE_ROOT%\ucrt"

echo.
echo ===============================================================================
echo =========================  Preparing %_PLATFORM% environment ==========================
echo ===============================================================================

if "%_PLATFORM%"=="Win32" (
    set _VC_BIN=%VCINSTALLDIR%\BIN
    set _WINSDK_ARCH=x86
    set _REDIST_ARCH=x86
    set _BIN_SUPPORT=x86
    set _OUTPUT_DIR=SFX\Output\Win32
    set _RESOURCES_SUBDIR=
) else if "%_PLATFORM%"=="x64" (
    set _VC_BIN=%VCINSTALLDIR%\BIN\amd64
    set _WINSDK_ARCH=x64
    set _REDIST_ARCH=x64
    set _BIN_SUPPORT=x64
    set _OUTPUT_DIR=SFX\Output\x64
    set _RESOURCES_SUBDIR=x64
) else if "%_PLATFORM%"=="ARM64" (
    set _VC_BIN=%VCINSTALLDIR%\BIN\arm64
    set _WINSDK_ARCH=x64
    set _REDIST_ARCH=arm64
    set _BIN_SUPPORT=x64
    set _OUTPUT_DIR=SFX\Output\arm64
    set _RESOURCES_SUBDIR=arm64
) else (
    echo ERROR: Invalid platform "%_PLATFORM%". Use Win32, x64, or ARM64.
    exit /b 1
)

set WINSDK_DIR_BIN=%WINSDK_DIR%\Bin\%WINSDK_VERSION%\%_WINSDK_ARCH%
set PATH=%WINSDK_DIR_BIN%;%VSINSTALLDIR%\Common7\IDE;%_VC_BIN%;%VSINSTALLDIR%\Common7\Tools;%VSINSTALLDIR%\Common7\Tools\bin;%WindowsSdkDir_10%\bin\%_WINSDK_ARCH%;%PATH%
set PATH=%VCINSTALLDIR%\redist\%_REDIST_ARCH%\Microsoft.VC140.CRT;%VSINSTALLDIR%\bin-support\%_BIN_SUPPORT%;%PATH%

echo PATH: %PATH%

echo.
echo ===============================================================================
echo ========================     Starting 7zSfxMod %_PLATFORM% build...     ========================
echo ===============================================================================

@REM GFN splash + no-UAC (matches build7zSfx.bat)
SET CL=/DUSE_SPLASH /DNOUAC

if "%_PLATFORM%"=="Win32" (
    call MSBuild SFX\7zSfxMod\7zSfxMod.sln /t:Rebuild /p:Configuration=LZMA;Platform=Win32;TargetFrameworkVersion=v4.8;BOOST_DIR=%BOOST_DIR% %GENERAL_BUILD_OPTIONS%
    call MSBuild SFX\7zSfxMod\7zSfxMod.sln /t:Rebuild /p:Configuration=LZMA2;Platform=Win32;TargetFrameworkVersion=v4.8;BOOST_DIR=%BOOST_DIR% %GENERAL_BUILD_OPTIONS%
) else if "%_PLATFORM%"=="x64" (
    call MSBuild SFX\7zSfxMod\7zSfxMod.sln /t:Rebuild /p:Configuration=LZMA;Platform=x64;TargetFrameworkVersion=v4.8;BOOST_DIR=%BOOST_DIR% %GENERAL_BUILD_OPTIONS%
    call MSBuild SFX\7zSfxMod\7zSfxMod.sln /t:Rebuild /p:Configuration=LZMA2;Platform=x64;TargetFrameworkVersion=v4.8;BOOST_DIR=%BOOST_DIR% %GENERAL_BUILD_OPTIONS%
) else if "%_PLATFORM%"=="ARM64" (
    call MSBuild SFX\7zSfxMod\7zSfxMod.sln /t:Rebuild /p:Configuration=LZMA;Platform=ARM64;TargetFrameworkVersion=v4.8 %GENERAL_BUILD_OPTIONS%
    call MSBuild SFX\7zSfxMod\7zSfxMod.sln /t:Rebuild /p:Configuration=LZMA2;Platform=ARM64;TargetFrameworkVersion=v4.8 %GENERAL_BUILD_OPTIONS%
)

if errorlevel 1 (
    set "PATH=%_PATH%"
    exit /b 1
)

if "%_RESOURCES_SUBDIR%"=="" (
    del pfw\BinRef\Prerequisites\resources\7zsd_LZMA.sfx 2>nul
    del pfw\BinRef\Prerequisites\resources\7zsd_LZMA2.sfx 2>nul
    xcopy %_OUTPUT_DIR%\*.sfx pfw\BinRef\Prerequisites\resources\ /Y
) else (
    del pfw\BinRef\Prerequisites\resources\%_RESOURCES_SUBDIR%\7zsd_LZMA.sfx 2>nul
    del pfw\BinRef\Prerequisites\resources\%_RESOURCES_SUBDIR%\7zsd_LZMA2.sfx 2>nul
    if not exist "pfw\BinRef\Prerequisites\resources\%_RESOURCES_SUBDIR%" mkdir pfw\BinRef\Prerequisites\resources\%_RESOURCES_SUBDIR%
    if not exist "pfw\BinRef\Prerequisites\resources" mkdir pfw\BinRef\Prerequisites\resources
    xcopy %_OUTPUT_DIR%\*.sfx pfw\BinRef\Prerequisites\resources\%_RESOURCES_SUBDIR%\ /Y
    copy /Y %_OUTPUT_DIR%\7zsd_LZMA.sfx pfw\BinRef\Prerequisites\resources\7ZSD_LZMA.sfx
    copy /Y %_OUTPUT_DIR%\7zsd_LZMA2.sfx pfw\BinRef\Prerequisites\resources\7ZSD_LZMA2.sfx
)

set "PATH=%_PATH%"

echo.
echo %_PLATFORM% 7zSfxMod build completed successfully
goto :eof

:error_no_tools_dir
echo ERROR: BUILD_TOOLS_DIR environment variable is not set or directory does not exist
exit /b 1

:error_no_vs
echo ERROR: Visual Studio %VCVERSION% not found in %VSINSTALLDIR%
exit /b 1

:error_no_vc
echo ERROR: Visual C++ not found in %VCINSTALLDIR%
exit /b 1

:nosdk_error
echo ERROR: Windows SDK not found in %WINSDK_DIR%
exit /b 1

:error_no_boost
echo ERROR: Boost not found at %BOOST_DIR%
echo Ensure BUILD_TOOLS_DIR\boost\boost_1_79_0 exists (same layout as DVS path sw/tools/boost/boost_1_79_0).
exit /b 1

:error_no_boost_lib
echo ERROR: Boost prebuilt libs not found at %BOOST_DIR%\lib-msvc-14.2
echo NVIDIA internal Boost layout expects libboost_*-vc142-mt-x64-1_79.lib (and Win32 equivalents) under lib-msvc-14.2.
exit /b 1
