@echo off

REM Set variables
SET CURRENT_DIR=%~dp0
SET SERVICE_EXECUTABLE=UPScom.App.Server.exe
SET SERVICE_NAME=UPScom
SET SERVICE_DISPLAYNAME=UPScom Service Manager
SET SERVICE_FAILURE_RESET=reset= 600
SET SERVICE_FAILURE_ACTION=actions= restart/10000

REM Check If Service Exists
sc.exe query %SERVICE_NAME% > nul
if errorlevel 1 goto :INSTALL_SERVICE
goto :DELETE_INSTALLED_SERVICE

REM Delete the installed service
:DELETE_INSTALLED_SERVICE
echo Service '%SERVICE_NAME%' allready installed. Service will be stopped...
sc.exe stop %SERVICE_NAME% > nul
REM if errorlevel 1 goto :ERROR
echo Service 'SERVICE_NAME%' will be deleted...
sc.exe delete %SERVICE_NAME%
if errorlevel 1 goto :ERROR

REM Install UPS-Service
:INSTALL_SERVICE
echo Create new service '%SERVICE_NAME%'...
sc.exe create %SERVICE_NAME% DisplayName= "%SERVICE_DISPLAYNAME%" binpath= "%CURRENT_DIR%%SERVICE_EXECUTABLE%" start= auto
if errorlevel 1 goto :ERROR
echo Add description to service '%SERVICE_NAME%'...
sc.exe description %SERVICE_NAME% "Uninterruptible Power Supply Management Service"
if errorlevel 1 goto :ERROR
echo Configure service '%SERVICE_NAME%' failure handling...
sc.exe failure %SERVICE_NAME% %SERVICE_FAILURE_RESET% %SERVICE_FAILURE_ACTION% %SERVICE_FAILURE_ACTION% %SERVICE_FAILURE_ACTION%
if errorlevel 1 goto :ERROR

:START_SERVICE
echo Start service '%SERVICE_NAME%'...
sc.exe start %SERVICE_NAME%
if errorlevel 1 goto :ERROR
goto :EXIT

:ERROR
echo An error occurred during the installation (%errorlevel%).

:EXIT
pause
