# Git symlinks on Windows are often checked out as one-line text files like "../src/foo.h".
# This script replaces such stubs with the real header contents.

param(
    [string]$Root = (Join-Path $PSScriptRoot "app\src\main\cpp\hev-socks5-tunnel")
)

$ErrorActionPreference = "Stop"

if (-not (Test-Path $Root)) {
    Write-Error "hev-socks5-tunnel not found: $Root"
}

$fixed = 0
$missing = 0

Get-ChildItem -Path $Root -Recurse -Filter *.h | ForEach-Object {
    $file = $_.FullName
    $lines = @(Get-Content $file -ErrorAction SilentlyContinue)
    if ($lines.Count -ne 1) {
        return
    }

    $line = $lines[0].Trim()
    if ($line -notmatch '^\.\./') {
        return
    }

    $target = [System.IO.Path]::GetFullPath((Join-Path $_.DirectoryName $line))
    if (-not (Test-Path $target)) {
        Write-Warning "Missing symlink target: $file -> $target"
        $script:missing++
        return
    }

    Copy-Item -Path $target -Destination $file -Force
    $script:fixed++
}

Write-Host "Fixed symlink stubs: $fixed"
if ($missing -gt 0) {
    Write-Warning "Missing targets: $missing"
    exit 1
}
