@echo off

SetLocal EnableDelayedExpansion

CLS
ECHO ***WARNING: DO NOT RUN THIS UNLESS RAN AS ADMINISTRATOR***
ECHO 1. Install Gateways
ECHO 2. Uninstall Gateways
ECHO 3. Revert Gateways
ECHO.

CHOICE /C 123 /M "Enter your choice: "

IF ERRORLEVEL 3 GOTO Revert
IF ERRORLEVEL 2 GOTO Uninstall
IF ERRORLEVEL 1 GOTO Install

:Install
reg query "HKCU\Software\Battle.net\Configuration" /v "Diablo II Battle.net gateways" | findstr /C:"roc.xtimus.com\06\0RealmOfCain">nul && (
    Echo Gateways are already installed!
) || (
    GOTO InstallA
)

GOTO End

:InstallA
    Echo Installing...
    
    set bgates="test"
    for /f "tokens=5*" %%d in ('reg query "HKCU\Software\Battle.net\Configuration" /v "Diablo II Battle.net gateways"') do set bgates=%%~e
    REG ADD "HKCU\Software\Battle.net\Configuration\D2Backup" /v "Diablo II Battle.net original gateways" /t REG_MULTI_SZ /d "%bgates%" /f
    
    set rnew="roc.xtimus.com\06\0RealmOfCain"
    for /f "tokens=5*" %%a in ('reg query "HKCU\Software\Battle.net\Configuration" /v "Diablo II Battle.net gateways"') do set rgates=%%~b
    REG ADD "HKCU\Software\Battle.net\Configuration" /v "Diablo II Battle.net gateways" /t REG_MULTI_SZ /d "%rgates%\0%rnew%" /f
    
    Echo Installed successfully, please start or restart Diablo II!
GOTO End

:Uninstall
reg query "HKCU\Software\Battle.net\Configuration\D2Backup" /v "Diablo II Battle.net original gateways">nul && (
    GOTO UninstallA
) || (
    Echo Gateways aren't even installed!
)

GOTO End

:UninstallA
    Echo Uninstalling...
    
    for /f "tokens=6*" %%a in ('reg query "HKCU\Software\Battle.net\Configuration\D2Backup" /v "Diablo II Battle.net original gateways"') do set rgates=%%~b
    REG ADD "HKCU\Software\Battle.net\Configuration" /v "Diablo II Battle.net gateways" /t REG_MULTI_SZ /d "%rgates%" /f
    
    REG DELETE "HKCU\Software\Battle.net\Configuration\D2Backup" /v "Diablo II Battle.net original gateways" /f
    
    Echo Uninstalled successfully, please start or restart Diablo II!
GOTO End

:Revert
REG ADD "HKCU\Software\Battle.net\Configuration" /v "Diablo II Battle.net gateways" /t REG_MULTI_SZ /d "1002\000\0uswest.battle.net\08\0U.S. West\0useast.battle.net\06\0U.S. East\0asia.battle.net\0-9\0Asia\0europe.battle.net\0-1\0Europe" /f
Echo Gateways were reverted!

:End
pause