@echo off
@REM
@REM Copyright (c) 2018-2022
@REM Beijing Baolande Software Corporation
@REM
@REM All Rights Reserved
@REM

setlocal

rem Guess BES_HOME if not defined
set "CURRENT_DIR=%cd%"
if not "%BES_HOME%" == "" goto validateBesHome
set "BES_HOME=%CURRENT_DIR%"
if exist "%BES_HOME%\bin\startserver.bat" goto rightBesHome
cd ..
set "BES_HOME=%cd%"
cd "%CURRENT_DIR%"

:validateBesHome
if not exist "%BES_HOME%\bin\setenv.bat" goto errorBesHome
if not exist "%BES_HOME%\bin\setpatchenv.bat" goto errorBesHome
goto rightBesHome

:errorBesHome
echo The BES_HOME environment variable is not defined correctly
echo This environment variable is needed to run this program
goto error

:rightBesHome

rem Copy BES_ROOT from BES_HOME if not defined
if not "%BES_ROOT%" == "" goto setBesRoot
set "BES_ROOT=%BES_HOME%"
:setBesRoot

rem Ensure that neither BES_HOME nor BES_ROOT contains a semi-colon
rem as this is used as the separator in the classpath and Java provides no
rem mechanism for escaping if the same character appears in the path. Check this
rem by replacing all occurrences of ';' with '' and checking that neither
rem BES_HOME nor BES_ROOT have changed
if "%BES_HOME%" == "%BES_HOME:;=%" goto besHomeNoSemicolon
echo Unable to start as BES_HOME contains a semicolon (;) character
goto error

:besHomeNoSemicolon

if "%BES_ROOT%" == "%BES_ROOT:;=%" goto besRootNoSemicolon
echo Unable to start as BES_ROOT contains a semicolon (;) character
goto error
:besRootNoSemicolon

set CLASSPATH=

call "%BES_HOME%\bin\setenv.bat"
call "%BES_HOME%\bin\setpatchenv.bat" bootstrap

set FORCE=0

if ""%1"" == ""--force"" goto setForce
goto overSetForce

:setForce
shift
set FORCE=1

:overSetForce

set PID_FILE=%BES_ROOT%\conf\.pid
if exist "%PID_FILE%" goto getPid
goto emptyPid

:getPid
for /f "usebackq delims==" %%p in ("%PID_FILE%") do (
set PID=%%p
)

if ""%PID%"" == """" goto emptyPid
goto validateServerStart

:emptyPid
set JPS="%JAVA_BASE%\bin\jps.exe"
if not exist %JPS% (
    echo "PID file does not exist, the server maybe not started."
    goto error
)

for /f %%a in ('%JPS% -v ^| findstr BESMain ^| findstr /L /C:%BES_ROOT%') do set PID=%%a
if not defined PID (
    echo "The server is not started."
    goto error
)

if not %PID% GTR 0 (
    echo "The server pid from jps was not greater than zero."
    goto error
)

:validateServerStart
tasklist /fo list | findstr %PID%
if "%errorlevel%" == "0" goto stopServer
echo "PID file found but either no matching process was found or the current user does not have permission to stop the process. Stop aborted."
goto error

:stopServer

if not exist "%JAVA_BASE%\lib\tools.jar" (
   set FORCE=1 
   goto killServerByPid
)

set CLASSPATH=%CLASSPATH%;%JAVA_BASE%\lib\tools.jar
set CLASSPATH=%CLASSPATH%;%BES_HOME%\lib\bootstrap\bes-common-util.jar
set CLASSPATH=%CLASSPATH%;%BES_HOME%\lib\bootstrap\bes-common-core.jar
set CLASSPATH=%CLASSPATH%;%BES_HOME%\lib\bootstrap\bes-config-provider.jar
set CLASSPATH=%CLASSPATH%;%BES_HOME%\lib\bes-server-assembler.jar
set CLASSPATH=%CLASSPATH%;%BES_HOME%\lib\bootstrap\bes-patch.jar

if "%PATCH_CLASSPATH%" == "" goto overSetPatchClassPath
set CLASSPATH=%PATCH_CLASSPATH%;%CLASSPATH%

:overSetPatchClassPath

%JAVA% %JAVA_OPTS% -Dcom.bes.installRoot="%BES_HOME%" -Dcom.bes.instanceRoot="%BES_ROOT%" -cp "%CLASSPATH%" com.bes.enterprise.server.StopServerMain %PID%

@REM stop failed. Shutdown port disabled? Try a normal taskkill.
if "%errorlevel%" == "0" goto end

:killServerByPid
if ""%PID%"" == """" goto error
if "%FORCE%" == "0" goto error

TASKKILL /F /FI "PID eq %PID%"
timeout /t 1 /nobreak > nul
tasklist /fo list | findstr %PID%
if "%errorlevel%" == "1" (
    del /q %PID_FILE%
    goto end
)
echo "BES has not been killed completely yet. The process might be waiting on some system call or might be UNINTERRUPTIBLE."
goto error

endlocal

:error
echo "Stop server failed."
exit /b 1

:end
echo "Stop server successfully."
exit /b 0
