@echo off
setlocal EnableExtensions

REM ============================================================
REM XIBO Windows Provisioning
REM ============================================================

set "WINLOGON=HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"

set "XIBOUSER=xibo"
set "XIBOPASS=!!Xibo2026"

set "XIBOSOURCE=C:\linuxmuster-win\xibo\default.config.xml"

set "NEEDREBOOT=0"


REM ============================================================
REM Lokalen Xibo Benutzer pruefen
REM ============================================================

net user "%XIBOUSER%" >nul 2>&1

if errorlevel 1 (
    echo [%DATE% %TIME%] FEHLER: Benutzer xibo fehlt. >> C:\Windows\Temp\xibo-setup.log
    exit /b 10
)


REM ============================================================
REM Xibo Installationsverzeichnis suchen
REM ============================================================

set "XIBODIR="

if exist "C:\Program Files (x86)\Xibo Player\XiboClient.exe" (
    set "XIBODIR=C:\Program Files (x86)\Xibo Player"
)

if exist "C:\Program Files\Xibo Player\XiboClient.exe" (
    set "XIBODIR=C:\Program Files\Xibo Player"
)


REM ============================================================
REM Zentrale Xibo-Konfiguration kopieren
REM ============================================================

if defined XIBODIR (
    if exist "%XIBOSOURCE%" (
        copy /Y "%XIBOSOURCE%" "%XIBODIR%\default.config.xml" >nul
    )
)


REM ============================================================
REM AutoLogin pruefen
REM ============================================================

reg query "%WINLOGON%" /v AutoAdminLogon 2>nul | findstr /R /C:"REG_SZ.*1$" >nul
if errorlevel 1 set "NEEDREBOOT=1"

reg query "%WINLOGON%" /v DefaultUserName 2>nul | findstr /I /R /C:"REG_SZ.*xibo$" >nul
if errorlevel 1 set "NEEDREBOOT=1"

reg query "%WINLOGON%" /v DefaultDomainName 2>nul | findstr /I /C:"%COMPUTERNAME%" >nul
if errorlevel 1 set "NEEDREBOOT=1"

reg query "%WINLOGON%" /v DefaultPassword >nul 2>&1
if errorlevel 1 set "NEEDREBOOT=1"


REM ============================================================
REM AutoLogin setzen
REM ============================================================

reg add "%WINLOGON%" /v AutoAdminLogon /t REG_SZ /d "1" /f >nul

reg add "%WINLOGON%" /v DefaultUserName /t REG_SZ /d "%XIBOUSER%" /f >nul

reg add "%WINLOGON%" /v DefaultDomainName /t REG_SZ /d "%COMPUTERNAME%" /f >nul

reg add "%WINLOGON%" /v DefaultPassword /t REG_SZ /d "%XIBOPASS%" /f >nul


echo [%DATE% %TIME%] AutoLogin gesetzt: %COMPUTERNAME%\%XIBOUSER% >> C:\Windows\Temp\xibo-setup.log


REM ============================================================
REM Falls erstmalig gesetzt -> einmal neu starten
REM ============================================================

if "%NEEDREBOOT%"=="1" (

    if not exist "C:\Windows\Temp\xibo-autologon-reboot.tag" (

        echo 1 > "C:\Windows\Temp\xibo-autologon-reboot.tag"

        shutdown /r /t 5 /c "Xibo AutoLogin wird eingerichtet"

        exit /b 0
    )
)


REM Marker nach erfolgreichem Folgestart entfernen

if exist "C:\Windows\Temp\xibo-autologon-reboot.tag" (
    del /Q "C:\Windows\Temp\xibo-autologon-reboot.tag"
)

exit /b 0
