@echo off

set SRC=random_tree
set SRCEXE=random_tree_test

REM Get the Java version

set KEY="HKLM\SOFTWARE\JavaSoft\Java Development Kit"
set VALUE=CurrentVersion
reg query %KEY% /v %VALUE% >nul 2>&1 || (
  echo ERROR: JDK not installed 
  exit /b 1
)

set JRE_VERSION=
for /f "tokens=2,*" %%a in ('reg query %KEY% /v %VALUE% ^| findstr %VALUE%') do (
  set JRE_VERSION=%%b
)

REM Get the JavaHome

set KEY="HKLM\SOFTWARE\JavaSoft\Java Development Kit\%JRE_VERSION%"
set VALUE=JavaHome
reg query %KEY% /v %VALUE% >nul 2>&1 || (
  echo ERROR: JavaHome not found
  exit /b 1
)

set JAVAHOME=
for /f "tokens=2,*" %%a in ('reg query %KEY% /v %VALUE% ^| findstr %VALUE%') do (
  set JAVAHOME=%%b
)

echo * Building the model class ...
"%JAVAHOME%\bin\javac" -d Java %SRC%.java

echo * Building the test class ...
"%JAVAHOME%\bin\javac" -d Java %SRCEXE%.java

echo * Building the jar archive ...
cd Java
"%JAVAHOME%\bin\jar" cvfe %SRCEXE%.jar %SRCEXE% *.class > NUL
cd..

echo * Done

REM Free the resources

set JAVAHOME=
set JRE_VERSION=
set KEY=
set SRC=
set SRCEXE=
set VALUE=
