@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

set "CURRENT_DIR=%cd%"
rem Set default Service name
set SERVICE_NAME=%2

if "x%1x" == "xx" goto displayUsage
if "x%2x" == "xx" goto displayUsage
if "x%3x" == "xx" goto displayUsage
if "%1" == "install" (
	if x%4x == xx goto displayUsage
	if x%5x == xx goto displayUsage
) 
set TOMCATVERSION=%3
set DISPLAYNAME=%4
CALL :dequote DISPLAYNAME
set DESCRIPTION=%5
CALL :dequote DESCRIPTION
set JVMARGS=%6
CALL :dequote JVMARGS
set JvmMs=%7
CALL :dequote JvmMs
if "%JvmMs%" == "" set JvmMs=2048
set JvmMx=%8
CALL :dequote JvmMx
if "%JvmMx%" == "" set JvmMx=2048
set JvmSs=%9
CALL :dequote JvmSs
if "%JvmSs%" == "" set JvmSs=0

@echo TOMCATVERSION = %TOMCATVERSION%
@echo DISPLAYNAME = %DISPLAYNAME%
@echo DESCRIPTION = %DESCRIPTION%
@echo JVMARGS=%JVMARGS%
@echo JvmMs=%JvmMs%
@echo JvmMx=%JvmMx%
@echo JvmSs=%JvmSs%

rem Guess CATALINA_HOME if not defined
if not "%CATALINA_HOME%" == "" goto gotHome
set "CATALINA_HOME=%cd%"
if exist "%CATALINA_HOME%\bin\tomcat%TOMCATVERSION%.exe" goto okHome
rem CD to the upper dir
cd ..
set "CATALINA_HOME=%cd%"
:gotHome
if exist "%CATALINA_HOME%\bin\tomcat%TOMCATVERSION%.exe" goto okHome
@echo The tomcat%TOMCATVERSION%.exe was not found...
@echo The CATALINA_HOME environment variable is not defined correctly.
@echo This environment variable is needed to run this program
goto end
:okHome
rem Make sure prerequisite environment variables are set
if not "%JAVA_HOME%" == "" goto gotJdkHome
@echo JAVA_HOME environment variable is not defined. this should be a jdk
@echo Service will try to guess them from the registry.
goto okJavaHome
:gotJdkHome
if not exist "%JAVA_HOME%\jre\bin\java.exe" goto noJavaHome
if not exist "%JAVA_HOME%\jre\bin\javaw.exe" goto noJavaHome
if not exist "%JAVA_HOME%\bin\javac.exe" goto noJavaHome
set "JRE_HOME=%JAVA_HOME%\jre"
goto okJavaHome
:noJavaHome
@echo The JAVA_HOME environment variable is not defined correctly
@echo This environment variable is needed to run this program
@echo NB: JAVA_HOME should point to a JDK not a JRE
goto end
:okJavaHome

if not "%CATALINA_BASE%" == "" goto gotBase
set "CATALINA_BASE=%CATALINA_HOME%"
:gotBase
 
set "EXECUTABLE=%CATALINA_HOME%\bin\tomcat%TOMCATVERSION%.exe"

:setServiceName
if %1 == install goto doInstall
if %1 == remove goto doRemove
if %1 == uninstall goto doRemove
@echo Unknown parameter "%1"
:displayUsage
@echo Usage: esservice.bat install/remove service_name tomcat_version (e.g. 8 or 9) [service_displayname] [service_description] 
goto end

:doRemove
rem Remove the service
@echo Removing the service '%SERVICE_NAME%' ...
@echo Using CATALINA_BASE:    "%CATALINA_BASE%"

"%EXECUTABLE%" //DS//%SERVICE_NAME% ^
    --LogPath "%CATALINA_BASE%\logs"
if not errorlevel 1 goto removed
echo Failed removing '%SERVICE_NAME%' service
goto end
:removed
echo The service '%SERVICE_NAME%' has been removed
goto end

:doInstall
rem Install the service
@echo Installing the service '%SERVICE_NAME%' ...
@echo Using CATALINA_HOME:    "%CATALINA_HOME%"
@echo Using CATALINA_BASE:    "%CATALINA_BASE%"
@echo Using JAVA_HOME:        "%JAVA_HOME%"

rem Try to use the server jvm
set "JVM=%JRE_HOME%\bin\server\jvm.dll"
if exist "%JVM%" goto foundJvm
rem Try to use the client jvm
set "JVM=%JRE_HOME%\bin\client\jvm.dll"
if exist "%JVM%" goto foundJvm
@echo Warning: Neither 'server' nor 'client' jvm.dll was found at JRE_HOME.
set JVM=auto
:foundJvm
@echo Using JVM:              "%JVM%"

set "CLASSPATH=%CATALINA_HOME%\bin\bootstrap.jar;%CATALINA_BASE%\bin\tomcat-juli.jar"
if not "%CATALINA_HOME%" == "%CATALINA_BASE%" set "CLASSPATH=%CLASSPATH%;%CATALINA_HOME%\bin\tomcat-juli.jar"

if "%SERVICE_STARTUP_MODE%" == "" set SERVICE_STARTUP_MODE=auto

@echo "%EXECUTABLE%" //IS//%SERVICE_NAME% ^
    --Description "%DESCRIPTION%" ^
    --DisplayName "%DISPLAYNAME%" ^
    --Install "%EXECUTABLE%" ^
    --LogPath "%CATALINA_BASE%\logs" ^
    --StdOutput auto ^
    --StdError auto ^
    --Classpath "%CLASSPATH%" ^
    --Jvm "%JVM%" ^
    --StartMode jvm ^
    --StopMode jvm ^
    --StartPath "%CATALINA_HOME%" ^
    --StopPath "%CATALINA_HOME%" ^
    --StartClass org.apache.catalina.startup.Bootstrap ^
    --StopClass org.apache.catalina.startup.Bootstrap ^
    --StartParams start ^
    --StopParams stop ^
	--ServiceUser "NT Authority\LocalService" ^
    --JvmOptions "-Dcatalina.home=%CATALINA_HOME%;-Dcatalina.base=%CATALINA_BASE%;-Djava.endorsed.dirs=%CATALINA_HOME%\endorsed;-Djava.io.tmpdir=%CATALINA_BASE%\temp;-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager;-Djava.util.logging.config.file=%CATALINA_BASE%\conf\logging.properties;%JVMARGS%" ^
    --Startup "%SERVICE_STARTUP_MODE%" ^
    --JvmMs "%JvmMs%" ^
    --JvmMx "%JvmMx%" ^
    --JvmSs "%JvmSs%"

"%EXECUTABLE%" //IS//%SERVICE_NAME% ^
    --Description "%DESCRIPTION%" ^
    --DisplayName "%DISPLAYNAME%" ^
    --Install "%EXECUTABLE%" ^
    --LogPath "%CATALINA_BASE%\logs" ^
    --StdOutput auto ^
    --StdError auto ^
    --Classpath "%CLASSPATH%" ^
    --Jvm "%JVM%" ^
    --StartMode jvm ^
    --StopMode jvm ^
    --StartPath "%CATALINA_HOME%" ^
    --StopPath "%CATALINA_HOME%" ^
    --StartClass org.apache.catalina.startup.Bootstrap ^
    --StopClass org.apache.catalina.startup.Bootstrap ^
    --StartParams start ^
    --StopParams stop ^
	--ServiceUser "NT Authority\LocalService" ^
    --JvmOptions "-Dcatalina.home=%CATALINA_HOME%;-Dcatalina.base=%CATALINA_BASE%;-Djava.endorsed.dirs=%CATALINA_HOME%\endorsed;-Djava.io.tmpdir=%CATALINA_BASE%\temp;-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager;-Djava.util.logging.config.file=%CATALINA_BASE%\conf\logging.properties;%JVMARGS%" ^
    --Startup "%SERVICE_STARTUP_MODE%" ^
    --JvmMs "%JvmMs%" ^
    --JvmMx "%JvmMx%" ^
    --JvmSs "%JvmSs%"

if not errorlevel 1 goto installed
@echo Failed installing '%SERVICE_NAME%' service
goto end
:installed
echo The service '%SERVICE_NAME%' has been installed.

:end
cd "%CURRENT_DIR%"
goto :EOF


:DeQuote 

SET _DeQuoteVar=%1
CALL SET _DeQuoteString=%%!_DeQuoteVar!%%
IF [!_DeQuoteString:~0^,1!]==[^"] (
IF [!_DeQuoteString:~-1!]==[^"] (SET _DeQuoteString=!_DeQuoteString:~1,-1!
) ELSE (GOTO :EOF)
) ELSE (GOTO :EOF)
SET !_DeQuoteVar!=!_DeQuoteString!
SET _DeQuoteVar=
SET _DeQuoteString=
GOTO :EOF
