@echo off
cd /d "%~dp0"

echo === Harmony Desktop — Windows Build ===
echo.

:: ── 1. Check for Node.js ──
where node >nul 2>nul
if %ERRORLEVEL% neq 0 (
    echo [!] Node.js not found. Installing via winget...
    winget install OpenJS.NodeJS.LTS --accept-source-agreements --accept-package-agreements
    echo.
    echo     Node.js installed. Please CLOSE this window and run build-windows.bat again
    echo     so that node/npm are in your PATH.
    pause
    exit /b 0
)
echo [ok] Node %node -v%

:: ── 2. Find MSVC compiler ──
echo [1/5] Looking for C++ compiler...
where cl >nul 2>nul
if %ERRORLEVEL% equ 0 (
    echo        Found cl.exe in PATH.
    goto :compile
)

:: Search every year and edition
set "VCVARS="
for %%y in (2030 2029 2028 2027 2026 2025 2024 2023 2022 2021 2020 2019 2018 2017) do (
    for %%e in (Enterprise Professional Community BuildTools Preview) do (
        if exist "C:\Program Files\Microsoft Visual Studio\%%y\%%e\VC\Auxiliary\Build\vcvarsall.bat" (
            set "VCVARS=C:\Program Files\Microsoft Visual Studio\%%y\%%e\VC\Auxiliary\Build\vcvarsall.bat"
            goto :found_vcvars
        )
        if exist "C:\Program Files (x86)\Microsoft Visual Studio\%%y\%%e\VC\Auxiliary\Build\vcvarsall.bat" (
            set "VCVARS=C:\Program Files (x86)\Microsoft Visual Studio\%%y\%%e\VC\Auxiliary\Build\vcvarsall.bat"
            goto :found_vcvars
        )
    )
)

:: Also check vswhere (the official way to find VS installations)
set "VSWHERE=%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe"
if not exist "%VSWHERE%" set "VSWHERE=%ProgramFiles%\Microsoft Visual Studio\Installer\vswhere.exe"
if exist "%VSWHERE%" (
    for /f "tokens=*" %%p in ('"%VSWHERE%" -latest -property installationPath 2^>nul') do (
        if exist "%%p\VC\Auxiliary\Build\vcvarsall.bat" (
            set "VCVARS=%%p\VC\Auxiliary\Build\vcvarsall.bat"
            goto :found_vcvars
        )
    )
)

:: Nothing found — install Build Tools automatically
echo [!] No C++ compiler found. Installing Visual Studio Build Tools...
echo     This will download ~2GB and may take a few minutes.
echo.

:: Download the installer
powershell -Command "Invoke-WebRequest -Uri 'https://aka.ms/vs/17/release/vs_BuildTools.exe' -OutFile '%TEMP%\vs_buildtools.exe'"
if not exist "%TEMP%\vs_buildtools.exe" (
    echo ERROR: Failed to download Build Tools installer.
    pause
    exit /b 1
)

:: Install with C++ workload (silent)
echo     Installing C++ Build Tools (this may take several minutes)...
"%TEMP%\vs_buildtools.exe" --quiet --wait --norestart --nocache ^
    --add Microsoft.VisualStudio.Workload.VCTools ^
    --add Microsoft.VisualStudio.Component.VC.Tools.x86.x64 ^
    --add Microsoft.Component.MSBuild ^
    --includeRecommended
if %ERRORLEVEL% neq 0 (
    echo.
    echo     Install may have failed or needs a reboot.
    echo     Please reboot and run build-windows.bat again.
    pause
    exit /b 1
)
del "%TEMP%\vs_buildtools.exe" 2>nul

:: Try finding it again after install
for %%y in (2030 2029 2028 2027 2026 2025 2024 2023 2022) do (
    for %%e in (BuildTools Enterprise Professional Community Preview) do (
        if exist "C:\Program Files\Microsoft Visual Studio\%%y\%%e\VC\Auxiliary\Build\vcvarsall.bat" (
            set "VCVARS=C:\Program Files\Microsoft Visual Studio\%%y\%%e\VC\Auxiliary\Build\vcvarsall.bat"
            goto :found_vcvars
        )
        if exist "C:\Program Files (x86)\Microsoft Visual Studio\%%y\%%e\VC\Auxiliary\Build\vcvarsall.bat" (
            set "VCVARS=C:\Program Files (x86)\Microsoft Visual Studio\%%y\%%e\VC\Auxiliary\Build\vcvarsall.bat"
            goto :found_vcvars
        )
    )
)

echo ERROR: Build Tools installed but vcvarsall.bat not found.
echo        Try rebooting and running this script again.
pause
exit /b 1

:found_vcvars
echo        Found: %VCVARS%
call "%VCVARS%" x64 >nul 2>nul
if %ERRORLEVEL% neq 0 (
    echo        Failed to initialize compiler environment.
    pause
    exit /b 1
)

:compile
:: ── 3. Build loopback capture tool ──
echo [2/5] Building loopback audio capture tool...
if not exist bin mkdir bin
pushd native\win-loopback
cl /EHsc /O2 /nologo /Fe:loopback_capture.exe loopback_capture.cpp ole32.lib mfplat.lib mmdevapi.lib >nul 2>&1
if %ERRORLEVEL% neq 0 (
    echo        First attempt failed, retrying with full output:
    cl /EHsc /O2 /Fe:loopback_capture.exe loopback_capture.cpp ole32.lib mfplat.lib mmdevapi.lib
    if %ERRORLEVEL% neq 0 (
        popd
        echo        C++ compilation failed.
        pause
        exit /b 1
    )
)
copy /y loopback_capture.exe ..\..\bin\loopback_capture.exe >nul
del loopback_capture.obj 2>nul
del loopback_capture.exe 2>nul
popd
echo        Built bin\loopback_capture.exe

:: ── 4. npm install ──
echo [3/5] Installing dependencies...
call npm install

:: ── 5. Build Electron app ──
echo [4/5] Building Windows installer...
call npx electron-builder --win

echo.
echo [5/5] Done!
echo Output is in .\dist\
dir /b dist\*.exe 2>nul
echo.
pause
