# Restore-EDncReg.ps1 - Import a per-machine eDNC .reg backup from a folder.
#
# Two callers:
#   1. Standard/03-RestoreEDncConfig.ps1 (imaging-time, source = local PXE copy)
#   2. Shopfloor/lib/Update-MachineNumber.ps1 (post-image 9999 -> real, source
#      = tsgwp00525 SFLD share mounted via existing SFLD credential pattern)
#
# The .reg files live as <num>-<host>.reg or <num>.reg. Returns the path
# that was imported, or $null if nothing was found/imported. Errors are
# caught and logged via Write-Host (no throwing); callers continue.
#
# Dot-source with:
#   . "$PSScriptRoot\lib\Restore-EDncReg.ps1"        (from Shopfloor\)
#   . "$PSScriptRoot\..\Shopfloor\lib\Restore-EDncReg.ps1"  (from Standard\)

function Mount-SFLDShare {
    <#
    .SYNOPSIS
        Mounts a \\server\share path using SFLD creds from
        HKLM:\SOFTWARE\GE\SFLD\Credentials\*. Returns $true on success,
        $false if creds are missing or mount fails.
    #>
    param(
        [Parameter(Mandatory)][string]$SharePath,
        [string]$DriveLetter = 'V:'
    )

    $server = ($SharePath -replace '^\\\\', '') -split '\\' | Select-Object -First 1
    $basePath = 'HKLM:\SOFTWARE\GE\SFLD\Credentials'
    if (-not (Test-Path $basePath)) { return $false }

    $cred = $null
    foreach ($entry in Get-ChildItem -Path $basePath -ErrorAction SilentlyContinue) {
        $props = Get-ItemProperty -Path $entry.PSPath -ErrorAction SilentlyContinue
        if (-not $props -or -not $props.TargetHost) { continue }
        if ($props.TargetHost -eq $server -or
            $props.TargetHost -like "$server.*" -or
            $server -like "$($props.TargetHost).*") {
            $cred = $props; break
        }
    }
    if (-not $cred -or -not $cred.Username -or -not $cred.Password) { return $false }

    & net use $DriveLetter /delete /y 2>$null | Out-Null
    $null = & net use $DriveLetter $SharePath /user:$($cred.Username) $($cred.Password) /persistent:no 2>&1
    return ($LASTEXITCODE -eq 0)
}

function Import-EDncRegBackup {
    <#
    .SYNOPSIS
        Looks for <MachineNumber>-*.reg or <MachineNumber>.reg in SourceRoot
        and runs `reg import` on it. Caller is responsible for mounting any
        network share first; this function works on whatever local or drive-
        letter path is handed in.
    .PARAMETER SourceRoot
        Directory containing the .reg backups.
    .PARAMETER MachineNumber
        The target machine number (digits). Used as a filename prefix.
    .OUTPUTS
        Path of the imported file, or $null if none found / import failed.
    #>
    param(
        [Parameter(Mandatory)][string]$SourceRoot,
        [Parameter(Mandatory)][string]$MachineNumber
    )

    if (-not (Test-Path -LiteralPath $SourceRoot)) {
        Write-Host "  Restore-EDncReg: source root not found: $SourceRoot"
        return $null
    }

    $candidate = Get-ChildItem -Path $SourceRoot -Filter "$MachineNumber.reg" -File -ErrorAction SilentlyContinue |
                 Select-Object -First 1
    if (-not $candidate) {
        Write-Host "  Restore-EDncReg: no backup for machine $MachineNumber in $SourceRoot"
        return $null
    }

    Write-Host "  Restore-EDncReg: importing $($candidate.FullName)"
    $out = & reg.exe import "$($candidate.FullName)" 2>&1
    if ($LASTEXITCODE -ne 0) {
        Write-Host "  Restore-EDncReg: reg import failed (exit $LASTEXITCODE): $out"
        return $null
    }
    Write-Host "  Restore-EDncReg: imported OK"
    return $candidate.FullName
}
