# ================================
# BackInfo OS detection script
# Writes OS name for BGInfo/BackInfo
# ================================

Set-ExecutionPolicy Unrestricted

$cvPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion"
$cv = Get-ItemProperty -Path $cvPath

# --- Detect OS by build number ---
$build = [int]$cv.CurrentBuild

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

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

$finalOSName = "$osName $edition"

# --- Registry paths for BackInfo (64bit + 32bit) ---
$regPaths = @(
    "HKLM:\SOFTWARE\BackInfo",
    "HKLM:\SOFTWARE\WOW6432Node\BackInfo"
)

foreach ($path in $regPaths) {
    if (-not (Test-Path $path)) {
        New-Item -Path $path -Force | Out-Null
    }

    New-ItemProperty `
        -Path $path `
        -Name "OSName" `
        -Value $finalOSName `
        -PropertyType String `
        -Force | Out-Null
}

# --- Optional output for logging ---
Write-Output "BackInfo OSName set to: $finalOSName"

$SourceFilePath = "C:\Program Files\BackInfo\BackInfo.exe"
$ShortcutPath = "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\BackInfo.lnk"
$WScriptObj = New-Object -ComObject ("WScript.Shell")
$shortcut = $WscriptObj.CreateShortcut($ShortcutPath)
$shortcut.TargetPath = $SourceFilePath
$shortcut.Save()
