[CmdletBinding()]
param()

$ErrorActionPreference = 'Stop'

# Release-controlled setting. Update this build number and redistribute the MAR package when OA3Tool is updated.
$minimumOa3ToolBuild = 26100
$toolPaths = @(
    (Join-Path $PSScriptRoot 'Home11\OA3Tool\amd64\oa3tool.exe'),
    (Join-Path $PSScriptRoot 'Pro11\OA3Tool\amd64\oa3tool.exe'),
    (Join-Path $PSScriptRoot 'Cit11\OA3Tool\amd64\oa3tool.exe')
)

try
{
    foreach($toolPath in $toolPaths)
    {
        if(-not (Test-Path -LiteralPath $toolPath -PathType Leaf))
        {
            throw "Required OA3Tool executable was not found: $toolPath"
        }

        $versionInfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($toolPath)
        $toolBuild = $versionInfo.FileBuildPart
        if($toolBuild -le 0)
        {
            throw "Could not determine the OA3Tool build from: $toolPath"
        }

        Write-Host "OA3Tool: $toolPath"
        Write-Host "Detected version: $($versionInfo.FileVersion) (build $toolBuild); required minimum build: $minimumOa3ToolBuild"
        if($toolBuild -lt $minimumOa3ToolBuild)
        {
            throw ("OA3Tool build {0} is below the required build {1}: {2}" -f $toolBuild, $minimumOa3ToolBuild, $toolPath)
        }
    }

    Write-Host 'OA3Tool version requirement met for all Windows editions.' -ForegroundColor Green
    exit 0
}
catch
{
    Write-Host "OA3Tool version validation failed: $($_.Exception.Message)" -ForegroundColor Red
    exit 1
}
