@echo off
rem ==========================================================================
rem  build.cmd
rem
rem  One-shot Release|x64 build launcher. Activates the VS 2022+ Developer
rem  Command Prompt, then runs msbuild on vhidkm.sln. Requires the Windows
rem  Driver Kit (WDK 10.0.26100) to be installed — otherwise msbuild will
rem  emit MSB8020 ("cannot find build tools for WindowsKernelModeDriver10.0").
rem
rem  Usage:  build.cmd                       Release x64 (default)
rem          build.cmd Debug                 Debug x64
rem          build.cmd Release ARM64         Release ARM64
rem ==========================================================================
setlocal EnableExtensions

set "CFG=%~1"
if "%CFG%"=="" set "CFG=Release"
set "PLAT=%~2"
if "%PLAT%"=="" set "PLAT=x64"

set "VSDEV="
for %%E in (Enterprise Professional Community BuildTools) do (
    if not defined VSDEV if exist "%ProgramFiles%\Microsoft Visual Studio\2022\%%E\Common7\Tools\VsDevCmd.bat" set "VSDEV=%ProgramFiles%\Microsoft Visual Studio\2022\%%E\Common7\Tools\VsDevCmd.bat"
)
for %%E in (Enterprise Professional Community BuildTools) do (
    if not defined VSDEV if exist "%ProgramFiles(x86)%\Microsoft Visual Studio\2022\%%E\Common7\Tools\VsDevCmd.bat" set "VSDEV=%ProgramFiles(x86)%\Microsoft Visual Studio\2022\%%E\Common7\Tools\VsDevCmd.bat"
)
for %%E in (Enterprise Professional Community BuildTools) do (
    if not defined VSDEV if exist "%ProgramFiles%\Microsoft Visual Studio\18\%%E\Common7\Tools\VsDevCmd.bat" set "VSDEV=%ProgramFiles%\Microsoft Visual Studio\18\%%E\Common7\Tools\VsDevCmd.bat"
)
for %%E in (Enterprise Professional Community BuildTools) do (
    if not defined VSDEV if exist "%ProgramFiles(x86)%\Microsoft Visual Studio\18\%%E\Common7\Tools\VsDevCmd.bat" set "VSDEV=%ProgramFiles(x86)%\Microsoft Visual Studio\18\%%E\Common7\Tools\VsDevCmd.bat"
)

if not defined VSDEV (
    echo [build] ERROR: VsDevCmd.bat not found. Install Visual Studio 2022+ with the
    echo [build]        "Desktop development with C++" workload.
    exit /b 1
)

echo [build] Using %VSDEV%
call "%VSDEV%" -arch=x64 -host_arch=x64 >nul 2>&1

rem WDKContentRoot: the HKLM\...\Installed Roots\KitsRoot10 entry may point at an
rem empty shell (e.g. "C:\Program Files\Windows Kits\10\" with no build/ folder).
rem Detect a real install by probing for build\<ver>\WindowsDriver.Default.props.
if not defined WDKContentRoot (
    if exist "%ProgramFiles(x86)%\Windows Kits\10\build\10.0.26100.0\WindowsDriver.Default.props" set "WDKContentRoot=%ProgramFiles(x86)%\Windows Kits\10\"
    if not defined WDKContentRoot if exist "%ProgramFiles%\Windows Kits\10\build\10.0.26100.0\WindowsDriver.Default.props" set "WDKContentRoot=%ProgramFiles%\Windows Kits\10\"
)
if defined WDKContentRoot echo [build] WDKContentRoot=%WDKContentRoot%

pushd "%~dp0"
msbuild vhidkm.sln /p:Configuration=%CFG% /p:Platform=%PLAT% /p:SignMode=Off /m /v:minimal
set RC=%ERRORLEVEL%
popd

if %RC% NEQ 0 (
    echo.
    echo [build] FAILED (exit code %RC%^).
    echo [build] If the error is MSB8020 "cannot find build tools for
    echo [build]        WindowsKernelModeDriver10.0", install the WDK:
    echo [build]          https://learn.microsoft.com/windows-hardware/drivers/download-the-wdk
    exit /b %RC%
)

echo.
echo [build] SUCCESS.
echo [build] Outputs:
echo [build]   .dist\%PLAT%\vusbbus\vusbbus.sys / .inf / .cat
echo [build]   .dist\%PLAT%\vhidkm\vhidkm.sys  / .inf / .cat
exit /b 0
