@echo off
rem =====================================================================
rem  Lineage-Next  Overseas Line Switcher
rem
rem  NOTE: this file is intentionally pure ASCII.
rem  cmd.exe parses a .bat using the SYSTEM ANSI codepage, which differs
rem  between Traditional Chinese (cp950) and Simplified Chinese (cp936)
rem  Windows. Any non-ASCII text here would corrupt the commands that
rem  follow it (echo -> cho, curl -> url). Chinese instructions live on
rem  the download page instead.
rem =====================================================================

set "DOMAIN=next.xn--cksr0a.tw"
set "GWIP=34.80.200.109"
set "TAG=# LineageNext-Overseas"
set "HOSTS=%SystemRoot%\System32\drivers\etc\hosts"

title Lineage-Next Overseas Line

net session >nul 2>&1
if not "%errorlevel%"=="0" (
    echo.
    echo   Administrator rights are required.
    echo   Please click "Yes" on the UAC prompt.
    echo.
    powershell -NoProfile -Command "Start-Process -FilePath '%~f0' -Verb RunAs" >nul 2>&1
    exit /b
)

:MENU
cls
echo ==================================================
echo    Lineage-Next   Overseas Line Switcher
echo ==================================================
echo.
echo   Same server, same account, same characters.
echo   This only changes which route you connect through.
echo.
echo   Current status:
findstr /C:"%TAG%" "%HOSTS%" >nul 2>&1
if "%errorlevel%"=="0" (echo       ON    - overseas line) else (echo       OFF   - normal line)
echo.
echo --------------------------------------------------
echo    [1]  Turn ON  overseas line
echo    [2]  Turn OFF - back to normal line
echo    [3]  Exit
echo --------------------------------------------------
echo.
set "CH="
set /p "CH=Type 1 / 2 / 3 then press Enter: "
if "%CH%"=="1" goto ADD
if "%CH%"=="2" goto DEL
if "%CH%"=="3" exit /b
if "%CH%"=="" exit /b
goto MENU

:ADD
echo.
call :BACKUP
call :STRIP
>>"%HOSTS%" echo %TAG%
>>"%HOSTS%" echo %GWIP% %DOMAIN%
ipconfig /flushdns >nul 2>&1
echo   Overseas line is now ON.
call :TEST
echo.
echo   Please CLOSE the browser completely, then open the game again.
echo.
pause
goto MENU

:DEL
echo.
call :BACKUP
call :STRIP
ipconfig /flushdns >nul 2>&1
echo   Restored to the normal line.
call :TEST
echo.
echo   Please CLOSE the browser completely, then open the game again.
echo.
pause
goto MENU

rem ---- keep one pristine copy of hosts, never overwrite it ----
:BACKUP
if not exist "%HOSTS%.nxbak" copy /Y "%HOSTS%" "%HOSTS%.nxbak" >nul 2>&1
exit /b

rem ---- drop any line this tool wrote before, so it never stacks up ----
:STRIP
findstr /V /C:"%TAG%" "%HOSTS%" > "%TEMP%\nx_h1.tmp" 2>nul
findstr /V /C:"%DOMAIN%" "%TEMP%\nx_h1.tmp" > "%TEMP%\nx_h2.tmp" 2>nul
copy /Y "%TEMP%\nx_h2.tmp" "%HOSTS%" >nul 2>&1
del "%TEMP%\nx_h1.tmp" "%TEMP%\nx_h2.tmp" >nul 2>&1
exit /b

rem ---- verify it actually took effect ----
:TEST
echo.
echo   Checking...
ping -n 1 %DOMAIN% > "%TEMP%\nx_ping.txt" 2>&1
findstr /C:"%GWIP%" "%TEMP%\nx_ping.txt" >nul 2>&1
if "%errorlevel%"=="0" (
    echo     Route       overseas  %GWIP%
) else (
    echo     Route       normal
)
del "%TEMP%\nx_ping.txt" >nul 2>&1
set "CODE="
for /f "delims=" %%c in ('curl -s -o nul -w "%%{http_code}" --max-time 15 https://%DOMAIN%/ 2^>nul') do set "CODE=%%c"
if "%CODE%"=="200" (
    echo     Connection  OK - server responded
) else (
    if "%CODE%"=="" (
        echo     Connection  FAILED - cannot reach server
    ) else (
        echo     Connection  ERROR - http %CODE%
    )
)
exit /b
