@echo off
call "%~dp0env.bat"
REM Kill any leftover Java side from a previous run
wmic process where "name='java.exe' and commandline like '%%JNBMain%%'" call terminate >nul 2>&1

echo ========================================
echo LoggerDemo - TCP Mode (.NET calling Java)
echo ========================================
echo.

echo Step 1: Building Java and generating proxies...
cd .\ProxyGen
call Build.bat
if errorlevel 1 (
  echo Java build failed!
  pause
  exit /b 1
)

echo.
echo Step 2: Building C# project...
cd ..\LoggerDemo
dotnet build -c Release
if errorlevel 1 (
  echo C# build failed!
  pause
  exit /b 1
)

echo.
echo Step 3: Swapping in TCP config...
copy /Y App.tcp.config bin\Release\net48\LoggerDemo.exe.config
if errorlevel 1 (
  echo Config copy failed!
  pause
  exit /b 1
)

echo.
echo Step 4: Starting Java side (TCP on port 8085)...
cd "..\Java Side"
start "JNBridge Java Side" "%JAVA_HOME%\bin\java" -cp ".;loggerDemo.jar;log4j.jar;log4j-core.jar;jnbcore.jar" com.jnbridge.jnbcore.JNBMain /props "jnbcore_tcp_no_security.properties"
echo Waiting for Java side to initialize...
timeout /t 3 /nobreak >nul

echo.
echo ========================================
echo Build complete! Running demo...
echo ========================================
echo.

cd ..\LoggerDemo\bin\Release\net48
LoggerDemo.exe

echo.
echo Shutting down Java side...
wmic process where "name='java.exe' and commandline like '%%JNBMain%%'" call terminate >nul 2>&1

pause
