@echo off
setlocal EnableExtensions
cd /d "%~dp0"

echo ============================================
echo  Local node mining CPU + Stratum 16002
echo ============================================
echo.

set "NODE="
if exist "%~dp0eid-node.exe" set "NODE=%~dp0eid-node.exe"
if exist "%~dp0..\miner-build\mars\eid-node.exe" set "NODE=%~dp0..\miner-build\mars\eid-node.exe"
if exist "%~dp0..\build\mars\eid-node.exe" set "NODE=%~dp0..\build\mars\eid-node.exe"
if exist "%~dp0..\build\mars\Release\eid-node.exe" set "NODE=%~dp0..\build\mars\Release\eid-node.exe"

if not defined NODE (
  echo ERROR: eid-node.exe not found.
  pause
  exit /b 1
)

findstr /B /C:"miner_key=" eid-node.cfg | findstr /V /C:"# miner_key=" >nul
if errorlevel 1 (
  echo eid-node.cfg has no miner_key. Run export-miner-key.bat first.
  pause
  exit /b 1
)

findstr /B /C:"owner_key=" eid-node.cfg | findstr /V /C:"# owner_key=" >nul
if errorlevel 1 (
  echo eid-node.cfg has no owner_key. GPU will login but get no jobs.
  echo Run export-miner-key.bat and fill owner_key= too.
  pause
  exit /b 1
)

if not exist "%~dp0stratum.crt" (
  if exist "%~dp0miner.crt" copy /Y "%~dp0miner.crt" "%~dp0stratum.crt" >nul
)
if not exist "%~dp0stratum.key" (
  if exist "%~dp0miner.key" copy /Y "%~dp0miner.key" "%~dp0stratum.key" >nul
)
if not exist "%~dp0stratum.crt" (
  echo Missing stratum.crt / stratum.key
  echo copy miner.crt stratum.crt
  echo copy miner.key stratum.key
  pause
  exit /b 1
)

echo Starting node...
echo.
"%NODE%"
pause
