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 ABS_WEBSERVERHOME=%1
SET ABS_WEBADMINSLOT=%2
SET ABS_WEBSERVERSLOT=%3
SET ABS_WEBSERVERSERVICENAME=%4
SET ABS_WEBSERVERMEMORY=%5
set ABS_UDP=%6
set ABS_HTTPS=%7

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_WEBADMINSLOT] %ABS_WEBADMINSLOT% %ABS_WEBSERVERHOME%conf\server.xml
call %ABS_WEBSERVERHOME%custom\bin\replace_values_in_file.bat %ABS_WEBSERVERHOME% [ABS_HTTPS] %ABS_HTTPS% %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\deploy_all_webcomponents_for_slot_xx_run_as_administrator.bat.org %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% [ABS_WEBADMINSLOT] %ABS_WEBADMINSLOT% %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_UDP] %ABS_UDP% %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
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
call %ABS_WEBSERVERHOME%custom\bin\replace_values_in_file.bat %ABS_WEBSERVERHOME% [WEBADMINSLOT] %ABS_WEBADMINSLOT% %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% [HTTPS] %ABS_HTTPS% %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

REM Prepare web component short cuts for quick testing
call %ABS_WEBSERVERHOME%custom\bin\create_shortcut_https.bat %ABS_WEBSERVERHOME%deploy\%ABS_WEBADMINSLOT%_ADMIN_console.url %ABS_WEBADMINSLOT%
call %ABS_WEBSERVERHOME%custom\bin\create_shortcut.bat %ABS_WEBSERVERHOME%deploy\%ABS_WEBSERVERSLOT%_GenericWebservice.url %ABS_WEBSERVERSLOT% absgenericwebservice/ABSGenericWebService
call %ABS_WEBSERVERHOME%custom\bin\create_shortcut.bat %ABS_WEBSERVERHOME%deploy\%ABS_WEBSERVERSLOT%_ws4MobileApps.url %ABS_WEBSERVERSLOT% absws4mobileapps/absws4mobile
call %ABS_WEBSERVERHOME%custom\bin\create_shortcut.bat %ABS_WEBSERVERHOME%deploy\%ABS_WEBSERVERSLOT%_UHFmonitor.url %ABS_WEBSERVERSLOT% absuhfmonitor/
call %ABS_WEBSERVERHOME%custom\bin\create_shortcut.bat %ABS_WEBSERVERHOME%deploy\%ABS_WEBSERVERSLOT%_WebPortal.url %ABS_WEBSERVERSLOT% abswebportal/
call %ABS_WEBSERVERHOME%custom\bin\create_shortcut.bat %ABS_WEBSERVERHOME%deploy\%ABS_WEBSERVERSLOT%_WebClientx.url %ABS_WEBSERVERSLOT% webclientx/
call %ABS_WEBSERVERHOME%custom\bin\create_shortcut.bat %ABS_WEBSERVERHOME%deploy\%ABS_WEBSERVERSLOT%_MobileRestServices.url %ABS_WEBSERVERSLOT% absmobilerestservices/
call %ABS_WEBSERVERHOME%custom\bin\create_shortcut.bat %ABS_WEBSERVERHOME%deploy\%ABS_WEBSERVERSLOT%_RestServices.url %ABS_WEBSERVERSLOT% absrestservices/
call %ABS_WEBSERVERHOME%custom\bin\create_shortcut.bat %ABS_WEBSERVERHOME%deploy\%ABS_WEBSERVERSLOT%_WebClientReplaceSmartbarcode.url %ABS_WEBSERVERSLOT% webclientreplacesmartbarcode/
call %ABS_WEBSERVERHOME%custom\bin\create_shortcut.bat %ABS_WEBSERVERHOME%deploy\%ABS_WEBSERVERSLOT%_WebClientResidentialDelivery.url %ABS_WEBSERVERSLOT% webclientresidentialdelivery/
call %ABS_WEBSERVERHOME%custom\bin\create_shortcut.bat %ABS_WEBSERVERHOME%deploy\%ABS_WEBSERVERSLOT%_WebClientRouteReturns.url %ABS_WEBSERVERSLOT% webclientroutereturns/
call %ABS_WEBSERVERHOME%custom\bin\create_shortcut.bat %ABS_WEBSERVERHOME%deploy\%ABS_WEBSERVERSLOT%_WebClientCleanroompackinginstructions.url %ABS_WEBSERVERSLOT% webclientcleanroompackinginstructions/
call %ABS_WEBSERVERHOME%custom\bin\create_shortcut.bat %ABS_WEBSERVERHOME%deploy\%ABS_WEBSERVERSLOT%_ABSMobileFirebaseServices.url %ABS_WEBSERVERSLOT% 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