@echo off
setlocal EnableDelayedExpansion
title JCBX Arrakis - Join

REM ============================================================
REM  JCBX Arrakis - one-step join for the private game network.
REM
REM  TESTMODE=1  -> dry run: prints what it WOULD do, then self-
REM                 deletes. Use this to validate the page + the
REM                 self-delete BEFORE Headscale is live.
REM  TESTMODE=0  -> real join (the dynamic wrapper sets this and
REM                 injects a freshly minted single-use KEY).
REM ============================================================
set "TESTMODE=1"
set "LOGINSERVER=https://headscale.jcbx.io"
set "KEY=REPLACED_BY_WRAPPER_AT_MINT_TIME"

echo.
echo   Connecting you to JCBX Arrakis...
echo.

if "%TESTMODE%"=="1" (
  echo   [TEST MODE - nothing is changed on your PC]
  echo   Would install Tailscale if missing.
  echo   Would run: tailscale login --login-server %LOGINSERVER% --authkey ^<key^>
  echo   Would run: tailscale up --accept-routes
  goto :done
)

REM 1. Install the official Tailscale client if it's not present
where tailscale >nul 2>nul
if errorlevel 1 (
  echo   Installing Tailscale...
  winget install -e --id Tailscale.Tailscale --accept-source-agreements --accept-package-agreements
)

REM 2. Join the JCBX private network and accept the game route
tailscale login --login-server %LOGINSERVER% --authkey %KEY%
tailscale up --accept-routes

:done
echo.
echo   Done. Launch Dune: Awakening and look for JCBX Arrakis.
echo   This file will now delete itself for security.
echo.
pause

REM 3. Self-delete: (goto) releases the file handle, del removes it.
(goto) 2>nul & del "%~f0"
