REM 
REM -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
REM

ECHO OFF

REM Main script for setting up the web server, including the domain, and preparing the related windows services
REM During the process, various hard coded values in the files will be replaced with the variables defined in this batch file. These files are all copied from the \org

SET ABS_WEBSERVERHOME=%1
SET ABS_WEBSERVERSLOT=%2
SET ABS_WEBSERVERSERVICENAME=%3

REM 
REM -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
REM

REM Prepare the Windows service config file for the Tomcat
ECHO
ECHO Prepare the Windows service config file for the Tomcat %ABS_WEBSERVERSERVICENAME% %ABS_WEBSERVERSLOT% 
ECHO
REM Work from the related .org file, this is the default
copy /Y %ABS_WEBSERVERHOME%custom\conf\server.xml %ABS_WEBSERVERHOME%conf\server.xml
copy /Y %ABS_WEBSERVERHOME%custom\conf\tomcat-users.xml %ABS_WEBSERVERHOME%conf\tomcat-users.xml
copy /Y %ABS_WEBSERVERHOME%custom\conf\localhost.p12 %ABS_WEBSERVERHOME%conf\localhost.p12
copy /Y %ABS_WEBSERVERHOME%custom\service\service.bat %ABS_WEBSERVERHOME%bin\service.bat

REM 
REM -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
REM

REM Uninstall the Windows service for the domain
ECHO [...] Stopping Tomcat service...
net stop "%ABS_WEBSERVERSERVICENAME%" >nul 2>&1

ECHO Scanning and Kill process %ABS_WEBSERVERSERVICENAME% if present
Title Scanning and Kill Tomcat Service
for /f "tokens=2 delims=:" %%b  in ('sc queryex "%ABS_WEBSERVERSERVICENAME%" ^|findstr PID') Do (
        if [%%b]==[%%b] (
            echo  Killing %%b ...
            Taskkill /f /fi "SERVICES eq %ABS_WEBSERVERSERVICENAME%"
        ) else (
            echo %ABS_WEBSERVERSERVICENAME% is not running
        )
    )
)
PING 127.0.0.1 -n 3 >nul

ECHO Deleting service %ABS_WEBSERVERSERVICENAME%...
sc delete "%ABS_WEBSERVERSERVICENAME%" >nul

REM 
REM -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
REM

REM Prepare the files needed for deployment

ECHO
ECHO Delete Deploy files
ECHO
REM Work from the related .org file, this is the default
rmdir /s /q %ABS_WEBSERVERHOME%deploy
mkdir %ABS_WEBSERVERHOME%deploy
del /F %ABS_WEBSERVERHOME%custom\deploy\AbsSnmpAgentSettings.properties
del /F %ABS_WEBSERVERHOME%custom\deploy\snmpwalk.bat
REM 
REM -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
REM
REM Cleanup Tomcat

call %ABS_WEBSERVERHOME%custom\bin\CleanTomcat.bat %ABS_WEBSERVERHOME%

EXIT