#Requires -Modules 'powershell-yaml'
[CmdletBinding()]
Param(
    [Parameter(Mandatory)]
    [ValidateScript({
            If (Test-Path($_)) {
                $True
            } Else {
                Throw "'$_' is not a valid filename (within working directory '$PWD'), or access denied; aborting."
            }
        })]
    [string]$ManifestFileName
)

$GetItemSplat = @{
    Path = $ManifestFileName
}
$ManifestFile = Get-Item @GetItemSplat

$SetLocationSplat = @{
    Path     = $ManifestFile.DirectoryName
}
Set-Location @SetLocationSplat

$GetContentSplat = @{
    Path = $ManifestFile.FullName
}
$Manifest = Get-Content @GetContentSplat

$UpdatedManifest = ForEach ($Line in $Manifest) {
    Write-Host "Processing '$($Line)' ..."
    If ($Line -match '^SHA256\((.+)\)= ([0-9a-fA-F]{64})$') {
        If (Test-Path $Matches[1]) {
            $GetFileHashSplat = @{
                Path      = $Matches[1]
                Algorithm = 'SHA256'
            }
            Write-Host "Updating checksum..."
            "SHA256($($Matches[1]))= $((Get-FileHash @GetFileHashSplat).Hash)"
        }
    }
}

If ($UpdatedManifest -ne $Null) {
    $SetContentSplat = @{
        Path        = $ManifestFile.FullName
        Value       = $UpdatedManifest
        Force       = $True
        Confirm     = $False
    }
    Set-Content @SetContentSplat    
} Else {
    Write-Host "Failed updating manifest."
    Exit 1
}
