@echo off
setlocal EnableExtensions EnableDelayedExpansion

rem Company Workpuls is a 32-bit per-machine MSI. Double-clicking this file
rem requests administrator permission and keeps the result on screen.

fltmc >nul 2>&1
if not errorlevel 1 goto :Run

echo Administrator permission is required.
echo Approve the Windows prompt to continue.
> "%TEMP%\Workpuls-elevate.vbs" echo Set UAC = CreateObject("Shell.Application")
>> "%TEMP%\Workpuls-elevate.vbs" echo UAC.ShellExecute "%~f0", "", "", "runas", 1
cscript //nologo "%TEMP%\Workpuls-elevate.vbs"
if errorlevel 1 (
    echo Could not request administrator permission.
    echo Right-click this file and choose Run as administrator.
    pause
)
del /f /q "%TEMP%\Workpuls-elevate.vbs" >nul 2>&1
exit /b 0

:Run
call :Main
set "RC=%ERRORLEVEL%"
echo.%CMDCMDLINE%| find /i "/c " >nul && pause
exit /b %RC%

:Main
set "APP_NAME=Workpuls"
set "PRODUCT_CODE="
set "REG_VIEW="
set "FAILED=0"

echo Finding %APP_NAME%...
call :FindInView 32
if not defined PRODUCT_CODE call :FindInView 64

if not defined PRODUCT_CODE (
    echo %APP_NAME% not found.
    exit /b 0
)

echo Found %APP_NAME% %PRODUCT_CODE% in the %REG_VIEW%-bit registry view.
echo Stopping %APP_NAME%...
sc config Workpuls start= disabled >nul 2>&1
net stop "Workpuls" >nul 2>&1
taskkill /F /T /IM "Workpuls.exe" >nul 2>&1
timeout /t 3 /nobreak >nul

set "MSIEXE=%SystemRoot%\System32\msiexec.exe"
if "%REG_VIEW%"=="32" if exist "%SystemRoot%\SysWOW64\msiexec.exe" set "MSIEXE=%SystemRoot%\SysWOW64\msiexec.exe"

echo Uninstalling using %PRODUCT_CODE% ...
set "LOG=%TEMP%\Workpuls-uninstall.log"
start /wait "" "%MSIEXE%" /x "%PRODUCT_CODE%" /qn /norestart /l*v "%LOG%"
set "MSIEXIT=%ERRORLEVEL%"
echo msiexec exit code: %MSIEXIT%
echo Log: %LOG%

set "MSI_OK=0"
if "%MSIEXIT%"=="0" set "MSI_OK=1"
if "%MSIEXIT%"=="1605" set "MSI_OK=1"
if "%MSIEXIT%"=="1641" set "MSI_OK=1"
if "%MSIEXIT%"=="3010" set "MSI_OK=1"
if "%MSI_OK%"=="0" (
    echo ERROR: msiexec failed.
    set "FAILED=1"
)
if "%MSIEXIT%"=="1641" echo NOTE: a restart is required to finish the uninstall.
if "%MSIEXIT%"=="3010" echo NOTE: a restart is required to finish the uninstall.

echo Waiting 15 seconds...
timeout /t 15 /nobreak >nul

reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\%PRODUCT_CODE%" /reg:%REG_VIEW% >nul 2>&1
if not errorlevel 1 (
    echo ERROR: %APP_NAME% still appears installed after uninstall.
    set "FAILED=1"
) else (
    echo %APP_NAME% not detected as installed.
)

if "%FAILED%"=="1" (
    echo Workpuls was stopped and left disabled because uninstall did not finish.
    echo DONE: Completed with errors.
    exit /b 1
)

echo DONE: Completed successfully.
exit /b 0

:FindInView
set "CANDIDATE="
for /f "usebackq delims=" %%L in (`reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" /reg:%~1 /s /f "%APP_NAME%" /d 2^>nul`) do set "LINE=%%L" & call :HandleLine %~1
exit /b 0

:HandleLine
if defined PRODUCT_CODE exit /b 0
if /i "!LINE:~0,5!"=="HKEY_" (
    set "CANDIDATE=!LINE!"
    exit /b 0
)
if not defined CANDIDATE exit /b 0
echo !LINE! | findstr /i /c:"DisplayName" >nul
if errorlevel 1 exit /b 0
echo !LINE! | findstr /i /c:"%APP_NAME%" >nul
if errorlevel 1 exit /b 0
call :ConfirmCandidate %~1
exit /b 0

:ConfirmCandidate
set "VIEW=%~1"
set "PUB="
for /f "usebackq tokens=1,2,*" %%A in (`reg query "!CANDIDATE!" /v Publisher /reg:%VIEW% 2^>nul`) do if /i "%%A"=="Publisher" set "PUB=%%C"
if defined PUB (
    echo !PUB! | findstr /i /c:"Microsoft" >nul
    if not errorlevel 1 (
        set "CANDIDATE="
        exit /b 0
    )
)
call :ExtractCode
if defined PRODUCT_CODE set "REG_VIEW=%VIEW%"
exit /b 0

:ExtractCode
set "REST=!CANDIDATE!"
:NextPiece
for /f "tokens=1* delims=\" %%A in ("!REST!") do (
    if "%%B"=="" (
        set "LEAF=%%A"
    ) else (
        set "REST=%%B"
        goto :NextPiece
    )
)
call :RTrimLeaf
call :IsGuid "%LEAF%"
if defined PRODUCT_CODE exit /b 0

set "UNINST="
for /f "usebackq tokens=1,2,*" %%A in (`reg query "!CANDIDATE!" /v UninstallString /reg:%VIEW% 2^>nul`) do if /i "%%A"=="UninstallString" set "UNINST=%%C"
if not defined UNINST exit /b 0
set "U=!UNINST!"
:ScanBrace
if "!U!"=="" exit /b 0
if "!U:~0,1!"=="{" (
    call :IsGuid "!U:~0,38!"
    exit /b 0
)
set "U=!U:~1!"
goto :ScanBrace

:RTrimLeaf
if not "!LEAF:~-1!"==" " exit /b 0
set "LEAF=!LEAF:~0,-1!"
goto :RTrimLeaf

:IsGuid
set "G=%~1"
if not "!G:~0,1!"=="{" exit /b 0
if not "!G:~37,1!"=="}" exit /b 0
if not "!G:~38,1!"=="" exit /b 0
if not "!G:~9,1!"=="-" exit /b 0
if not "!G:~14,1!"=="-" exit /b 0
if not "!G:~19,1!"=="-" exit /b 0
if not "!G:~24,1!"=="-" exit /b 0
set "PRODUCT_CODE=!G!"
exit /b 0
