# check_target.ps1 - Checks if target binary needs to be rebuilt based on binary existence and source file modification times
param(
    [string]$TargetBinary,
    [string]$SearchDirs
)

if (-not (Test-Path $TargetBinary)) {
    Set-Content -Path "Intermediate\need_build.tmp" -Value "1"
    exit 0
}

$binTime = (Get-Item $TargetBinary).LastWriteTime
$dirs = $SearchDirs -split ','

$needsRebuild = "0"
foreach ($dir in $dirs) {
    if (Test-Path $dir) {
        # Check source .cpp, .h, .hpp, .inl files for changes newer than target binary
        $newer = Get-ChildItem -Path $dir -Recurse -File -ErrorAction SilentlyContinue | Where-Object { 
            ($_.Extension -eq '.cpp' -or $_.Extension -eq '.h' -or $_.Extension -eq '.hpp' -or $_.Extension -eq '.inl') -and $_.LastWriteTime -gt $binTime 
        }
        if ($newer) {
            $needsRebuild = "1"
            break
        }
    }
}

Set-Content -Path "Intermediate\need_build.tmp" -Value $needsRebuild
