@echo off
setlocal EnableExtensions DisableDelayedExpansion

set "SCRIPT_DIR=%~dp0"
set "SUPPORT_DIR=%SCRIPT_DIR%VirtualManagerFiles"
set "PS1_PATH=%SUPPORT_DIR%\install.ps1"
set "LOG_PATH=%TEMP%\VirtualManagerInstall.log"

echo ============================================
echo  Virtual Manager - Bootstrap Installer
echo ============================================
echo.
echo Log file: %LOG_PATH%
echo.

if exist "%LOG_PATH%" del /f /q "%LOG_PATH%" >nul 2>&1

if exist "%PS1_PATH%" goto run_installer

set "SUPPORT_DIR=%SCRIPT_DIR%"
set "PS1_PATH=%SUPPORT_DIR%install.ps1"

if not exist "%PS1_PATH%" goto missing_ps1

:run_installer
powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%PS1_PATH%" > "%LOG_PATH%" 2>&1
set "INSTALL_EXIT_CODE=%ERRORLEVEL%"

if exist "%LOG_PATH%" goto print_log
echo ERROR: Installer log was not created.
goto after_log

:print_log
type "%LOG_PATH%"

:after_log
echo.
if "%INSTALL_EXIT_CODE%"=="0" goto success

echo Installation failed. Send this log file for debugging:
echo %LOG_PATH%
echo.
pause
exit /b %INSTALL_EXIT_CODE%

:success
echo Installation finished successfully.
echo.
pause
exit /b 0

:missing_ps1
echo ERROR: install.ps1 not found at %PS1_PATH%
echo Keep this .bat file next to the VirtualManagerFiles folder.
echo.
pause
exit /b 1
