@echo off
:: ============================================================
:: MIBService Installation Script with Auto-Elevation
:: ============================================================

::::::::::::::::::::::::::::::::::::::::::::
:: 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%" )

::::::::::::::::::::::::::::::::::::::::::::
:: START MAIN INSTALLATION SCRIPT
::::::::::::::::::::::::::::::::::::::::::::

cls
echo.
echo ========================================
echo MIBService Installation Script
echo ========================================
echo Administrator privileges confirmed.
echo.

:: Set service details
set SERVICE_NAME=MIBService
set SERVICE_DISPLAY_NAME=MIB Production Service
set SERVICE_DESCRIPTION=MIB Production Security and Monitoring Service
set SERVICE_EXE=%~dp0MIBService.exe

:: Verify the service executable exists
if not exist "%SERVICE_EXE%" (
    echo.
    echo ERROR: Service executable not found!
    echo Expected location: %SERVICE_EXE%
    echo.
    exit /b 1
)

echo [1/5] Service executable found: %SERVICE_EXE%

:: Check if service is already running
sc query %SERVICE_NAME% >nul 2>&1
if %errorlevel% equ 0 (
    echo [2/5] Service already exists. Stopping and removing...
    
    :: Stop the service if it's running
    sc query %SERVICE_NAME% | find "RUNNING" >nul
    if %errorlevel% equ 0 (
        echo     - Stopping service...
        net stop %SERVICE_NAME% >nul 2>&1
        timeout /t 3 /nobreak >nul
    )
    
    :: Delete the existing service
    echo     - Removing existing service...
    sc delete %SERVICE_NAME% >nul 2>&1
    timeout /t 2 /nobreak >nul
) else (
    echo [2/5] No existing service found.
)

:: Install the service using sc create
echo [3/5] Installing service...
sc create %SERVICE_NAME% binPath= "\"%SERVICE_EXE%\"" DisplayName= "%SERVICE_DISPLAY_NAME%" start= auto error= normal type= own >nul 2>&1

if %errorlevel% neq 0 (
    echo.
    echo ERROR: Failed to create service!
    echo Error code: %errorlevel%
    exit /b 1
)
echo     [OK] Service created successfully.

:: Set service description
sc description %SERVICE_NAME% "%SERVICE_DESCRIPTION%" >nul
echo     [OK] Service description set.

:: Configure service recovery options (restart on failure)
sc failure %SERVICE_NAME% reset= 86400 actions= restart/60000/restart/60000/restart/60000 >nul
sc failureflag %SERVICE_NAME% 1 >nul
echo     [OK] Auto-restart on failure configured.

:: Start the service
echo [4/5] Starting service...
net start %SERVICE_NAME% >nul 2>&1

if %errorlevel% neq 0 (
    echo.
    echo ERROR: Failed to start service!
    echo Check the Event Viewer for more details.
    echo Log location: %~dp0Logs\
    echo.
    exit /b 1
)
echo     [OK] Service started.

:: Verify service is running
echo [5/5] Verifying service status...
timeout /t 2 /nobreak >nul
sc query %SERVICE_NAME% | find "RUNNING" >nul
if %errorlevel% equ 0 (
    echo     [OK] Service is running.
    echo.
    echo ========================================
    echo  SUCCESS!
    echo ========================================
    echo.
    echo Service Name:     %SERVICE_NAME%
    echo Display Name:     %SERVICE_DISPLAY_NAME%
    echo Status:           Running
    echo Startup Type:     Automatic
    echo.
    exit /b 0
) else (
    echo.
    echo WARNING: Service installed but may not be running.
    echo Check logs at: %~dp0Logs\
    echo.
    exit /b 1
)
