@echo off
setlocal EnableDelayedExpansion

set SERVER=http://162.0.237.134:8081
set TMP=%TEMP%\it-deployment
set ME_DIR=%TMP%\ManageEngine
set WALLPAPER_DIR=%USERPROFILE%\Downloads
set FALCON_CID=C92F0041E19B488FA0A5C36021AC5CE8-FB

mkdir "%ME_DIR%" 2>nul

echo =====================================
echo  Provisioning : %COMPUTERNAME%
echo  Started      : %DATE% %TIME%
echo =====================================
echo(

:: ── Chrome ───────────────────────────────────────────────
echo [1/6] Google Chrome
if exist "C:\Program Files\Google\Chrome\Application\chrome.exe" (
    echo   Already installed, skipping
) else (
    echo   Downloading...
    curl -fsSL -o "%TMP%\ChromeSetup.exe" "%SERVER%/ChromeSetup.exe"
    echo   Installing...
    start /wait "" "%TMP%\ChromeSetup.exe" /silent /install
    echo   Done
)
echo(

:: ── Slack ────────────────────────────────────────────────
echo [2/6] Slack
if exist "%LOCALAPPDATA%\slack\slack.exe" (
    echo   Already installed, skipping
) else (
    echo   Downloading...
    curl -fsSL -o "%TMP%\Slack.msix" "%SERVER%/Slack.msix"
    echo   Installing...
    powershell -NoProfile -Command "Add-AppxPackage -Path '%TMP%\Slack.msix'"
    echo   Done
)
echo(

:: ── Zoom ─────────────────────────────────────────────────
echo [3/6] Zoom
if exist "C:\Program Files\Zoom\bin\Zoom.exe" (
    echo   Already installed, skipping
) else (
    echo   Downloading...
    curl -fsSL -o "%TMP%\ZoomInstaller.exe" "%SERVER%/ZoomInstallerFull.exe"
    echo   Installing...
    start /wait "" "%TMP%\ZoomInstaller.exe" /silent
    echo   Done
)
echo(

:: ── CrowdStrike Falcon ───────────────────────────────────
echo [4/6] CrowdStrike Falcon
sc query csagent >nul 2>&1
if !ERRORLEVEL!==0 (
    echo   Already installed, skipping
) else (
    echo   Downloading...
    curl -fsSL -o "%TMP%\FalconSensor.exe" "%SERVER%/FalconSensor_Windows.exe"
    echo   Installing...
    "%TMP%\FalconSensor.exe" /install /quiet /norestart CID=!FALCON_CID!
    if !ERRORLEVEL!==0 (
        echo   Done
    ) else (
        echo   Warning: Exit code !ERRORLEVEL!
    )
)
echo(

:: ── ManageEngine UEMS ────────────────────────────────────
echo [5/6] ManageEngine UEMS
sc query DCFAService >nul 2>&1
if !ERRORLEVEL!==0 (
    echo   Already installed, skipping
) else (
    echo   Downloading...
    powershell -NoProfile -Command ^
        "$base = '%SERVER%/DefaultRemoteOffice 7 May 26'; " ^
        "$dest = '%ME_DIR%'; " ^
        "$files = @('setup.bat','UEMSAgent.msi','UEMSAgent.mst','DCAgentServerInfo.json','dcremagentinstaller.exe','DMRootCA.crt','RemCom.exe','computernames.txt'); " ^
        "foreach ($f in $files) { Invoke-WebRequest -Uri \"$base/$f\" -OutFile \"$dest\$f\" -UseBasicParsing }"
    echo   Installing...
    cd /d "%ME_DIR%"
    setup.bat
    cd /d "%TMP%"
    echo   Done
)
echo(

:: ── Wallpaper & Lockscreen ───────────────────────────────
echo [6/6] Wallpaper ^& Lockscreen
echo   Downloading...
curl -fsSL -o "%WALLPAPER_DIR%\wallpaper.png"  "%SERVER%/Home_Wallpaper.png"
curl -fsSL -o "%WALLPAPER_DIR%\lockscreen.png" "%SERVER%/Lockscreen_Wallpaper.png"

:: Set desktop wallpaper via registry + RUNDLL32
echo   Setting desktop wallpaper...
reg add "HKCU\Control Panel\Desktop" /v Wallpaper /t REG_SZ /d "%WALLPAPER_DIR%\wallpaper.png" /f >nul
reg add "HKCU\Control Panel\Desktop" /v WallpaperStyle /t REG_SZ /d "10" /f >nul
reg add "HKCU\Control Panel\Desktop" /v TileWallpaper /t REG_SZ /d "0" /f >nul
RUNDLL32.EXE user32.dll,UpdatePerUserSystemParameters

:: Set lockscreen via registry
echo   Setting lockscreen...
reg add "HKLM\SOFTWARE\Policies\Microsoft\Windows\Personalization" /v LockScreenImage /t REG_SZ /d "%WALLPAPER_DIR%\lockscreen.png" /f >nul
reg add "HKLM\SOFTWARE\Policies\Microsoft\Windows\Personalization" /v LockScreenOverlaysDisabled /t REG_DWORD /d 1 /f >nul

echo   Done
echo(

:: ── Cleanup ──────────────────────────────────────────────
del /q "%TMP%\*.exe" "%TMP%\*.msi" "%TMP%\*.msix" 2>nul
del /q "%ME_DIR%\*" 2>nul

:: ── Done ─────────────────────────────────────────────────
echo =====================================
echo  Done : %DATE% %TIME%
echo  NEXT : Assign %COMPUTERNAME% in ManageEngine
echo =====================================
echo(
pause