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

echo ============================================
echo  ZYL GPU miner - official pool
echo  47.238.103.203:16102
echo  Device id required: ZYL00001 .. ZYL10000
echo  CLI and GUI cannot use the same id at once
echo  Wallet address is NOT accepted
echo ============================================
echo.

wmic path win32_VideoController get Name
echo.
echo Need NVIDIA or AMD. Intel iGPU is skipped.
echo.

if exist "%~dp0eid-opencl-miner.exe" goto HAVE_BIN
if exist "%~dp0..\build\opencl-miner\Release\eid-opencl-miner.exe" (
  copy /Y "%~dp0..\build\opencl-miner\Release\eid-opencl-miner.exe" "%~dp0eid-opencl-miner.exe" >nul
  goto HAVE_BIN
)
if exist "%~dp0..\build\opencl-miner\eid-opencl-miner.exe" (
  copy /Y "%~dp0..\build\opencl-miner\eid-opencl-miner.exe" "%~dp0eid-opencl-miner.exe" >nul
  goto HAVE_BIN
)
if exist "%~dp0..\miner-build\opencl-miner\eid-opencl-miner.exe" (
  copy /Y "%~dp0..\miner-build\opencl-miner\eid-opencl-miner.exe" "%~dp0eid-opencl-miner.exe" >nul
  goto HAVE_BIN
)

echo ERROR: eid-opencl-miner.exe not found.
pause
exit /b 1

:HAVE_BIN
set "DEVICE_ID=%~1"
if not defined DEVICE_ID if exist "%~dp0device-id.txt" (
  set /p DEVICE_ID=<"%~dp0device-id.txt"
)
if not defined DEVICE_ID (
  set /p DEVICE_ID=Enter official device id [ZYL00001]: 
)
if not defined DEVICE_ID (
  echo ERROR: device id required. Format ZYL00001 .. ZYL10000
  pause
  exit /b 1
)

set "DEVICE_ID=!DEVICE_ID: =!"
set "DEVICE_ID=!DEVICE_ID:z=Z!"
set "DEVICE_ID=!DEVICE_ID:y=Y!"
set "DEVICE_ID=!DEVICE_ID:l=L!"

echo(!DEVICE_ID!| findstr /R /C:"^ZYL[0-9][0-9][0-9][0-9][0-9]$" >nul
if errorlevel 1 (
  echo ERROR: device id must be ZYL + 5 digits, e.g. ZYL00001
  echo Do NOT use a wallet address.
  echo Do NOT use package slot like ZYL00001-1. Use the device number.
  pause
  exit /b 1
)

> "%~dp0device-id.txt" echo !DEVICE_ID!

echo Starting miner with --device-id !DEVICE_ID!
echo Open Aliyun security group TCP 16102 if connect fails.
echo.
"%~dp0eid-opencl-miner.exe" --device-id !DEVICE_ID! --server 47.238.103.203 --port 16102 --force3g
echo.
pause
