@echo off
:: ==========================================================================
::  Ferminux Network - START NODE (no mining)
::
::  Runs a full Ferminux node. It verifies and relays every block, which is
::  what keeps the network decentralised, but it does NOT mine and earns no
::  FMX. It is light: no 1.1 GB mining DAG is ever built.
::
::  Same batch conventions as START-MINING.bat - read the notes there before
::  editing this one.
:: ==========================================================================
setlocal EnableExtensions DisableDelayedExpansion
title Ferminux Network - Node

set "HERE=%~dp0"
set "EXE=%HERE%ferminux-geth.exe"
set "PORT=30303"
set "IPCNAME=ferminux.ipc"

cls
echo.
echo   ====================================================================
echo    FERMINUX NETWORK   -   FULL NODE  (no mining)
echo   ====================================================================
echo.

if exist "%EXE%" goto :have_exe
echo   PROBLEM: ferminux-geth.exe is not in this folder:
echo.
echo       %HERE%
echo.
echo   This usually means the ZIP was never extracted. Windows lets you
echo   look inside a ZIP as if it were a folder, but programs cannot run
echo   from in there.
echo.
echo   FIX: close this window. Right-click the ZIP you downloaded, choose
echo        "Extract All...", open the folder it creates, and double-click
echo        START-NODE.bat in THAT folder.
goto :fail
:have_exe

if defined LOCALAPPDATA goto :have_appdata
echo   PROBLEM: Windows did not set the LOCALAPPDATA variable, so this
echo   script cannot work out where to store the chain data.
echo.
echo   FIX: press the Windows key, type cmd, press Enter, then drag
echo        START-NODE.bat into that window and press Enter.
goto :fail
:have_appdata

set "DATADIR=%LOCALAPPDATA%\Ferminux"
if not exist "%DATADIR%" mkdir "%DATADIR%" >nul 2>&1

echo   WHAT THIS DOES
echo   --------------------------------------------------------------------
echo   Your PC downloads every Ferminux block, checks it is valid, and
echo   passes it on to other people running the same software. That is
echo   what a "node" is. More independent nodes means the network is
echo   harder to censor or shut down.
echo.
echo   Honest expectations:
echo.
echo     * It earns you nothing. No FMX, no fees, no points. If you want
echo       to earn, close this and run START-MINING.bat instead.
echo     * It is light. No mining DAG is built, so no 1.1 GB file and no
echo       pegged CPU - a node only needs the small verification cache.
echo     * It uses a little bandwidth continuously and grows the chain
echo       folder slowly over time.
echo.
echo   SETTINGS
echo   --------------------------------------------------------------------
echo    Chain data : %DATADIR%
echo    P2P port   : %PORT% (TCP and UDP)
echo    Local RPC  : http://127.0.0.1:8545  (this PC only, not the internet)
echo    IPC pipe   : \\.\pipe\%IPCNAME%
echo.
echo   The genesis block and the bootnode list are compiled into
echo   ferminux-geth.exe, so there is nothing to configure. An empty folder
echo   joins ChainID 3961 on its own.
echo.
echo   The first time you run this, Windows Firewall will ask whether to
echo   allow ferminux-geth. Tick "Private networks" and click Allow, or
echo   your node will struggle to find peers.
echo.
echo   Leave this window open. Closing it stops the node.
echo   To stop: press Ctrl+C and answer Y, or just close the window.
echo.
set "GO="
set /p "GO=  Press Enter to start the node (or type N to cancel): "
if /i "%GO%"=="n" (
  echo.
  echo   Cancelled. Nothing was started.
  goto :done
)

echo.
echo   Starting ferminux-geth. Command:
echo.
echo     ferminux-geth.exe --datadir "%DATADIR%" --syncmode full --cache 512 --port %PORT% --ipcpath %IPCNAME% --http --http.addr 127.0.0.1 --http.port 8545 --http.api eth,net,web3,txpool
echo.
echo   --------------------------------------------------------------------
echo.

"%EXE%" --datadir "%DATADIR%" --syncmode full --cache 512 --port %PORT% --ipcpath %IPCNAME% --http --http.addr 127.0.0.1 --http.port 8545 --http.api eth,net,web3,txpool
set "RC=%ERRORLEVEL%"

echo.
echo   --------------------------------------------------------------------
if "%RC%"=="0" (
  echo   ferminux-geth stopped normally.
  goto :done
)

echo   ferminux-geth stopped with error code %RC%.
echo.
echo   Most likely causes, in order:
echo.
echo    * "Failed to start IPC" or "datadir already used" - Ferminux is
echo      already running in another window (START-MINING.bat counts).
echo      Only one copy can run at a time.
echo    * "bind: ... permitted" on port %PORT% or 8545 - another program
echo      holds that port, or Windows Firewall blocked it.
echo    * Disk full - free space on the drive holding %DATADIR%.
echo.
echo   Scroll up. The last few lines above normally say exactly what
echo   happened; copy them if you need to ask for help.
goto :fail

:fail
echo.
echo   ====================================================================
echo    Press any key to close this window.
echo   ====================================================================
pause >nul
exit /b 1

:done
echo.
echo   ====================================================================
echo    Press any key to close this window.
echo   ====================================================================
pause >nul
exit /b 0
