@echo off
setlocal enabledelayedexpansion
rem ============================================================
rem  BigCoin easy solo miner
rem  Guide: http://133.242.139.204/mining
rem
rem  Put this file in the folder where you extracted the
rem  BigCoin-win64 release zip (next to bitcoind.exe) and
rem  double-click it. It will:
rem    1. start a BigCoin node (chain data goes to the "data"
rem       subfolder of this directory)
rem    2. wait until the blockchain is synced
rem    3. create a wallet and a mining address (reused on restarts)
rem    4. mine blocks to that address until you close this window
rem
rem  More speed : run this script several times (one window per
rem               CPU core is a sensible maximum).
rem  Stop mining: close the window(s).
rem  Stop node  : bigcoin-cli.exe -datadir=data stop
rem ============================================================

cd /d "%~dp0"

if not exist "bitcoind.exe" (
  echo [ERROR] bitcoind.exe was not found in this folder.
  echo.
  echo Put mine-bigcoin.cmd into the folder where you extracted the
  echo BigCoin-win64 release zip, then run it again.
  pause
  exit /b 1
)

set "DD=-datadir=%~dp0data"

if not exist "%~dp0data" mkdir "%~dp0data"
if not exist "%~dp0data\bitcoin.conf" (
  >"%~dp0data\bitcoin.conf"  echo server=1
  >>"%~dp0data\bitcoin.conf" echo addnode=133.242.139.204:8338
)

echo Starting the BigCoin node ...
start "BigCoin node" /min bitcoind.exe "%DD%"

echo Waiting for the node to accept commands ...
set /a TRIES=0
:waitrpc
bigcoin-cli.exe "%DD%" getblockcount >nul 2>&1
if not errorlevel 1 goto rpcup
set /a TRIES+=1
if !TRIES! equ 24 (
  echo.
  echo Still waiting. If BigCoin Core or bigcoin-qt is already running
  echo on this PC, close it first and run this script again.
  echo.
)
timeout /t 5 /nobreak >nul
goto waitrpc

:rpcup
echo Node is up. Syncing the blockchain ...
:waitsync
bigcoin-cli.exe "%DD%" getblockchaininfo 2>nul | findstr /r /c:"initialblockdownload.*false" >nul
if not errorlevel 1 goto synced
for /f "usebackq delims=" %%H in (`bigcoin-cli.exe "%DD%" getblockcount 2^>nul`) do set "HEIGHT=%%H"
echo   synced up to block !HEIGHT! ...
timeout /t 5 /nobreak >nul
goto waitsync

:synced
bigcoin-cli.exe "%DD%" createwallet miner >nul 2>&1
bigcoin-cli.exe "%DD%" loadwallet miner >nul 2>&1
bigcoin-cli.exe "%DD%" -rpcwallet=miner getwalletinfo >nul 2>&1
if errorlevel 1 (
  echo [ERROR] could not create or load the mining wallet.
  pause
  exit /b 1
)

set "ADDRFILE=%~dp0data\mining-address.txt"
if exist "%ADDRFILE%" (
  set /p ADDR=<"%ADDRFILE%"
) else (
  for /f "usebackq delims=" %%A in (`bigcoin-cli.exe "%DD%" -rpcwallet=miner getnewaddress`) do set "ADDR=%%A"
  >"%ADDRFILE%" echo !ADDR!
)
if "!ADDR!"=="" (
  echo [ERROR] could not create a mining address.
  pause
  exit /b 1
)

echo.
echo ============================================================
echo  Mining to address: !ADDR!
echo.
echo  Every block found pays 50 BIG. Rewards become spendable
echo  after 100 confirmations - roughly one day.
echo  Your wallet lives in the "data" subfolder - back it up.
echo.
echo  Leave this window open. Close it to stop mining.
echo  To stop the node later:  bigcoin-cli.exe -datadir=data stop
echo ============================================================
echo.

set "OUTFILE=%TEMP%\bigcoin-mine-%RANDOM%%RANDOM%.txt"
:mineloop
bigcoin-cli.exe "%DD%" -rpcwallet=miner generatetoaddress 1 !ADDR! 100000000 >"%OUTFILE%" 2>&1
if errorlevel 1 (
  echo [!time!] node not reachable - retrying in 15 seconds ...
  timeout /t 15 /nobreak >nul
  goto mineloop
)
findstr /c:"00000" "%OUTFILE%" >nul
if not errorlevel 1 (
  echo [!time!] *** BLOCK FOUND - 50 BIG credited to your wallet ***
) else (
  echo [!time!] mining ... nothing found in this round yet
)
goto mineloop
