@echo off
setlocal EnableExtensions DisableDelayedExpansion
title Benicode - Installation des logiciels
for /f "delims=" %%E in ('echo prompt $E^| cmd') do set "ESC=%%E"
set "BRAND_DARK=%ESC%[38;2;36;56;45m"
set "BRAND_LIGHT=%ESC%[38;2;183;220;148m"
set "BRAND_PANEL=%ESC%[48;2;36;56;45m"
set "STATUS_WARN=%ESC%[38;2;255;207;102m"
set "STATUS_ERROR=%ESC%[38;2;255;135;112m"
set "RESET=%ESC%[0m"
color 0A
cls
call :brand_banner
echo.
echo %BRAND_LIGHT%PACK LOGICIELS WINDOWS%RESET%
echo %BRAND_LIGHT%[=] LibreOffice   [@] Firefox   [7z] 7-Zip   /!\ VLC%RESET%
echo.
echo %BRAND_LIGHT%[+] Driver Booster Free   [*] Advanced SystemCare Free%RESET%
echo.
echo Ce script installe ou met a jour ces six logiciels avec WinGet.
echo Leurs contrats de licence sont acceptes automatiquement.
echo Le fond d'ecran Benicode sera applique a ce compte Windows.
echo Une connexion Internet est necessaire. Autorisez les demandes Windows.
echo.
where winget.exe >nul 2>&1
if errorlevel 1 (
    echo %STATUS_ERROR%[ERREUR] WinGet est introuvable.%RESET%
    echo Installez ou mettez a jour App Installer dans le Microsoft Store.
    echo Puis fermez cette fenetre et relancez le script.
    pause
    exit /b 1
)
set "review=0"
call :install TheDocumentFoundation.LibreOffice LibreOffice "[=]"
call :install Mozilla.Firefox Firefox "[@]"
call :install 7zip.7zip 7-Zip "[7z]"
call :install VideoLAN.VLC VLC "/!\"
call :install IObit.DriverBooster "Driver Booster Free" "[+]"
call :install IObit.AdvancedSystemCare "Advanced SystemCare Free" "[*]"
call :wallpaper
echo.
echo ============================================================
if "%review%"=="0" (
    echo %BRAND_LIGHT%[TERMINE] Les logiciels et le fond d'ecran ont ete traites avec succes.%RESET%
) else (
    echo %STATUS_WARN%[A VERIFIER] %review% commandes dans les messages ci-dessus.%RESET%
    echo Un logiciel deja a jour peut aussi produire un code non nul.
)
echo ============================================================
pause
if not "%review%"=="0" exit /b 1
exit /b 0

:brand_banner
echo %BRAND_PANEL%%BRAND_LIGHT%                                                                            %RESET%
echo %BRAND_PANEL%%BRAND_LIGHT%      ____  _____ _   _ ___ ____ ___  ____  _____       ____                %RESET%
echo %BRAND_PANEL%%BRAND_LIGHT%     ^| __ ^)^| ____^| \ ^| ^|_ _/ ___/ _ \^|  _ \^| ____^|     / __ )               %RESET%
echo %BRAND_PANEL%%BRAND_LIGHT%     ^|  _ \^|  _^| ^|  \^| ^|^| ^| ^|  ^| ^| ^| ^| ^| ^| ^| ^|  _^|      / __  ^|             %RESET%
echo %BRAND_PANEL%%BRAND_LIGHT%     ^| ^|_^) ^| ^|___^| ^|\  ^|^| ^| ^|__^| ^|_^| ^| ^|_^| ^| ^|___    / /_/ /                %RESET%
echo %BRAND_PANEL%%BRAND_LIGHT%     ^|____/^|_____^|_^| \_^|___\____\___/^|____/^|_____^|  /_____/                 %RESET%
echo %BRAND_PANEL%%BRAND_LIGHT%                                                                            %RESET%
echo %BRAND_LIGHT%                         Des outils utiles, simplement.%RESET%
exit /b 0

:wallpaper
echo.
echo %BRAND_LIGHT%--- Fond d'ecran Benicode ---%RESET%
set "BENICODE_INSTALLER=%~f0"
powershell.exe -NoLogo -NoProfile -Command "$ErrorActionPreference='Stop'; try { $content=[IO.File]::ReadAllText($env:BENICODE_INSTALLER); $parts=$content -split '(?m)^# BENICODE_WALLPAPER\r?$',2; if ($parts.Count -ne 2) { throw 'Section wallpaper introuvable' }; & ([scriptblock]::Create($parts[1])); exit 0 } catch { Write-Host ('ERREUR fond ecran: '+$_.Exception.Message); exit 1 }"
if errorlevel 1 (
    echo %STATUS_WARN%[A VERIFIER] Le fond d'ecran n'a pas pu etre applique.%RESET%
    set /a review+=1 >nul
) else (
    echo %BRAND_LIGHT%[OK] Fond d'ecran Benicode applique.%RESET%
)
exit /b 0

:install
echo.
echo %BRAND_LIGHT%--- %~3 %~2 ---%RESET%
winget.exe install --id %~1 --exact --source winget --silent --accept-package-agreements --accept-source-agreements
set "result=%errorlevel%"
if "%result%"=="0" (
    echo %BRAND_LIGHT%[OK] %~2%RESET%
) else (
    echo %STATUS_WARN%[A VERIFIER] %~2 - code %result%%RESET%
    set /a review+=1 >nul
)
exit /b 0

# BENICODE_WALLPAPER
$folder = Join-Path ([Environment]::GetFolderPath('LocalApplicationData')) 'Benicode'
[IO.Directory]::CreateDirectory($folder) | Out-Null
$download = Join-Path $folder (([Guid]::NewGuid().ToString()) + '.png')
$wallpaper = Join-Path $folder 'desktop-wallpaper.bmp'
$desktopKey = 'HKCU:\Control Panel\Desktop'
$previous = Get-ItemProperty -LiteralPath $desktopKey
$backup = Join-Path $folder 'previous-wallpaper.json'
if (-not (Test-Path -LiteralPath $backup)) {
    $previous | Select-Object Wallpaper, WallpaperStyle, TileWallpaper | ConvertTo-Json | Set-Content -LiteralPath $backup -Encoding UTF8
}
try {
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    Invoke-WebRequest -UseBasicParsing -Uri 'https://assets.benicode.fr/benicode-wallpaper-nokia-snake.png' -OutFile $download -TimeoutSec 60
    Add-Type -AssemblyName System.Drawing
    $image = [Drawing.Image]::FromFile($download)
    try { $image.Save($wallpaper, [Drawing.Imaging.ImageFormat]::Bmp) } finally { $image.Dispose() }
    Add-Type -TypeDefinition @'
using System;
using System.Runtime.InteropServices;
public static class BenicodeDesktop {
    [DllImport("user32.dll", EntryPoint="SystemParametersInfoW", CharSet=CharSet.Unicode, SetLastError=true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool SetWallpaper(uint action, uint parameter, string path, uint flags);
}
'@
    Set-ItemProperty -LiteralPath $desktopKey -Name WallpaperStyle -Value '6'
    Set-ItemProperty -LiteralPath $desktopKey -Name TileWallpaper -Value '0'
    if (-not [BenicodeDesktop]::SetWallpaper(20, 0, $wallpaper, 3)) {
        Set-ItemProperty -LiteralPath $desktopKey -Name WallpaperStyle -Value $previous.WallpaperStyle
        Set-ItemProperty -LiteralPath $desktopKey -Name TileWallpaper -Value $previous.TileWallpaper
        throw 'Windows a refuse le changement du fond ecran.'
    }
} finally {
    if (Test-Path -LiteralPath $download) { Remove-Item -LiteralPath $download -Force }
}

