@echo off
setlocal
call "%~dp0..\env.bat"

set JAVA_SIDE=..\Java Side

echo Compiling Java source...
"%JAVA_HOME%\bin\javac" -cp "%JAVA_SIDE%\log4j.jar" "%JAVA_SIDE%\loggerDemo\JavaClass.java"
if errorlevel 1 (
  echo Error: javac failed. Exiting build.
  pause
  exit /b 1
)

echo Creating JAR...
"%JAVA_HOME%\bin\jar" cvf "%JAVA_SIDE%\loggerDemo.jar" -C "%JAVA_SIDE%" loggerDemo\JavaClass.class
if errorlevel 1 (
  echo Error: jar failed. Exiting build.
  pause
  exit /b 1
)

echo Generating class list from JARs...
jnbproxy.exe /ls /ns /pd n2j /cp "%JAVA_SIDE%\loggerDemo.jar;%JAVA_SIDE%\log4j.jar;%JAVA_SIDE%\log4j-core.jar" /f "classList.txt" /d . /n logging /jp "%JAVA_SIDE%\jnbcore.jar" /bp "%JAVA_SIDE%\bcel-6.10.0.jar" /pro s /jvm "%JVM_DLL_64%"
if errorlevel 1 (
  echo Error: class list generation failed. Exiting build.
  pause
  exit /b 1
)

echo Generating .NET proxy DLL...
jnbproxy.exe /ns /pd n2j /cp "%JAVA_SIDE%\loggerDemo.jar;%JAVA_SIDE%\log4j.jar;%JAVA_SIDE%\log4j-core.jar" /f "classList.txt" /d . /n logging /jp "%JAVA_SIDE%\jnbcore.jar" /bp "%JAVA_SIDE%\bcel-6.10.0.jar" /pro s /jvm "%JVM_DLL_64%"
if errorlevel 1 (
  echo Error: proxy generation failed. Exiting build.
  pause
  exit /b 1
)

echo Copying proxy DLL to .NET project...
copy /Y logging.dll ..\LoggerDemo\logging.dll
if errorlevel 1 (
  echo Error: copy failed. Exiting build.
  pause
  exit /b 1
)

echo Build complete.
endlocal
