<#
.SYNOPSIS
    Windows equivalent of build_core.sh - builds the Go core binaries.

.DESCRIPTION
    Cross-compiles the Android ARM64 client (dropped into android\lib\arm64-v8a\
    as libdragontcp_client.so) and the Linux AMD64/ARM64 servers.
    CGO is disabled, so no C toolchain or NDK is required.

.EXAMPLE
    .\build_core.ps1
    .\build_core.ps1 -ClientOnly
#>
[CmdletBinding()]
param(
    # Only build the Android client .so (skip the Linux servers).
    [switch]$ClientOnly,
    # Where to drop libdragontcp_client.so. Defaults to android\lib\arm64-v8a
    # under this script; build_apk.ps1 passes its own app directory explicitly,
    # which is what makes this work regardless of how the tree is nested.
    [string]$AndroidLibDir,
    # Path to the go executable.
    [string]$GoBin = $(if ($env:GO_BIN) { $env:GO_BIN } else { 'go' })
)

$ErrorActionPreference = 'Stop'
$Root = $PSScriptRoot
if (-not $AndroidLibDir) { $AndroidLibDir = Join-Path $Root 'android\lib\arm64-v8a' }

function Invoke-Go {
    param([hashtable]$Env, [string[]]$GoArgs)
    $saved = @{}
    foreach ($k in $Env.Keys) {
        $saved[$k] = [Environment]::GetEnvironmentVariable($k)
        [Environment]::SetEnvironmentVariable($k, $Env[$k])
    }
    try {
        & $GoBin @GoArgs
        if ($LASTEXITCODE -ne 0) { throw "go build failed (exit $LASTEXITCODE)" }
    } finally {
        foreach ($k in $saved.Keys) { [Environment]::SetEnvironmentVariable($k, $saved[$k]) }
    }
}

$go = Get-Command $GoBin -ErrorAction SilentlyContinue
if (-not $go) { throw "Go compiler not found. Install from https://go.dev/dl/ or set -GoBin." }

New-Item -ItemType Directory -Force -Path "$Root\bin", $AndroidLibDir | Out-Null
Push-Location "$Root\core"
try {
    $ldflags = '-ldflags=-s -w'

    Write-Host '[core] Android ARM64 client...' -ForegroundColor Cyan
    Invoke-Go @{ CGO_ENABLED = '0'; GOOS = 'android'; GOARCH = 'arm64' } `
        @('build', '-trimpath', $ldflags, '-o', (Join-Path $AndroidLibDir 'libdragontcp_client.so'), './cmd/dragontcp-client')

    if (-not $ClientOnly) {
        Write-Host '[core] Linux AMD64 client...' -ForegroundColor Cyan
        Invoke-Go @{ CGO_ENABLED = '0'; GOOS = 'linux'; GOARCH = 'amd64' } `
            @('build', '-trimpath', $ldflags, '-o', "$Root\bin\dragontcp-hybrid-client-linux-amd64", './cmd/dragontcp-client')

        Write-Host '[core] Linux AMD64 server...' -ForegroundColor Cyan
        Invoke-Go @{ CGO_ENABLED = '0'; GOOS = 'linux'; GOARCH = 'amd64' } `
            @('build', '-trimpath', $ldflags, '-o', "$Root\bin\dragontcp-hybrid-server-linux-amd64", './cmd/dragontcp-server')

        Write-Host '[core] Linux ARM64 server...' -ForegroundColor Cyan
        Invoke-Go @{ CGO_ENABLED = '0'; GOOS = 'linux'; GOARCH = 'arm64' } `
            @('build', '-trimpath', $ldflags, '-o', "$Root\bin\dragontcp-hybrid-server-linux-arm64", './cmd/dragontcp-server')
    }
} finally {
    Pop-Location
}

Write-Host 'Core build complete.' -ForegroundColor Green
