@ECHO OFF
rem ---------------------------------------------------------------------------
rem Start Script for the NMS DataController
rem ---------------------------------------------------------------------------

rem Guess DATACONTROLLER_HOME if not defined
set "PRG_DIR=%~dp0"


if not "%DATACONTROLLER_HOME%" == "" goto gotHome

set "DATACONTROLLER_HOME=%PRG_DIR%"
if exist "%DATACONTROLLER_HOME%\bin\DataController.exe" goto okHome
cd /d %PRG_DIR%\..
set "DATACONTROLLER_HOME=%cd%"
cd "%PRG_DIR%"

:gotHome
if exist "%DATACONTROLLER_HOME%\bin\DataController.exe" goto okHome
echo The DATACONTROLLER_HOME environment variable is not defined correctly
echo This environment variable is needed to run this program
goto end

:okHome

echo DATACONTROLLER_HOME: %DATACONTROLLER_HOME%
set "wtitle=DataController"
set "_NAME=DataController.exe"

set procExist=0
wmic process where name="%_NAME%" get name |findstr "%_NAME%">nul &&set /a procExist+=1
if not "%procExist%" == "0" goto stopProc
echo %wtitle% is not start,  now start......
goto startProc

:stopProc
echo %wtitle% is start,  now restart......
wmic process where name="%_NAME%" delete >nul
ping -n 3 127.0.0.1>nul

:startProc
set XMS_VALUE=32
set XMX_VALUE=128
if exist "%DATACONTROLLER_HOME%\bin\jvm.ini" (
cd /d "%DATACONTROLLER_HOME%\bin"
for /f "tokens=1,2 delims==" %%a IN (jvm.ini) Do (
	rem echo "Xms"=="%%a"

	if "Xms"=="%%a" (
  		set XMS_VALUE=%%b
	) 
)

for /f "tokens=1,2 delims==" %%c IN (jvm.ini) Do (
  	rem echo "Xmx"=="%%c"
 	if "Xmx"=="%%c" (
		set XMX_VALUE=%%d
	)
)
cd "%PRG_DIR%"
)
rem echo %XMS_VALUE%
rem echo %XMX_VALUE%
set /a XMS_VALUE=%XMS_VALUE%*(1024*1024)
set /a XMX_VALUE=%XMX_VALUE%*(1024*1024)
rem echo %XMS_VALUE%
rem echo %XMX_VALUE%


start "" "%DATACONTROLLER_HOME%\bin\DataController.exe" -Jinitialheap=%XMS_VALUE% -Jmaxheap=%XMX_VALUE% 
set startOk=0
ping -n 3 127.0.0.1>nul
wmic process where name="%_NAME%" get name |findstr "%_NAME%">nul &&set /a startOk+=1
if not "%startOk%" == "1" goto handleTask
goto end

:end