$ErrorActionPreference = "Stop"
$root = Split-Path -Parent $MyInvocation.MyCommand.Path
$files = [ordered]@{}

$verifiedRoots = @(
    (Join-Path $root "payload"),
    (Join-Path $root "external-tools")
)

$verifiedRoots |
    Where-Object { Test-Path -LiteralPath $_ } |
    ForEach-Object { Get-ChildItem -LiteralPath $_ -File -Recurse } |
    Where-Object { $_.Extension -ne ".pdb" } |
    Sort-Object FullName |
    ForEach-Object {
        $relative = $_.FullName.Substring($root.Length + 1).Replace("\", "/")
        $files[$relative] = (Get-FileHash -LiteralPath $_.FullName -Algorithm SHA256).Hash.ToLowerInvariant()
    }

$manifest = [ordered]@{
    product = "INPLUG.BIM"
    version = "1.6.2"
    generatedAt = [DateTimeOffset]::UtcNow.ToString("O")
    files = $files
}

$manifest | ConvertTo-Json -Depth 5 |
    Set-Content -LiteralPath (Join-Path $root "release-manifest.json") -Encoding UTF8
Write-Host "release-manifest.json generated with $($files.Count) files."
