REM Batch file for cleaning up the web components, sometimes needed when deploying is not working

SET ABS_WEBSERVERHOME=%1
SET ABS_WEBSERVERSERVICENAME=%2

REM Clean up the domain's Applications, Generated, and OSGI-cache folders
SET ABS_WEBAPPS=%ABS_WEBSERVERHOME%webapps
SET ABS_WORK=%ABS_WEBSERVERHOME%work

ECHO Stopping service %ABS_WEBSERVERSERVICENAME%
net stop "%ABS_WEBSERVERSERVICENAME%"
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
        )
    )
)

ECHO Wait 5 seconds to make sure all locked files are freed for clean up
PING 127.0.0.1 -n 5 > nul

ECHO Remove all files under %ABS_WEBAPPS%
:: rmdir %ABS_WEBAPPS% /s /q
:: mkdir %ABS_WEBAPPS%
:: ECHO Remove all files under %ABS_WORK%
:: rmdir %ABS_WORK% /s /q
:: mkdir %ABS_WORK%
call %ABS_WEBSERVERHOME%custom\bin\CleanTomcat.bat %ABS_WEBSERVERHOME%
REM Recreate WEBAPPS (management and admin)
ECHO Rebuild Tomcat WebApps
SET PATH=%PATH%;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem
ECHO Environment variables set to %PATH%
REM ======= COPY CUSTOM CONTEXT.XML =======
ECHO Copying custom context.xml into manager and host-manager apps for console access...

if exist "%ABS_WEBSERVERHOME%\custom\deploy\context.xml" (
    if exist "%ABS_WEBSERVERHOME%\webapps\host-manager\META-INF\" (
        copy /Y "%ABS_WEBSERVERHOME%\custom\deploy\context.xml" "%ABS_WEBSERVERHOME%\webapps\host-manager\META-INF\context.xml"
    ) else (
        ECHO WARNING: host-manager\META-INF folder not found.
    )

    if exist "%ABS_WEBSERVERHOME%\webapps\manager\META-INF\" (
        copy /Y "%ABS_WEBSERVERHOME%\custom\deploy\context.xml" "%ABS_WEBSERVERHOME%\webapps\manager\META-INF\context.xml"
    ) else (
        ECHO WARNING: manager\META-INF folder not found.
    )
) else (
    ECHO WARNING: Missing "%ABS_WEBSERVERHOME%\custom\deploy\context.xml"
)

ECHO Starting service %ABS_WEBSERVERSERVICENAME%
net start "%ABS_WEBSERVERSERVICENAME%"

ECHO Wait 30 seconds to make sure Tomcat has started correctly
PING 127.0.0.1 -n 10 > nul
Echo.20 Seconds remaining
PING 127.0.0.1 -n 10 > nul
Echo.10 Seconds remaining
PING 127.0.0.1 -n 10 > nul





