# Download Node.js 20 Linux x64 tarball for WSL offline install (when WSL curl/DNS fails).
$ErrorActionPreference = "Stop"
$Version = "v20.19.2"
$Arch = "x64"
$Root = Split-Path -Parent $PSScriptRoot
$CacheDir = Join-Path $Root ".cache"
$Out = Join-Path $CacheDir "node-${Version}-linux-${Arch}.tar.xz"
$Urls = @(
    "https://npmmirror.com/mirrors/node/${Version}/node-${Version}-linux-${Arch}.tar.xz",
    "https://nodejs.org/dist/${Version}/node-${Version}-linux-${Arch}.tar.xz"
)

New-Item -ItemType Directory -Force -Path $CacheDir | Out-Null
foreach ($url in $Urls) {
    Write-Host "Downloading $url ..."
    try {
        curl.exe --noproxy "*" -fsSL --connect-timeout 30 -o $Out $url
        if ((Get-Item $Out).Length -gt 1000000) {
            Write-Host "OK: $Out ($((Get-Item $Out).Length) bytes)"
            Write-Host "Run in WSL: bash scripts/wsl_install_node20.sh"
            exit 0
        }
    } catch {
        Write-Host "Failed: $_"
    }
}
Write-Host "ERROR: download failed" -ForegroundColor Red
exit 1
