

:: Copyright 2018-present Network Optix, Inc. Licensed under MPL 2.0: www.mozilla.org/MPL/2.0/

@echo off
setlocal EnableDelayedExpansion

:: Step 1: Define variables
set "INI_DIR=%LocalAppData%\nx_ini"
set "INI_FILE=%INI_DIR%\desktop_client.ini"

:: Step 2: Create directory if it doesn't exist
if not exist "%INI_DIR%" (
    mkdir "%INI_DIR%"
)

:: Step 3: Check if the INI file exists, if not, create it
if not exist "%INI_FILE%" (
    type nul > "%INI_FILE%"
)

:: Step 4: Ask user for graphics API choice
:ChooseAPI
echo.
echo Select your preferred graphics API:
echo [1] DirectX (direct3d11)
echo [2] OpenGL (opengl)
echo.
set /p "API_CHOICE=Enter 1 for DirectX or 2 for OpenGL: "
if "%API_CHOICE%"=="1" (
    set "API_VALUE=direct3d11"
) else if "%API_CHOICE%"=="2" (
    set "API_VALUE=opengl"
) else (
    echo Invalid choice. Please try again.
    goto ChooseAPI
)

:: Step 5: Update or add graphicsApi line
set "FOUND=0"

(
for /f "delims=" %%L in ('findstr /n "^" "%INI_FILE%"') do (
    set "LINE=%%L"
    set "LINE=!LINE:*:=!"
    set "CHECK=!LINE:#=!"
    if /i "!CHECK:~0,11!"=="graphicsApi" (
        echo graphicsApi="!API_VALUE!"
        set "FOUND=1"
    ) else (
        echo(!LINE!
    )
)
) > "%INI_FILE%.tmp"

if !FOUND! == 0 (
    echo graphicsApi="!API_VALUE!" >> "%INI_FILE%.tmp"
)

move /Y "%INI_FILE%.tmp" "%INI_FILE%" >nul

:: Step 6: Display message to the user
echo.
echo You successfully changed the graphicsApi value to "!API_VALUE!".
echo Please restart the Nx Witness Desktop Client to apply the change.
echo.

pause
endlocal

