@echo off

:: Set Path Options
:: to make sure that chcp and findstr are found
set PATH=%PATH%;c:\windows\system32
@chcp 1252

setlocal enabledelayedexpansion

set EXPECTED_JAVA_VERSION1=1.7
set EXPECTED_JAVA_VERSION2=1.8
set JAVA_COMMAND=java

:: Set Java Options 
:: Which version of WinPlausi should be used? ldw oder fsn:
set JAVA_OPTS=%JAVA_OPTS% -Dspring.profiles.active=fsn,gui 

:: Reducing Spring Log Output on startup:
set JAVA_OPTS=%JAVA_OPTS% -Dorg.apache.commons.logging.Log=org.apache.commons.logging.impl.NoOpLog  

::Performance Settings:
set JAVA_OPTS=%JAVA_OPTS% -Dsun.java2d.d3d=false 
set JAVA_OPTS=%JAVA_OPTS% -Dswing.aatext=true 
set JAVA_OPTS=%JAVA_OPTS% -XX:+AggressiveOpts 
set JAVA_OPTS=%JAVA_OPTS% -XX:+UseFastAccessorMethods 
set JAVA_OPTS=%JAVA_OPTS% -Xms128m 
set JAVA_OPTS=%JAVA_OPTS% -Xmx768m

set JAVA_OPTS=%JAVA_OPTS% -XX:+UseParNewGC 
set JAVA_OPTS=%JAVA_OPTS% -XX:+UseConcMarkSweepGC
set JAVA_OPTS=%JAVA_OPTS% -XX:ParallelGCThreads=20
set JAVA_OPTS=%JAVA_OPTS% -XX:SurvivorRatio=8
set JAVA_OPTS=%JAVA_OPTS% -XX:TargetSurvivorRatio=90
set JAVA_OPTS=%JAVA_OPTS% -XX:MaxTenuringThreshold=15
set JAVA_OPTS=%JAVA_OPTS% -XX:+ScavengeBeforeFullGC
set JAVA_OPTS=%JAVA_OPTS% -XX:+UseNUMA
set JAVA_OPTS=%JAVA_OPTS% -XX:CompileThreshold=1500
set JAVA_OPTS=%JAVA_OPTS% -XX:NewRatio=3

:: End Set Java Options

:: If setenv.bat exists, load it and start execution with provided JAVA_HOME
if exist setenv.bat (
	call setenv.bat	
	goto check_java_version1
)
 
:: If JAVA_HOME is undefined, check whether java can be executed. If not, let User specify it and store input into setenv.bat to be called on next execution
if not defined JAVA_HOME (
    for %%x in (java.exe) do if not [%%~$PATH:x]==[] goto check_java_exec_version
    :undefined    
    set /p JAVA_HOME=Bitte geben Sie den Pfad zu Ihrer Java-Installation an:
    goto define
    if not defined JAVA_HOME goto:undefined
) else (
	goto check_java_version1
)


:check_java_exec_version
java.exe -version 2>NUL && goto check_java_version1

:define
echo.|set /P ="set JAVA_HOME="%JAVA_HOME%"" > setenv.bat
goto check_java_version1

:check_java_version1
set JAVA_COMMAND=""%JAVA_HOME%\bin\java""
%JAVA_COMMAND% -version 2> java_version
for /f "tokens=3" %%g in (java_version) do (
	set FOUND_VERSION=%%g
	echo.%%g | findstr /C:"%EXPECTED_JAVA_VERSION1%" 1>nul	
  if errorlevel 1 ( 
  
  goto check_java_version2
  ) 
  goto task
)

:check_java_version2
set JAVA_COMMAND=""%JAVA_HOME%\bin\java""
%JAVA_COMMAND% -version 2> java_version
for /f "tokens=3" %%g in (java_version) do (
	set FOUND_VERSION=%%g
	echo.%%g | findstr /C:"%EXPECTED_JAVA_VERSION2%" 1>nul	
  if errorlevel 1 ( 
  
  goto wrong_java_version
  ) 
  goto task
)

:wrong_java_version
echo Mindestvorraussetzung fuer diese Applikation ist Java %EXPECTED_JAVA_VERSION1% oder %EXPECTED_JAVA_VERSION2%. Bitte passen Sie Ihre JAVA_HOME Variable an und versuchen Sie es erneut: 
goto undefined


:task
:: der Name des jars wird beim Bau durch den aktuellen Namen des Gui Jars ersetzt
%JAVA_COMMAND% %JAVA_OPTS% -jar %~dp0winplausi-gui.jar %*
if !ERRORLEVEL! gtr 0 (
set JAVA_RETURN=1
)
goto done

:done
:: Cleanup
if exist java_version del java_version
set JAVA_OPTS=

if defined JAVA_RETURN (
if JAVA_RETURN gtr 0 (
pause)
)