@echo off
title Blue Primes - Client Tunnel Setup
color 0A

net session >nul 2>&1
if %errorLevel% neq 0 (
    powershell -Command "Start-Process '%~f0' -Verb RunAs"
    exit
)

:: ==============================
set "SERVICE_URL=http://127.0.0.1:8000"
set "DOMAIN=blueprimes.com"
:: ==============================

set "CF_DIR=%USERPROFILE%\.cloudflared"
set "CF_EXE=%~dp0cloudflared.exe"
set "INSTALL_DIR=C:\BluePrimes"

:START
cls
echo ================================================
echo    Blue Primes Software - Client Setup Tool
echo ================================================
echo.

set CLIENT_NAME=
set /p CLIENT_NAME=Client ka naam likho (jaise: ali, nazeer, sara): 
echo.

if "%CLIENT_NAME%"=="" (
    echo ERROR: Naam khali nahi chor sakte!
    pause
    goto START
)

echo %CLIENT_NAME%| findstr /r "[^a-zA-Z0-9_-]" >nul
if %errorLevel% equ 0 (
    echo ERROR: Naam mein sirf letters/numbers allowed hain!
    pause
    goto START
)

:: ── Prerequisites ─────────────────────────────────
if not exist "%CF_EXE%" (
    echo ERROR: cloudflared.exe is script ke saath hona chahiye!
    pause
    exit
)

if not exist "%CF_DIR%" mkdir "%CF_DIR%"
if not exist "%INSTALL_DIR%" mkdir "%INSTALL_DIR%"

:: ── Cloudflare Login ───────────────────────────────
if not exist "%CF_DIR%\cert.pem" (
    if exist "%~dp0cert.pem" (
        copy "%~dp0cert.pem" "%CF_DIR%\cert.pem" >nul
        echo cert.pem copy ho gaya!
    ) else (
        echo Cloudflare login window khul rahi hai...
        echo Login karo aur browser band karo - phir yahan Enter dabao.
        "%CF_EXE%" tunnel login
        if not exist "%CF_DIR%\cert.pem" (
            echo ERROR: Login nahi ho saka! Dobara try karo.
            pause
            goto START
        )
    )
)

:: cloudflared install dir mein copy karo
copy "%CF_EXE%" "%INSTALL_DIR%\cloudflared.exe" >nul

:: ── STEP 1: Cleanup ────────────────────────────────
echo.
echo [1/7] Purana cleanup kar raha hoon...
net stop cloudflared >nul 2>&1
timeout /t 2 >nul
"%INSTALL_DIR%\cloudflared.exe" service uninstall >nul 2>&1
timeout /t 2 >nul
"%INSTALL_DIR%\cloudflared.exe" tunnel delete --force %CLIENT_NAME% >nul 2>&1
timeout /t 2 >nul
del "%CF_DIR%\*.json" >nul 2>&1
echo     Cleanup complete!

:: ── STEP 2: Tunnel Create ──────────────────────────
echo.
echo [2/7] Tunnel bana raha hoon "%CLIENT_NAME%" ke liye...
set "TUNNEL_ID="
for /f "tokens=*" %%L in ('"%INSTALL_DIR%\cloudflared.exe" tunnel create %CLIENT_NAME% 2^>^&1') do (
    echo     %%L
    echo %%L | findstr /i "Created tunnel" >nul
    if not errorlevel 1 (
        for /f "tokens=6" %%I in ("%%L") do set "TUNNEL_ID=%%I"
    )
)

if "%TUNNEL_ID%"=="" (
    echo.
    echo ERROR: Tunnel nahi bana! Dobara try karo.
    pause
    goto START
)

set "JSON_FILE=%CF_DIR%\%TUNNEL_ID%.json"
if not exist "%JSON_FILE%" (
    echo ERROR: JSON file nahi mili: %JSON_FILE%
    pause
    goto START
)
echo     Tunnel ID: %TUNNEL_ID%

:: ── STEP 3: Config File ────────────────────────────
echo.
echo [3/7] Config file bana raha hoon...
(
echo tunnel: %TUNNEL_ID%
echo credentials-file: %JSON_FILE%
echo.
echo ingress:
echo   - hostname: %CLIENT_NAME%.%DOMAIN%
echo     service: %SERVICE_URL%
echo   - service: http_status:404
) > "%CF_DIR%\config.yml"
echo     Config file ban gayi!

:: ── STEP 4: DNS Route (tunnel create ke BAAD) ──────
echo.
echo [4/7] DNS route set kar raha hoon...
"%INSTALL_DIR%\cloudflared.exe" tunnel route dns --overwrite-dns %CLIENT_NAME% %CLIENT_NAME%.%DOMAIN%
if %errorLevel% neq 0 (
    echo ERROR: DNS route set nahi ho saka!
    pause
    goto START
)
echo     DNS route set ho gaya!

:: ── STEP 5: tunnel_start.bat ───────────────────────
echo.
echo [5/7] Startup files bana raha hoon...
(
echo @echo off
echo "%INSTALL_DIR%\cloudflared.exe" tunnel --config "%CF_DIR%\config.yml" run %CLIENT_NAME%
) > "%INSTALL_DIR%\tunnel_start.bat"

:: ── STEP 6: tunnel_start.vbs (silent background) ───
(
echo Set WshShell = CreateObject^("WScript.Shell"^)
echo WshShell.Run """%INSTALL_DIR%\tunnel_start.bat""", 0, False
echo Set WshShell = Nothing
) > "%INSTALL_DIR%\tunnel_start.vbs"
echo     Startup files ban gayi!

:: ── STEP 7: Windows Startup Register ──────────────
echo.
echo [6/7] Windows Startup mein register kar raha hoon...
set "STARTUP_FOLDER=%APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup"
copy "%INSTALL_DIR%\tunnel_start.vbs" "%STARTUP_FOLDER%\blueprimes_tunnel.vbs" >nul
echo     Startup mein register ho gaya!

:: ── STEP 8: Tunnel Start ───────────────────────────
echo.
echo [7/7] Tunnel start kar raha hoon...
wscript.exe "%INSTALL_DIR%\tunnel_start.vbs"
timeout /t 6 >nul
echo     Tunnel start ho gaya!

:: ── DONE ───────────────────────────────────────────
cls
echo.
echo ================================================
echo         SETUP COMPLETE - 100%% SUCCESS!
echo ================================================
echo.
echo   Client Naam  :  %CLIENT_NAME%
echo   Live Link    :  https://%CLIENT_NAME%.%DOMAIN%
echo   Local Link   :  %SERVICE_URL%
echo   Tunnel ID    :  %TUNNEL_ID%
echo.
echo ------------------------------------------------
echo   NOTES:
echo   - PC restart par tunnel AUTO-START hoga
echo   - Background mein SILENTLY chalega
echo   - Internet aate hi ACCESSIBLE hoga
echo   - Pehli dafa link open karne se pehle
echo     30 seconds wait karo (DNS time lagta hai)
echo ================================================
echo.
pause