REM First, try to remove the service if it already exists
REM Ignore any errors from this script
call UninstallService.cmd

sc create "Focusrite Control Server" binPath= "\"%~dp0ControlServer.exe\" service" start= auto
if %errorlevel% NEQ 0 exit /b 1

sc description "Focusrite Control Server" "Process that listens for Focusrite devices and drivers and reports them to the Focusrite Control application. This must be running for Focusrite Control to function"

REM reset= After this many seconds, the server will reset its 'number of fails' counter
REM actions= This determines what will happen after the first, second, and third failures (in milliseconds)
sc failure "Focusrite Control Server" reset= 60 actions= restart/10000/restart/10000/restart/10000
if %errorlevel% NEQ 0 exit /b 2

REM Indicates that if the service fails it can usually be restarted without any problem.
sc failureflag "Focusrite Control Server" 1
if %errorlevel% NEQ 0 exit /b 3

REM Might as well start the server now...
net start "Focusrite Control Server"
if %errorlevel% NEQ 0 exit /b 4