param(
    [switch]$Windows,

    [switch]$Linux,

    [string[]]$LinuxRuntimes = @("linux-x64", "linux-arm64"),

    [switch]$LinuxSelfContained,

    [string]$Configuration = "Release",

    [switch]$SkipWindowsInnoSetup,

    [switch]$SkipWindowsVersionBump
)

$ErrorActionPreference = "Stop"

$ScriptPath = $PSScriptRoot

$publishWindowsScript = Join-Path $ScriptPath "publish-windows.ps1"
$publishLinuxScript = Join-Path $ScriptPath "publish-linux.ps1"

$shouldPublishWindows = $Windows.IsPresent
$shouldPublishLinux = $Linux.IsPresent
if (!$shouldPublishWindows -and !$shouldPublishLinux) {
    $shouldPublishWindows = $true
    $shouldPublishLinux = $true
}

if ($shouldPublishWindows) {
    if (!(Test-Path $publishWindowsScript)) {
        Write-Error "publish-windows.ps1 not found: $publishWindowsScript"
        exit 1
    }

    & $publishWindowsScript `
        -Configuration $Configuration `
        -SkipInnoSetup:$SkipWindowsInnoSetup `
        -SkipVersionBump:$SkipWindowsVersionBump

    if ($LASTEXITCODE -ne 0) {
        exit $LASTEXITCODE
    }
}

if ($shouldPublishLinux) {
    if (!(Test-Path $publishLinuxScript)) {
        Write-Error "publish-linux.ps1 not found: $publishLinuxScript"
        exit 1
    }

    foreach ($rid in $LinuxRuntimes) {
        & $publishLinuxScript `
            -RuntimeIdentifier $rid `
            -SelfContained:$LinuxSelfContained `
            -Configuration $Configuration

        if ($LASTEXITCODE -ne 0) {
            exit $LASTEXITCODE
        }
    }
}
