Param (
        [switch]$CompareWithCumulativeBuild
)

Clear-Host
$ReDMCSBPath = (Get-Item $PSScriptRoot).Parent.Parent.Parent.FullName

$Executables = Import-CSV -Delimiter "`t" -Path "$PSScriptRoot\Executables.txt"
$Executables = $Executables | Sort-Object -Property "Media","File Name"
$Executables = $Executables | Select-Object -Property *,@{ Name="ReferenceExecutablePath"; Expression={
        $ReferenceOriginalExecutablePath = "$ReDMCSBPath\Reference\Original\$($_.Media)\$($_.Name)"
        $ReferenceReDMCSBExecutablePath = "$ReDMCSBPath\Reference\ReDMCSB\$($_.Media)\$($_.Name)"
        if (Test-Path -Path $ReferenceReDMCSBExecutablePath -PathType Leaf) {
                $ReferenceExecutablePath = $ReferenceReDMCSBExecutablePath
        } else {
                $ReferenceExecutablePath = $ReferenceOriginalExecutablePath
        }
        $ReferenceExecutablePath
}},@{ Name="BuildExecutablePath"; Expression={
        "$ReDMCSBPath\Output\$($_.Toolchain)\HARDDISK\BUILD\$($_.Media)\$($_.Name)"
}},@{ Name="CumulativeBuildExecutablePath"; Expression={
        "$ReDMCSBPath\Output\Build\$($_.Media)\$($_.Name)"
}},@{ Name="Identifier"; Expression={
        "$($_.'Media')\$($_.Name)"
}}

$IdentifierWidth =  ($Executables.Media | Measure-Object -Maximum -Property Length).Maximum + 1 + ($Executables.Name | Measure-Object -Maximum -Property Length).Maximum
$ReferenceExecutablePathWidth = ($Executables.ReferenceExecutablePath | Measure-Object -Maximum -Property Length).Maximum

Write-Host (" Status   Executable" + (' ' * ($IdentifierWidth - 7)) + "Reference Executable" + (' ' * ($ReferenceExecutablePathWidth - 18)) + "Build Executable")
foreach ($Executable in $Executables) {
        if ($CompareWithCumulativeBuild) {
                $BuildExecutablePath = $Executable.CumulativeBuildExecutablePath
        } else {
                $BuildExecutablePath = $Executable.BuildExecutablePath
        }
        if ((Test-Path -Path $Executable.ReferenceExecutablePath -PathType Leaf) -and (Test-Path -Path $BuildExecutablePath -PathType Leaf)) {
                if ((Get-FileHash -Path $Executable.ReferenceExecutablePath -Algorithm SHA1).Hash -eq (Get-FileHash -Path $BuildExecutablePath -Algorithm SHA1).Hash) {
                        Write-Host "Identical" -NoNewline -ForegroundColor DarkGreen -BackgroundColor Black
                } else {
                        Write-Host "Different" -NoNewline -ForegroundColor Red -BackgroundColor Black
                }
        } else {
                Write-Host " Missing " -NoNewline -ForegroundColor Yellow -BackgroundColor Black
        }
        Write-Host (" [$($Executable.Identifier)] " + (' ' * ($IdentifierWidth - $Executable.Identifier.Length)) + "[$($Executable.ReferenceExecutablePath)]" + (' ' * ($ReferenceExecutablePathWidth - $Executable.ReferenceExecutablePath.Length)) + "[$BuildExecutablePath]")
}
