@echo off
::::::::::::::::::::::::::::::::::::::::::::
:: Automatically check & get admin rights
::::::::::::::::::::::::::::::::::::::::::::
:init
setlocal DisableDelayedExpansion
set cmdInvoke=1
set "batchPath=%~f0"
for %%k in (%0) do set batchName=%%~nk
set "vbsGetPrivileges=%temp%\OEgetPriv_%batchName%.vbs"
setlocal EnableDelayedExpansion

:checkPrivileges
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges )

:getPrivileges
if '%1'=='ELEV' (echo ELEV & shift /1 & goto gotPrivileges)

echo.
echo ========================================
echo Requesting Administrator Privileges...
echo ========================================
echo.

ECHO Set UAC = CreateObject^("Shell.Application"^) > "%vbsGetPrivileges%"
ECHO args = "ELEV " >> "%vbsGetPrivileges%"
ECHO For Each strArg in WScript.Arguments >> "%vbsGetPrivileges%"
ECHO args = args ^& strArg ^& " "  >> "%vbsGetPrivileges%"
ECHO Next >> "%vbsGetPrivileges%"
ECHO UAC.ShellExecute "cmd.exe", "/c """ + WScript.Arguments(0) + """ " + args, "", "runas", 1 >> "%vbsGetPrivileges%"

"%SystemRoot%\System32\WScript.exe" "%vbsGetPrivileges%" "%batchPath%"
exit /B

:gotPrivileges
setlocal & cd /d %~dp0
if '%1'=='ELEV' ( shift /1 )

REM Delete the temporary VBS file if it exists
if exist "%vbsGetPrivileges%" ( del "%vbsGetPrivileges%" )

echo ============================================================
echo MIBService Uninstaller
echo ============================================================
echo.

set SERVICE_NAME=MIBService
set SERVICE_EXE=MIBService.exe

echo [1/3] Checking if service exists...
sc query %SERVICE_NAME% >nul 2>&1
if %errorLevel% neq 0 (
    echo    Service not found. Nothing to uninstall.
) else (
    echo    Service found. Proceeding with removal...

    echo.
    echo [2/3] Attempting to stop service...
    sc stop %SERVICE_NAME% >nul 2>&1
    timeout /t 3 /nobreak >nul

    echo    Forcefully terminating service process...
    taskkill /F /IM %SERVICE_EXE% >nul 2>&1
    timeout /t 2 /nobreak >nul

    echo.
    echo [3/3] Deleting service...
    sc delete %SERVICE_NAME%
    if %errorLevel% equ 0 (
        echo    Service deleted successfully!
    ) else (
        echo    WARNING: Service deletion may have failed. Error code: %errorLevel%
    )
    timeout /t 2 /nobreak >nul
)

echo.
echo ============================================================
echo UNINSTALLATION COMPLETE!
echo ============================================================
echo.
echo Service has been stopped and removed from the system.
echo.

exit /b 0
