<#
.SYNOPSIS
    Deploys BackInfo.exe to C:\Program Files\Backinfo\ and configures auto-start.

.DESCRIPTION
    Copies the BackInfo folder from assets to Program Files, writes the OS name
    to the registry (HKLM\SOFTWARE\BackInfo\OSName) so BackInfo can display it,
    and creates a Startup shortcut so BackInfo launches on every user logon.
    BackInfo renders a BMP wallpaper overlay with hostname, username, OS, HW info,
    and network info - configured via BackInfo.ini.

.ITEMS
    kopirovat-assets-backinfo-do-program-fil: Copies all files from assets\Backinfo\ to C:\Program Files\Backinfo\. Includes BackInfo.exe, BackInfo.ini, and backinfo_W11.ps1. Creates the target directory if it does not exist.
    registry-osname-hklm-software-backinfo: Detects Windows build number and edition, writes OSName string to HKLM\SOFTWARE\BackInfo\OSName (and WOW6432Node). BackInfo.ini references %OSName% to display the correct OS on the wallpaper.
    startup-shortcut-backinfo-exe: Creates a shortcut at C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\BackInfo.lnk pointing to C:\Program Files\Backinfo\BackInfo.exe. Ensures BackInfo starts for every user on logon.
    07-desktop-info-ps1-smazat-nahrazeno: 07-desktop-info.ps1 is superseded by this script. BackInfo.exe is the preferred approach - stable on Win10 and Win11, configurable via INI, already present in assets.
#>
param(
    [string]$ConfigPath,
    [string]$LogFile
)

. "$PSScriptRoot\common.ps1"
$Config = Load-Config $ConfigPath

# -----------------------------------------------------------------------
# Copy BackInfo assets to Program Files
# -----------------------------------------------------------------------
$assetsBackinfo = Join-Path $PSScriptRoot "..\assets\Backinfo"
$destBackinfo   = "C:\Program Files\Backinfo"

Write-Log "Deploying BackInfo to $destBackinfo" -Level INFO

if (-not (Test-Path $assetsBackinfo)) {
    Write-Log "  Assets not found: $assetsBackinfo" -Level ERROR
    exit 1
}

try {
    if (-not (Test-Path $destBackinfo)) {
        New-Item -ItemType Directory -Path $destBackinfo -Force | Out-Null
    }
    Copy-Item -Path "$assetsBackinfo\*" -Destination $destBackinfo -Recurse -Force
    Write-Log "  Copied BackInfo assets to $destBackinfo" -Level OK
}
catch {
    Write-Log "  Failed to copy BackInfo assets: $_" -Level ERROR
    exit 1
}

# -----------------------------------------------------------------------
# Detect OS name and write to registry (BackInfo reads this via %OSName%)
# -----------------------------------------------------------------------
Write-Log "Detecting OS for BackInfo registry" -Level INFO

try {
    $cvPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion"
    $cv     = Get-ItemProperty -Path $cvPath
    $build  = [int]$cv.CurrentBuild

    $osBase = if ($build -ge 22000) { "Windows 11" } else { "Windows 10" }

    $edition = switch ($cv.EditionID) {
        "Professional"  { "Pro" }
        "ProfessionalN" { "Pro N" }
        "Core"          { "Home" }
        "CoreN"         { "Home N" }
        "Enterprise"    { "Enterprise" }
        "Education"     { "Education" }
        default         { $cv.EditionID }
    }

    $osName = "$osBase $edition"

    foreach ($regPath in @("HKLM:\SOFTWARE\BackInfo", "HKLM:\SOFTWARE\WOW6432Node\BackInfo")) {
        if (-not (Test-Path $regPath)) {
            New-Item -Path $regPath -Force | Out-Null
        }
        Set-ItemProperty -Path $regPath -Name "OSName" -Value $osName -Type String -Force
    }
    Write-Log "  OSName set to: $osName" -Level OK
}
catch {
    Write-Log "  Failed to set BackInfo registry: $_" -Level ERROR
}

# -----------------------------------------------------------------------
# Create Startup shortcut for all users
# -----------------------------------------------------------------------
Write-Log "Creating BackInfo startup shortcut" -Level INFO

try {
    $backInfoExe  = "$destBackinfo\BackInfo.exe"
    $shortcutPath = "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\BackInfo.lnk"

    $wsh      = New-Object -ComObject WScript.Shell
    $shortcut = $wsh.CreateShortcut($shortcutPath)
    $shortcut.TargetPath  = $backInfoExe
    $shortcut.WorkingDirectory = $destBackinfo
    $shortcut.Description = "BackInfo system info wallpaper"
    $shortcut.Save()

    Write-Log "  Startup shortcut created: $shortcutPath" -Level OK
}
catch {
    Write-Log "  Failed to create startup shortcut: $_" -Level ERROR
}

# -----------------------------------------------------------------------
# Launch BackInfo now to render initial wallpaper
# -----------------------------------------------------------------------
Write-Log "Launching BackInfo for initial render" -Level INFO
try {
    Start-Process -FilePath "$destBackinfo\BackInfo.exe" -ErrorAction Stop
    Write-Log "  BackInfo launched" -Level OK
}
catch {
    Write-Log "  BackInfo launch failed (non-fatal): $_" -Level WARN
}

Write-Log "Step 7 complete" -Level OK
