SqlSpnManager.psm1

# =============================================================================
# Script : SqlSpnManager.psm1
# Author : Keith Ramsey
# =============================================================================
# Change Log
# -----------------------------------------------------------------------------
# 2026-05-08 Keith Ramsey Phase 0 reconciliation: thin loader replaces
# inline functions; per-function files in Public\
# and Private\.
# 2026-05-28 cross-suite meta Claude DR-312: capture suite-standard
# provenance at module load. ModuleVersion from the
# manifest, SchemaVersion baseline '1.0' (bump
# when SqlSpn.* result shapes break), CommitSha
# best-effort from .git. Add-SqlSpnProvenance reads
# these and stamps them on every public result
# before return (pilot: Invoke-SqlSpnExecutionEngine
# this commit; remaining 12 Public commands in
# follow-on sessions).
# =============================================================================

# --- Provenance, captured once at load (DR-312) ------------------------------
$script:schemaVersion = '1.0'   # SqlSpn.ExecutionResult / SqlSpn.* contract revision
$script:moduleVersion = 'unknown'
try {
    $manifestPath = Join-Path $PSScriptRoot 'SqlSpnManager.psd1'
    if (Test-Path -LiteralPath $manifestPath -PathType Leaf) {
        $m = Import-PowerShellDataFile -Path $manifestPath
        if ($m -and $m.ModuleVersion) { $script:moduleVersion = [string]$m.ModuleVersion }
    }
} catch {
    [void]$_  # Honest fallback: keep 'unknown' rather than fabricate.
}
$script:commitSha = $null
try {
    $gitDir = Join-Path $PSScriptRoot '.git'
    if (Test-Path -LiteralPath $gitDir -PathType Container) {
        $headFile = Join-Path $gitDir 'HEAD'
        if (Test-Path -LiteralPath $headFile -PathType Leaf) {
            $headLine = (Get-Content -LiteralPath $headFile -ErrorAction Stop -TotalCount 1).Trim()
            if ($headLine -match '^ref:\s+(.+)$') {
                $refPath = Join-Path $gitDir $matches[1]
                if (Test-Path -LiteralPath $refPath -PathType Leaf) {
                    $script:commitSha = (Get-Content -LiteralPath $refPath -ErrorAction Stop -TotalCount 1).Trim()
                }
            } elseif ($headLine -match '^[0-9a-f]{7,40}$') {
                $script:commitSha = $headLine
            }
        }
    }
} catch {
    [void]$_  # Honest fallback: $null. CommitSha is best-effort only.
}

Get-ChildItem -Path (Join-Path $PSScriptRoot 'Private') -Filter '*.ps1' -File -ErrorAction SilentlyContinue |
    Sort-Object Name |
    ForEach-Object { . $_.FullName }

Get-ChildItem -Path (Join-Path $PSScriptRoot 'Public') -Filter '*.ps1' -File -ErrorAction SilentlyContinue |
    Sort-Object Name |
    ForEach-Object { . $_.FullName }

Export-ModuleMember -Function @(
    'Add-SqlSpn',
    'Assert-SqlAccountStandard',
    'Export-SqlSpnRegistrationScript',
    'Get-SqlSpnAccount',
    'Get-SqlSpnDiscoveryEngine',
    'Get-SqlSpnInfrastructure',
    'Invoke-SqlSpnExecutionEngine',
    'New-SqlSpnPlan',
    'Remove-SqlSpn',
    'Show-SqlSpnDiagnostic',
    'Start-SqlSpnConfiguration',
    'Start-SqlSpnManager',
    'Test-SqlSpnPlan'
)