ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
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 PATH=%PATH%;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem
SET ABS_WEBSERVERHOME=%1
SET ABS_WEBSERVERSLOT=%2
SET ABS_WEBSERVERSERVICENAME=%3
SET ABS_WEBSERVERMEMORY=%4

REM 
REM -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
REM
REM ======= DISPLAY VERSION =======
ECHO.
ECHO [...] Checking installed Tomcat version...
SET "VERSION_FOUND="
SET "TCVERS="

if exist "%ABS_WEBSERVERHOME%\RELEASE-NOTES" (
    for /f "tokens=* delims=" %%A in ('findstr /C:"Apache Tomcat Version" "%ABS_WEBSERVERHOME%\RELEASE-NOTES"') do (
        SET "VERSION_FOUND=%%A"
    )

    if defined VERSION_FOUND (
        REM Extract just the numbers from VERSION_FOUND
        for /f "tokens=4" %%B in ("!VERSION_FOUND!") do SET "TCVERS=%%B"
        ECHO New Installed: !VERSION_FOUND!
        ECHO version number is: !TCVERS!
    ) ELSE (
        ECHO Version string not found in RELEASE-NOTES.
    )
) ELSE (
    ECHO RELEASE-NOTES file not found.
)

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 ======= DETECT VERSION =======
SET "INI_FILE=%ABS_WEBSERVERHOME%\ABSSolute_version.ini"
IF NOT EXIST "%INI_FILE%" (
    ECHO ERROR: Version file not found: %INI_FILE%
    GOTO End
)
FOR /F "tokens=4" %%A IN ('findstr /C:"ABSSOLUTE VERSION IS:" "%INI_FILE%"') DO (
    SET "ABS_VERSION=%%A"
)
IF NOT DEFINED ABS_VERSION (
    ECHO ERROR: Could not detect ABSOLUTE VERSION from INI file.
    GOTO End
)
ECHO Detected ABSOLUTE VERSION: %ABS_VERSION%
FOR /F "tokens=1-3 delims=." %%A IN ("%ABS_VERSION%") DO (
    SET /A VER_MAJOR=%%A
    SET /A VER_MINOR=1%%B - 100
    SET /A VER_PATCH=1%%C - 100
)
SET /A VER_NUMBER=%VER_MAJOR%*10000 + %VER_MINOR%*100 + %VER_PATCH%

REM ======= DECIDE Service VERSION =======
SET "TOMCAT_SERVER=server.xml"
IF %VER_NUMBER% LSS 90800 SET "TOMCAT_SERVER=old\server.xml"
ECHO Using Tomcat Server version: %TOMCAT_SERVER%

REM Work from the related .org file, this is the default
copy /Y %ABS_WEBSERVERHOME%custom\conf\%TOMCAT_SERVER% %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
call %ABS_WEBSERVERHOME%custom\bin\replace_values_in_file.bat %ABS_WEBSERVERHOME% [ABS_WEBSERVERSLOT] %ABS_WEBSERVERSLOT% %ABS_WEBSERVERHOME%conf\server.xml
call %ABS_WEBSERVERHOME%custom\bin\replace_values_in_file.bat %ABS_WEBSERVERHOME% [ABS_WEBSERVERMEMORY] %ABS_WEBSERVERMEMORY% %ABS_WEBSERVERHOME%bin\service.bat
call %ABS_WEBSERVERHOME%custom\bin\replace_values_in_file.bat %ABS_WEBSERVERHOME% [TCVERS] %TCVERS% %ABS_WEBSERVERHOME%bin\service.bat

REM 
REM -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
REM Extract the first IP address from the Windows MAchine
FOR /f "tokens=1-2 delims=:" %%a in ('ipconfig^|find "IPv4"') do SET ip=%%b
SET LOCALHOSTIP=%ip:~1%

copy /Y %ABS_WEBSERVERHOME%custom\install\org\AbsSnmpAgentSettings.properties %ABS_WEBSERVERHOME%custom\deploy\AbsSnmpAgentSettings.properties
copy /Y %ABS_WEBSERVERHOME%custom\install\org\snmpwalk.bat %ABS_WEBSERVERHOME%custom\deploy\snmpwalk.bat
call %ABS_WEBSERVERHOME%custom\bin\replace_values_in_file.bat %ABS_WEBSERVERHOME% [LOCALHOSTIP] %LOCALHOSTIP% %ABS_WEBSERVERHOME%custom\deploy\AbsSnmpAgentSettings.properties
call %ABS_WEBSERVERHOME%custom\bin\replace_values_in_file.bat %ABS_WEBSERVERHOME% [LOCALHOSTIP] %LOCALHOSTIP% %ABS_WEBSERVERHOME%custom\deploy\snmpwalk.bat
REM 
REM -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
call %ABS_WEBSERVERHOME%custom\install\jre_install.bat %ABS_WEBSERVERHOME%
REM
REM Install the Windows service for the domain
ECHO
ECHO Install the Windows service for the domain %ABS_WEBSERVERSERVICENAME%
ECHO
call "%ABS_WEBSERVERHOME%\bin\service.bat" install "%ABS_WEBSERVERSERVICENAME%"
ECHO Starting service %ABS_WEBSERVERSERVICENAME%
net start "%ABS_WEBSERVERSERVICENAME%"

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

REM Prepare the files needed for deployment

ECHO
ECHO Prepare the files needed for deployment
ECHO
ECHO Prepare the file for deploying the web components
REM Work from the related .org file, this is the default
rmdir /s /q %ABS_WEBSERVERHOME%deploy
mkdir %ABS_WEBSERVERHOME%deploy
mkdir %ABS_WEBSERVERHOME%deploy\CustomSkin

robocopy %ABS_WEBSERVERHOME%custom\install\org\deploy\CustomSkin %ABS_WEBSERVERHOME%deploy\CustomSkin /E /NFL /NDL /NJH /NJS /nc /ns /np
REM ======= DECIDE Deploy VERSION =======
SET "DEPLOY=64"
SET "TOMCAT_DEPLOY=new\deploy_all_webcomponents_for_slot_xx_run_as_administrator.bat.org"
IF %VER_NUMBER% LSS 90800 SET "DEPLOY=34"
IF %VER_NUMBER% LSS 90800 SET "TOMCAT_DEPLOY=deploy_all_webcomponents_for_slot_xx_run_as_administrator.bat.org"
ECHO Using Tomcat Server version: %TOMCAT_DEPLOY%
ECHO Using Tomcat Deploy version: %DEPLOY%
copy %ABS_WEBSERVERHOME%custom\install\org\deploy\%TOMCAT_DEPLOY% %ABS_WEBSERVERHOME%deploy\deploy_all_webcomponents_for_slot_%ABS_WEBSERVERSLOT%_run_as_administrator.bat
call %ABS_WEBSERVERHOME%custom\bin\replace_values_in_file.bat %ABS_WEBSERVERHOME% [ABS_WEBSERVERHOME] %ABS_WEBSERVERHOME% %ABS_WEBSERVERHOME%deploy\deploy_all_webcomponents_for_slot_%ABS_WEBSERVERSLOT%_run_as_administrator.bat
call %ABS_WEBSERVERHOME%custom\bin\replace_values_in_file.bat %ABS_WEBSERVERHOME% [ABS_WEBSERVERSLOT] %ABS_WEBSERVERSLOT% %ABS_WEBSERVERHOME%deploy\deploy_all_webcomponents_for_slot_%ABS_WEBSERVERSLOT%_run_as_administrator.bat
call %ABS_WEBSERVERHOME%custom\bin\replace_values_in_file.bat %ABS_WEBSERVERHOME% [DEPLOY] %DEPLOY% %ABS_WEBSERVERHOME%deploy\deploy_all_webcomponents_for_slot_%ABS_WEBSERVERSLOT%_run_as_administrator.bat
call %ABS_WEBSERVERHOME%custom\bin\replace_values_in_file.bat %ABS_WEBSERVERHOME% [ABS_WEBSERVERSERVICENAME] %ABS_WEBSERVERSERVICENAME% %ABS_WEBSERVERHOME%deploy\deploy_all_webcomponents_for_slot_%ABS_WEBSERVERSLOT%_run_as_administrator.bat

REM Tomcat Update
copy %ABS_WEBSERVERHOME%custom\install\org\deploy\update_tomcat9_for_slot_xx_run_as_administrator.bat_org %ABS_WEBSERVERHOME%deploy\update_tomcat9_for_slot_%ABS_WEBSERVERSLOT%_run_as_administrator.bat
call %ABS_WEBSERVERHOME%custom\bin\replace_values_in_file.bat %ABS_WEBSERVERHOME% [ABS_WEBSERVERHOME] %ABS_WEBSERVERHOME% %ABS_WEBSERVERHOME%deploy\update_tomcat9_for_slot_%ABS_WEBSERVERSLOT%_run_as_administrator.bat
call %ABS_WEBSERVERHOME%custom\bin\replace_values_in_file.bat %ABS_WEBSERVERHOME% [ABS_WEBSLOT] %ABS_WEBSERVERSLOT% %ABS_WEBSERVERHOME%deploy\update_tomcat9_for_slot_%ABS_WEBSERVERSLOT%_run_as_administrator.bat
call %ABS_WEBSERVERHOME%custom\bin\replace_values_in_file.bat %ABS_WEBSERVERHOME% [ABS_WEBSERVERSERVICENAME] %ABS_WEBSERVERSERVICENAME% %ABS_WEBSERVERHOME%deploy\update_tomcat9_for_slot_%ABS_WEBSERVERSLOT%_run_as_administrator.bat
call %ABS_WEBSERVERHOME%custom\bin\replace_values_in_file.bat %ABS_WEBSERVERHOME% [ABS_WEBMEMORY] %ABS_WEBSERVERMEMORY% %ABS_WEBSERVERHOME%deploy\update_tomcat9_for_slot_%ABS_WEBSERVERSLOT%_run_as_administrator.bat

REM Java Update
copy %ABS_WEBSERVERHOME%custom\install\org\deploy\jre_update_for_slot_xx_run_as_administrator.bat_org %ABS_WEBSERVERHOME%deploy\jre_update_for_slot_%ABS_WEBSERVERSLOT%_run_as_administrator.bat
call %ABS_WEBSERVERHOME%custom\bin\replace_values_in_file.bat %ABS_WEBSERVERHOME% [ABS_WEBSERVERHOME] %ABS_WEBSERVERHOME% %ABS_WEBSERVERHOME%deploy\jre_update_for_slot_%ABS_WEBSERVERSLOT%_run_as_administrator.bat
call %ABS_WEBSERVERHOME%custom\bin\replace_values_in_file.bat %ABS_WEBSERVERHOME% [ABS_WEBSLOT] %ABS_WEBSERVERSLOT% %ABS_WEBSERVERHOME%deploy\jre_update_for_slot_%ABS_WEBSERVERSLOT%_run_as_administrator.bat
call %ABS_WEBSERVERHOME%custom\bin\replace_values_in_file.bat %ABS_WEBSERVERHOME% [ABS_WEBSERVERSERVICENAME] %ABS_WEBSERVERSERVICENAME% %ABS_WEBSERVERHOME%deploy\jre_update_for_slot_%ABS_WEBSERVERSLOT%_run_as_administrator.bat
REM
copy /Y %ABS_WEBSERVERHOME%custom\install\org\uninstall_Run_as_Administrator.bat %ABS_WEBSERVERHOME%uninstall_Run_as_Administrator.bat
call %ABS_WEBSERVERHOME%custom\bin\replace_values_in_file.bat %ABS_WEBSERVERHOME% [ABS_WEBSERVERSLOT] %ABS_WEBSERVERSLOT% %ABS_WEBSERVERHOME%uninstall_Run_as_Administrator.bat
call %ABS_WEBSERVERHOME%custom\bin\replace_values_in_file.bat %ABS_WEBSERVERHOME% [ABS_WEBSERVERSERVICENAME] %ABS_WEBSERVERSERVICENAME% %ABS_WEBSERVERHOME%uninstall_Run_as_Administrator.bat
 
REM -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
REM

REM ======= DECIDE SLOT VERSION =======
SET "SLOT=6"
IF %VER_NUMBER% LSS 90800 SET "SLOT=3"
ECHO Using Tomcat Server version: %SLOT%

REM Prepare web component short cuts for quick testing
IF %VER_NUMBER% GEQ 90800 (
    REM --- Only new shortcuts ---
    call "%ABS_WEBSERVERHOME%custom\bin\create_shortcut.bat" "%ABS_WEBSERVERHOME%deploy\%ABS_WEBSERVERSLOT%_ADMIN_console.url" 5%ABS_WEBSERVERSLOT%%SLOT%4
    call "%ABS_WEBSERVERHOME%custom\bin\create_shortcut.bat" "%ABS_WEBSERVERHOME%deploy\%ABS_WEBSERVERSLOT%_GenericWebservice.url" 5%ABS_WEBSERVERSLOT%%SLOT%2 absgenericwebservice/ABSGenericWebService
    call "%ABS_WEBSERVERHOME%custom\bin\create_shortcut.bat" "%ABS_WEBSERVERHOME%deploy\%ABS_WEBSERVERSLOT%_ws4MobileApps.url" 5%ABS_WEBSERVERSLOT%%SLOT%2 absws4mobileapps/absws4mobile
    call "%ABS_WEBSERVERHOME%custom\bin\create_shortcut.bat" "%ABS_WEBSERVERHOME%deploy\%ABS_WEBSERVERSLOT%_MobileRestServices.url" 5%ABS_WEBSERVERSLOT%%SLOT%2 absmobilerestservices/
    call "%ABS_WEBSERVERHOME%custom\bin\create_shortcut.bat" "%ABS_WEBSERVERHOME%deploy\%ABS_WEBSERVERSLOT%_RestServices.url" 5%ABS_WEBSERVERSLOT%%SLOT%2 absrestservices
) ELSE (
    REM --- Legacy version, all shortcuts ---
    call "%ABS_WEBSERVERHOME%custom\bin\create_shortcut.bat" "%ABS_WEBSERVERHOME%deploy\%ABS_WEBSERVERSLOT%_ADMIN_console.url" 5%ABS_WEBSERVERSLOT%%SLOT%4
    call "%ABS_WEBSERVERHOME%custom\bin\create_shortcut.bat" "%ABS_WEBSERVERHOME%deploy\%ABS_WEBSERVERSLOT%_GenericWebservice.url" 5%ABS_WEBSERVERSLOT%%SLOT%2 absgenericwebservice/ABSGenericWebService
    call "%ABS_WEBSERVERHOME%custom\bin\create_shortcut.bat" "%ABS_WEBSERVERHOME%deploy\%ABS_WEBSERVERSLOT%_ws4MobileApps.url" 5%ABS_WEBSERVERSLOT%%SLOT%2 absws4mobileapps/absws4mobile
    call "%ABS_WEBSERVERHOME%custom\bin\create_shortcut.bat" "%ABS_WEBSERVERHOME%deploy\%ABS_WEBSERVERSLOT%_MobileRestServices.url" 5%ABS_WEBSERVERSLOT%%SLOT%2 absmobilerestservices/
    call "%ABS_WEBSERVERHOME%custom\bin\create_shortcut.bat" "%ABS_WEBSERVERHOME%deploy\%ABS_WEBSERVERSLOT%_RestServices.url" 5%ABS_WEBSERVERSLOT%%SLOT%2 absrestservices/
    call "%ABS_WEBSERVERHOME%custom\bin\create_shortcut.bat" "%ABS_WEBSERVERHOME%deploy\%ABS_WEBSERVERSLOT%_UHFmonitor.url" 5%ABS_WEBSERVERSLOT%%SLOT%2 absuhfmonitor/
    call "%ABS_WEBSERVERHOME%custom\bin\create_shortcut.bat" "%ABS_WEBSERVERHOME%deploy\%ABS_WEBSERVERSLOT%_WebPortal.url" 5%ABS_WEBSERVERSLOT%%SLOT%2 abswebportal/
    call "%ABS_WEBSERVERHOME%custom\bin\create_shortcut.bat" "%ABS_WEBSERVERHOME%deploy\%ABS_WEBSERVERSLOT%_WebClientx.url" 5%ABS_WEBSERVERSLOT%%SLOT%2 webclientx/
    call "%ABS_WEBSERVERHOME%custom\bin\create_shortcut.bat" "%ABS_WEBSERVERHOME%deploy\%ABS_WEBSERVERSLOT%_WebClientReplaceSmartbarcode.url" 5%ABS_WEBSERVERSLOT%%SLOT%2 webclientreplacesmartbarcode/
    call "%ABS_WEBSERVERHOME%custom\bin\create_shortcut.bat" "%ABS_WEBSERVERHOME%deploy\%ABS_WEBSERVERSLOT%_WebClientResidentialDelivery.url" 5%ABS_WEBSERVERSLOT%%SLOT%2 webclientresidentialdelivery/
    call "%ABS_WEBSERVERHOME%custom\bin\create_shortcut.bat" "%ABS_WEBSERVERHOME%deploy\%ABS_WEBSERVERSLOT%_WebClientRouteReturns.url" 5%ABS_WEBSERVERSLOT%%SLOT%2 webclientroutereturns/
    call "%ABS_WEBSERVERHOME%custom\bin\create_shortcut.bat" "%ABS_WEBSERVERHOME%deploy\%ABS_WEBSERVERSLOT%_WebClientCleanroompackinginstructions.url" 5%ABS_WEBSERVERSLOT%%SLOT%2 webclientcleanroompackinginstructions/
    call "%ABS_WEBSERVERHOME%custom\bin\create_shortcut.bat" "%ABS_WEBSERVERHOME%deploy\%ABS_WEBSERVERSLOT%_ABSMobileFirebaseServices.url" 5%ABS_WEBSERVERSLOT%%SLOT%2 absmobilefirebaseservices/
)
ECHO.
SET /P RUNUPDATE=Do you want to run update tomcat9 to latest version ? (Y/N): 

IF /I "%RUNUPDATE%"=="Y" (
    ECHO Running update...
    CALL "%ABS_WEBSERVERHOME%\deploy\update_tomcat9_for_slot_%ABS_WEBSERVERSLOT%_run_as_administrator.bat"
) ELSE (
    ECHO Skipping Tomcat update...
)
ENDLOCAL	
PAUSE