Files
luxtools-client/install-windows.bat
2026-01-05 13:53:25 +01:00

113 lines
2.9 KiB
Batchfile

@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "SERVICE_NAME=LuxtoolsClient"
set "DISPLAY_NAME=luxtools-client"
set "INSTALL_DIR=%ProgramFiles%\LuxtoolsClient"
set "BIN_PATH=%INSTALL_DIR%\luxtools-client.exe"
set "DATA_DIR=%ProgramData%\LuxtoolsClient"
set "TOKEN_FILE=%DATA_DIR%\token.txt"
set "LISTEN=127.0.0.1:8765"
set "SCRIPT_DIR=%~dp0"
set "SRC_EXE=%SCRIPT_DIR%luxtools-client.exe"
rem Optional args:
rem --listen host:port
if /I "%~1"=="-h" goto :usage
if /I "%~1"=="--help" goto :usage
:parse
if "%~1"=="" goto :main
if /I "%~1"=="--listen" (
set "LISTEN=%~2"
shift
shift
goto :parse
)
echo Unknown arg: %~1
goto :usage
:usage
echo Usage: %~nx0 [--listen host:port]
echo.
echo Installs/updates %DISPLAY_NAME% as a Windows Service (auto-start).
echo Re-running updates the installed binary and restarts the service.
echo The token is stored in %TOKEN_FILE%.
exit /b 2
:main
rem Require admin
net session >nul 2>&1
if not "%ERRORLEVEL%"=="0" (
echo This script must be run as Administrator.
exit /b 1
)
if not exist "%INSTALL_DIR%" mkdir "%INSTALL_DIR%" >nul 2>&1
if not exist "%DATA_DIR%" mkdir "%DATA_DIR%" >nul 2>&1
if not exist "%SRC_EXE%" (
echo Missing binary next to script: %SRC_EXE%
echo Build it first ^(e.g. "go build -o luxtools-client.exe ."^) and re-run.
exit /b 1
)
set "CURRENT_TOKEN="
if exist "%TOKEN_FILE%" (
set /p CURRENT_TOKEN=<"%TOKEN_FILE%"
)
set "SUGGESTED_TOKEN="
if "%CURRENT_TOKEN%"=="" (
rem Generate a URL-safe-ish token; PowerShell is available on modern Windows.
for /f "usebackq delims=" %%T in (`powershell -NoProfile -Command "$b=[byte[]]::new(32);[Security.Cryptography.RandomNumberGenerator]::Fill($b);[Convert]::ToBase64String($b).TrimEnd('=') -replace '\+','-' -replace '/','_'"`) do (
set "SUGGESTED_TOKEN=%%T"
)
)
echo.
if not "%CURRENT_TOKEN%"=="" (
echo A token is already configured. Press Enter to keep it, or type a new one.
) else (
echo No token configured yet. Press Enter to use a generated token, or type your own.
)
set /p TOKEN_INPUT=Token:
if not "%TOKEN_INPUT%"=="" (
set "TOKEN=%TOKEN_INPUT%"
) else (
if not "%CURRENT_TOKEN%"=="" (
set "TOKEN=%CURRENT_TOKEN%"
) else (
set "TOKEN=%SUGGESTED_TOKEN%"
)
)
>"%TOKEN_FILE%" (echo %TOKEN%)
copy /Y "%SRC_EXE%" "%BIN_PATH%" >nul
if not "%ERRORLEVEL%"=="0" exit /b 1
set "BINPATH=\"%BIN_PATH%\" -listen %LISTEN% -token %TOKEN%"
sc.exe query "%SERVICE_NAME%" >nul 2>&1
if "%ERRORLEVEL%"=="0" (
echo Updating existing service...
sc.exe stop "%SERVICE_NAME%" >nul 2>&1
sc.exe config "%SERVICE_NAME%" start= auto binPath= "%BINPATH%" DisplayName= "%DISPLAY_NAME%" >nul
) else (
echo Creating service...
sc.exe create "%SERVICE_NAME%" start= auto binPath= "%BINPATH%" DisplayName= "%DISPLAY_NAME%" >nul
)
sc.exe start "%SERVICE_NAME%" >nul 2>&1
echo.
echo Installed/updated %DISPLAY_NAME%.
echo - Binary: %BIN_PATH%
echo - Token: %TOKEN%
echo - Listen: %LISTEN%
endlocal
exit /b 0