# build_native_bridge.ps1 - Build daw_vst_bridge.exe (C++ Native Host Bridge)
# Run on Windows x64 with VS Build Tools 2019+ (C++ workload) installed.
# ASCII only (PowerShell 5.1 reads .ps1 without BOM as ANSI).
$ErrorActionPreference = "Stop"
# NOTE: no Set-Location here - this script must not change the caller's CWD
# (build_windows.ps1 calls it with & and then checks relative paths).
# All paths below are absolute (derived from $PSScriptRoot).
$REPO_ROOT = Resolve-Path (Join-Path $PSScriptRoot "..\..")   # ...\SonicForgeStudio
$NB_DIR = Join-Path $REPO_ROOT "native_bridge"
$VS_GEN = "Visual Studio 16 2019"   # VS2019 Build Tools on this machine
$VS_ARCH = "x64"

# 1. vcpkg (bootstrap once; skip if already present)
$VCPKG_ROOT = Join-Path $env:USERPROFILE "vcpkg"
if (-not (Test-Path (Join-Path $VCPKG_ROOT "vcpkg.exe"))) {
  Write-Host "== [1/7] Bootstrap vcpkg =="
  git clone https://github.com/microsoft/vcpkg $VCPKG_ROOT
  & (Join-Path $VCPKG_ROOT "bootstrap-vcpkg.bat") -disableMetrics
}

# 2. fluidsynth via vcpkg manifest (native_bridge/vcpkg.json; pkgconf included)
Write-Host "== [2/7] vcpkg install fluidsynth (manifest) =="
Push-Location $NB_DIR
& (Join-Path $VCPKG_ROOT "vcpkg.exe") install --triplet x64-windows
if ($LASTEXITCODE -ne 0) { Pop-Location; Write-Host "ERROR: vcpkg install failed" -ForegroundColor Red; exit 1 }
Pop-Location

# 3. sfizz 1.2.3 static lib (sfizz has NO vcpkg port: clone + build once)
Write-Host "== [3/7] sfizz 1.2.3 (clone + build static /MD, skip if built) =="
$SFIZZ_DIR = Join-Path $NB_DIR "sfizz-src"
$SFIZZ_BUILD = Join-Path $SFIZZ_DIR "build"
$SFIZZ_STATIC = Join-Path $SFIZZ_BUILD "library\lib\Release\sfizz_static.lib"
if (-not (Test-Path $SFIZZ_STATIC)) {
  if (-not (Test-Path (Join-Path $SFIZZ_DIR "CMakeLists.txt"))) {
    git clone --branch 1.2.3 --depth 1 https://github.com/sfztools/sfizz.git $SFIZZ_DIR
    if ($LASTEXITCODE -ne 0) { Write-Host "ERROR: sfizz clone failed" -ForegroundColor Red; exit 1 }
  }
  Push-Location $SFIZZ_DIR
  git submodule update --init --recursive
  if ($LASTEXITCODE -ne 0) { Pop-Location; Write-Host "ERROR: sfizz submodules failed" -ForegroundColor Red; exit 1 }
  Pop-Location
  # Patch: sfizz forces /MT (static CRT) in SfizzConfig.cmake; vcpkg fluidsynth
  # is /MD. Replace the forced line (idempotent; no-op if already patched).
  $cfg = Join-Path $SFIZZ_DIR "cmake\SfizzConfig.cmake"
  $cfgText = [System.IO.File]::ReadAllText($cfg)
  $patched = $cfgText -replace '(?m)^\s*set\(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded\$<\$<CONFIG:Debug>:Debug>"\)\s*$', '# patched for local Windows build: caller picks runtime (/MD) to match vcpkg'
  if ($patched -ne $cfgText) { [System.IO.File]::WriteAllText($cfg, $patched) }
  # Configure + build static lib with /MD to match fluidsynth
  cmake -S $SFIZZ_DIR -B $SFIZZ_BUILD -G $VS_GEN -A $VS_ARCH `
    -DSFIZZ_SHARED=OFF -DSFIZZ_RENDER=OFF -DSFIZZ_TESTS=OFF -DSFIZZ_DEMOS=OFF `
    -DSFIZZ_DEVTOOLS=OFF -DSFIZZ_BENCHMARKS=OFF -DSFIZZ_JACK=OFF `
    -DSFIZZ_USE_SNDFILE=OFF -DSFIZZ_USE_SYSTEM_ABSEIL=OFF `
    -DSFIZZ_USE_SYSTEM_KISS_FFT=OFF -DSFIZZ_USE_SYSTEM_PUGIXML=OFF `
    -DSFIZZ_USE_SYSTEM_CATCH=OFF -DSFIZZ_USE_SYSTEM_CXXOPTS=OFF `
    -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=OFF `
    -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreadedDLL
  if ($LASTEXITCODE -ne 0) { Write-Host "ERROR: sfizz cmake configure failed" -ForegroundColor Red; exit 1 }
  cmake --build $SFIZZ_BUILD --config Release --target sfizz_static -- /m
  if ($LASTEXITCODE -ne 0) { Write-Host "ERROR: sfizz build failed" -ForegroundColor Red; exit 1 }
} else {
  Write-Host "  sfizz_static.lib already present - skip"
}

# 4. VST3 SDK submodule (Steinberg; NOT in vcpkg; already registered in repo)
Write-Host "== [4/7] VST3 SDK (submodule) =="
if (-not (Test-Path (Join-Path $NB_DIR "vst3sdk"))) {
  Push-Location $REPO_ROOT
  git submodule update --init --recursive -- native_bridge/vst3sdk
  if ($LASTEXITCODE -ne 0) { Pop-Location; Write-Host "ERROR: vst3sdk submodule failed" -ForegroundColor Red; exit 1 }
  Pop-Location
}

# 5. Configure + build with CMake + vcpkg toolchain (MSVC, bridge target only)
Write-Host "== [5/7] CMake configure + build (target daw_vst_bridge) =="
$BUILD_DIR = Join-Path $NB_DIR "build"
New-Item -ItemType Directory -Force $BUILD_DIR | Out-Null
cmake -S $NB_DIR -B $BUILD_DIR -G $VS_GEN -A $VS_ARCH `
  -DCMAKE_TOOLCHAIN_FILE=(Join-Path $VCPKG_ROOT "scripts\buildsystems\vcpkg.cmake") `
  -DCMAKE_BUILD_TYPE=Release `
  -DVCPKG_TARGET_TRIPLET=x64-windows
if ($LASTEXITCODE -ne 0) { Write-Host "ERROR: cmake configure failed" -ForegroundColor Red; exit 1 }
cmake --build $BUILD_DIR --config Release --target daw_vst_bridge -- /m
if ($LASTEXITCODE -ne 0) { Write-Host "ERROR: cmake build failed" -ForegroundColor Red; exit 1 }

# 6. Copy sidecar to Tauri externalBin location
Write-Host "== [6/7] Copy to src-tauri\binaries =="
$BIN_DIR = Join-Path $REPO_ROOT "src-tauri\binaries"
New-Item -ItemType Directory -Force $BIN_DIR | Out-Null
$EXE = Join-Path $BUILD_DIR "Release\daw_vst_bridge.exe"
if (-not (Test-Path $EXE)) { Write-Host "ERROR: $EXE not found" -ForegroundColor Red; exit 1 }
Copy-Item $EXE (Join-Path $BIN_DIR "daw_vst_bridge-x86_64-pc-windows-msvc.exe") -Force

# 7. Copy fluidsynth runtime DLL next to sidecar (Tauri externalBin bundles only the exe)
Write-Host "== [7/7] Copy libfluidsynth-3.dll =="
$FS_DLL = Join-Path $BUILD_DIR "vcpkg_installed\x64-windows\bin\libfluidsynth-3.dll"
if (-not (Test-Path $FS_DLL)) { $FS_DLL = Join-Path $VCPKG_ROOT "installed\x64-windows\bin\libfluidsynth-3.dll" }
if (Test-Path $FS_DLL) {
  Copy-Item $FS_DLL (Join-Path $BIN_DIR "libfluidsynth-3.dll") -Force
  Copy-Item $FS_DLL (Join-Path $BUILD_DIR "Release\libfluidsynth-3.dll") -Force
  Write-Host "  libfluidsynth-3.dll copied to src-tauri\binaries"
} else {
  Write-Host "WARN: libfluidsynth-3.dll not found - bridge needs it at runtime" -ForegroundColor Yellow
}
Write-Host "== DONE: sidecar + DLL in src-tauri\binaries =="
