@echo off
:: ==========================================================================
::  Ferminux Network - GPU MINING helper
::
::  We do not ship a GPU miner: lolMiner, GMiner, SRBMiner-MULTI and
::  TeamRedMiner are other people's programs with their own licences, and
::  a bundled copy would be an antivirus magnet. This script asks for your
::  address, then either launches a GPU miner it finds next to itself or
::  prints the exact command line for you to copy.
::
::  Flags are taken verbatim from /proxy/README.md in the Ferminux repo -
::  the pool speaks the ETHPROXY dialect, not real stratum, which is why
::  lolMiner needs --ethstratum ETHPROXY and GMiner needs --proto proxy.
::
::  Same batch conventions as START-MINING.bat.
:: ==========================================================================
setlocal EnableExtensions DisableDelayedExpansion
title Ferminux Network - GPU Mining

set "HERE=%~dp0"
set "POOLHOST=pool.ferminux.net"
set "POOLPORT=3333"
set "WORKER=win1"

cls
echo.
echo   ====================================================================
echo    FERMINUX NETWORK   -   GPU MINING
echo   ====================================================================
echo.
echo   A graphics card mines Ethash roughly a hundred times faster than a
echo   processor. Ferminux is standard Ethash at epoch 0, so every ordinary
echo   Ethash miner works unmodified.
echo.
echo   You need two things:
echo.
echo     1. A GPU miner program. None is included here - download one
echo        yourself from its official site:
echo           lolMiner        https://github.com/Lolliedieb/lolMiner-releases
echo           GMiner          https://github.com/develsoftware/GMinerRelease
echo           SRBMiner-MULTI  https://github.com/doktor83/SRBMiner-Multi
echo           TeamRedMiner    https://github.com/todxx/teamredminer
echo.
echo     2. Your 0x payout address (https://wallet.ferminux.net).
echo.
echo   You do NOT need ferminux-geth running for pool mining. The pool at
echo   %POOLHOST%:%POOLPORT% runs the node for you.
echo.
echo   Read GPU-MINING.txt for the full explanation and for solo mining
echo   against your own node.
echo.

:: ---------------------------------------------------------------------
:: Address: reuse the one START-MINING.bat saved, if it is still valid.
:: ---------------------------------------------------------------------
set "CFG=%HERE%ferminux-config.txt"
if exist "%CFG%" goto :read_cfg
if not defined LOCALAPPDATA goto :no_cfg
set "CFG=%LOCALAPPDATA%\Ferminux\ferminux-config.txt"
if exist "%CFG%" goto :read_cfg
goto :no_cfg

:read_cfg
for /f "usebackq eol=# tokens=1,* delims==" %%A in ("%CFG%") do (
  if /i "%%A"=="address" set "ADDR=%%B"
)

:no_cfg
if not defined ADDR goto :ask_addr
set "V_IN=%ADDR%"
call :validate_addr
if "%V_OK%"=="1" goto :have_addr
set "ADDR="

:ask_addr
echo   --------------------------------------------------------------------
set "ADDR_IN="
set /p "ADDR_IN=  Paste your 0x payout address and press Enter: "
set "V_IN=%ADDR_IN%"
call :trim
call :validate_addr
if "%V_OK%"=="1" goto :addr_ok
echo.
echo   That does not look like a Ferminux address. It must start with 0x
echo   and have exactly 40 more characters, each a digit 0-9 or a letter
echo   a-f. For example:
echo       0x1a2b3c4d5e6f708192a3b4c5d6e7f8091a2b3c4d
echo.
echo   If you copied it with quote marks around it, leave those out -
echo   paste only the address itself.
echo.
goto :ask_addr

:addr_ok
set "ADDR=%V_IN%"

:have_addr
set "USERSTR=%ADDR%.%WORKER%"
echo.
echo   Payout address: %ADDR%
echo   Worker name   : %WORKER%   (shows up in the pool log)
echo.

:: ---------------------------------------------------------------------
:: Look for a GPU miner sitting in (or under) this folder.
:: ---------------------------------------------------------------------
set "LOL="
set "GMI="
set "SRB="
set "TRM="
for /f "delims=" %%F in ('dir /b /s "%HERE%lolMiner.exe" 2^>nul') do set "LOL=%%F"
for /f "delims=" %%F in ('dir /b /s "%HERE%miner.exe" 2^>nul') do set "GMI=%%F"
for /f "delims=" %%F in ('dir /b /s "%HERE%SRBMiner-MULTI.exe" 2^>nul') do set "SRB=%%F"
for /f "delims=" %%F in ('dir /b /s "%HERE%teamredminer.exe" 2^>nul') do set "TRM=%%F"

if defined LOL goto :run_lol
if defined GMI goto :run_gmi
if defined SRB goto :run_srb
if defined TRM goto :run_trm

:: ---------------------------------------------------------------------
:: Nothing found - print the exact commands.
:: ---------------------------------------------------------------------
echo   --------------------------------------------------------------------
echo   No GPU miner was found in this folder, so nothing will be launched.
echo.
echo   Download one, unzip it, copy its files into this folder, and run
echo   GPU-MINING.bat again - it will then start automatically.
echo.
echo   Or run it yourself. Copy ONE of these lines into a Command Prompt
echo   opened in the miner's own folder:
echo.
echo   lolMiner:
echo     lolMiner.exe --algo ETHASH --ethstratum ETHPROXY --pool stratum+tcp://%POOLHOST%:%POOLPORT% --user %USERSTR%
echo.
echo   GMiner:
echo     miner.exe --algo ethash --proto proxy --server %POOLHOST% --port %POOLPORT% --user %USERSTR%
echo.
echo   SRBMiner-MULTI:
echo     SRBMiner-MULTI.exe --algorithm ethash --esm 0 --pool %POOLHOST%:%POOLPORT% --wallet %ADDR%
echo.
echo   TeamRedMiner:
echo     teamredminer.exe -a ethash -o stratum+tcp://%POOLHOST%:%POOLPORT% -u %ADDR% -p x
echo.
goto :done

:run_lol
echo   Found lolMiner:
echo     %LOL%
echo.
echo   Launching:
echo     lolMiner.exe --algo ETHASH --ethstratum ETHPROXY --pool stratum+tcp://%POOLHOST%:%POOLPORT% --user %USERSTR%
echo.
pause
"%LOL%" --algo ETHASH --ethstratum ETHPROXY --pool stratum+tcp://%POOLHOST%:%POOLPORT% --user %USERSTR%
goto :after_miner

:run_gmi
echo   Found GMiner:
echo     %GMI%
echo.
echo   Launching:
echo     miner.exe --algo ethash --proto proxy --server %POOLHOST% --port %POOLPORT% --user %USERSTR%
echo.
pause
"%GMI%" --algo ethash --proto proxy --server %POOLHOST% --port %POOLPORT% --user %USERSTR%
goto :after_miner

:run_srb
echo   Found SRBMiner-MULTI:
echo     %SRB%
echo.
echo   Launching:
echo     SRBMiner-MULTI.exe --algorithm ethash --esm 0 --pool %POOLHOST%:%POOLPORT% --wallet %ADDR%
echo.
pause
"%SRB%" --algorithm ethash --esm 0 --pool %POOLHOST%:%POOLPORT% --wallet %ADDR%
goto :after_miner

:run_trm
echo   Found TeamRedMiner:
echo     %TRM%
echo.
echo   Launching:
echo     teamredminer.exe -a ethash -o stratum+tcp://%POOLHOST%:%POOLPORT% -u %ADDR% -p x
echo.
pause
"%TRM%" -a ethash -o stratum+tcp://%POOLHOST%:%POOLPORT% -u %ADDR% -p x
goto :after_miner

:after_miner
set "RC=%ERRORLEVEL%"
echo.
echo   --------------------------------------------------------------------
if "%RC%"=="0" (
  echo   The miner exited normally.
  goto :done
)
echo   The miner exited with error code %RC%.
echo.
echo   Things worth checking:
echo.
echo    * "connection refused" / "cannot connect" - check your internet,
echo      then confirm the pool is up: ferminux.net shows its status.
echo    * "invalid share" or endless rejects - the protocol flags matter.
echo      lolMiner needs --ethstratum ETHPROXY, GMiner needs --proto proxy,
echo      SRBMiner needs --esm 0. Without them the miner speaks a dialect
echo      the pool does not answer.
echo    * "out of memory" on the GPU - the Ethash DAG needs about 1.1 GB
echo      of video memory today and grows ~8 MB every 2.5 days, so a 2 GB
echo      card runs out within about a year and a 4 GB card in about 2.5.
echo    * Driver errors - install the current AMD or NVIDIA driver.
goto :fail

:: =====================================================================
::  Subroutines
:: =====================================================================

:trim
if not defined V_IN goto :eof
for /f "tokens=*" %%a in ("%V_IN%") do set "V_IN=%%a"
:trim_tail
if not defined V_IN goto :eof
if "%V_IN:~-1%"==" " (
  set "V_IN=%V_IN:~0,-1%"
  goto :trim_tail
)
goto :eof

:validate_addr
set "V_OK=0"
if not defined V_IN goto :eof
if /i not "%V_IN:~0,2%"=="0x" goto :eof
if "%V_IN:~41,1%"=="" goto :eof
if not "%V_IN:~42,1%"=="" goto :eof
set "V_REST=%V_IN:~2%"
set "V_REST=%V_REST:0=%"
set "V_REST=%V_REST:1=%"
set "V_REST=%V_REST:2=%"
set "V_REST=%V_REST:3=%"
set "V_REST=%V_REST:4=%"
set "V_REST=%V_REST:5=%"
set "V_REST=%V_REST:6=%"
set "V_REST=%V_REST:7=%"
set "V_REST=%V_REST:8=%"
set "V_REST=%V_REST:9=%"
set "V_REST=%V_REST:a=%"
set "V_REST=%V_REST:b=%"
set "V_REST=%V_REST:c=%"
set "V_REST=%V_REST:d=%"
set "V_REST=%V_REST:e=%"
set "V_REST=%V_REST:f=%"
if defined V_REST goto :eof
set "V_OK=1"
goto :eof

: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
