rem Checks if the JAVA_HOME has a space in it (can cause issues)
if DEFINED JAVA_HOME goto RUN_CHECKS
set JAVA_HOME=
:RUN_CHECKS
set _marker="x%JAVA_HOME%"
set _marker=%_marker: =%
if %_marker% == "x%JAVA_HOME%" goto RUN_JAVA
echo.
echo -------------------------------------------------------------------------------
echo   JAVA_HOME "%JAVA_HOME%" contains spaces.
echo   Please change to a location without spaces if this causes problems.
echo -------------------------------------------------------------------------------

:RUN_JAVA
rem Check that JAVA_HOME is valid
if exist "%JAVA_HOME%\bin\java.exe" goto CHECK_JAVA_VERSION
echo.
echo -------------------------------------------------------------------------------
echo   JAVA_HOME "%JAVA_HOME%" does not point to a valid Java home directory.
echo -------------------------------------------------------------------------------
goto DONE


:CHECK_JAVA_VERSION
set JAVA_BINARY="%JAVA_HOME:"=%\bin\java.exe"
for /f "tokens=3" %%g in ('%%JAVA_BINARY%% -version 2^>^&1 ^| findstr /i "version"') do set JAVA_VERSION=%%g
if %JAVA_VERSION% GEQ "1.8" goto DONE
echo.
echo -------------------------------------------------------------------------------
echo   Atlassian Bitbucket does not support Java %JAVA_VERSION:"=%.
echo   Please start the product with Java 8.
echo -------------------------------------------------------------------------------
pause
exit /b 1

:DONE