<#
.SYNOPSIS
  Install Pomar Rack's PROTOTYPE NKS content packs on Windows (RACK-103 / RACK-101).

.DESCRIPTION
  PowerShell port of scripts/install_prototype_content_packs.sh. topo-refresh only scans the standard NKS
  roots, not the repo/ZIP, so this copies the bundled prototype packs (content-packs/prototype/.../extracted/)
  into the user's NKS user-content root (%USERPROFILE%\Documents\Native Instruments) - a topo-refresh
  discovery root - so a Legion beta tester gets the bundled presets after ONE step + a refresh.

  Additive by default (never overwrites existing files); -Force overwrites. Never touches app/libs source.
  Official/vendor NKS already installed always wins later (these packs are FALLBACK only).

.EXAMPLE
  .\Install-ContentPacks.ps1
  .\Install-ContentPacks.ps1 -Refresh "$env:USERPROFILE\TopoLibrary"
  .\Install-ContentPacks.ps1 -Force
#>
[CmdletBinding()]
param(
  [switch] $Force,                          # overwrite existing files (default: skip existing)
  [string] $Refresh = "",                   # after install, run topo-refresh against this TopoLibrary root
  [string] $PacksDir = "",                  # override the packs source dir
  [string] $RefreshExe = "",                # override the topo-refresh binary
  [string] $NksRoot = $env:TOPO_NKS_ROOT    # override the NKS root (default: %USERPROFILE%\Documents\Native Instruments)
)
$ErrorActionPreference = "Stop"

# Resolve the packs dir: explicit -PacksDir, else next to this script (ZIP layout), else ..\content-packs (repo).
if (-not $PacksDir) {
  $PacksDir = @("$PSScriptRoot\content-packs\prototype", "$PSScriptRoot\..\content-packs\prototype") |
              Where-Object { Test-Path $_ } | Select-Object -First 1
}
if (-not $PacksDir -or -not (Test-Path $PacksDir)) {
  Write-Error "No prototype content packs found (looked next to the script and ..\content-packs\prototype). Pass -PacksDir."
  exit 1
}
if (-not $NksRoot) { $NksRoot = Join-Path $env:USERPROFILE "Documents\Native Instruments" }

New-Item -ItemType Directory -Force -Path $NksRoot | Out-Null
Write-Host "Prototype content-pack install"
Write-Host "  from: $PacksDir"
Write-Host "  into: $NksRoot   (NKS user-content discovery root)"
Write-Host ("  mode: " + $(if ($Force) { "-Force (overwrite existing)" } else { "additive (skip existing files)" }))
Write-Host ""

# Each pack ships an extracted/ tree mirroring an NKS install. robocopy = the Windows rsync; /E recurses,
# and (additive) /XC /XN /XO exclude already-present files so only NEW files copy. robocopy success codes
# are 0..7 (8+ = real error), so we must not let its nonzero "files copied" code fail the script.
$packs = 0
Get-ChildItem -Path $PacksDir -Recurse -Directory -Filter "extracted" | ForEach-Object {
  $ext  = $_.FullName
  $pack = Split-Path (Split-Path $ext -Parent) -Leaf
  Write-Host "* pack: $pack"
  $opts = @("/E", "/NFL", "/NDL", "/NJH", "/NJS", "/NP")
  if (-not $Force) { $opts += @("/XC", "/XN", "/XO") }
  & robocopy $ext $NksRoot @opts | Out-Null
  if ($LASTEXITCODE -ge 8) { Write-Warning "robocopy reported errors for '$pack' (code $LASTEXITCODE)" }
  $packs++
}
$global:LASTEXITCODE = 0

Write-Host ""
Write-Host "Done: $packs pack(s) installed into $NksRoot."

# Resolve topo-refresh: -RefreshExe, else next to the script (ZIP), else the repo build path, else PATH.
if (-not $RefreshExe) {
  $RefreshExe = @("$PSScriptRoot\topo-refresh.exe",
                  "$PSScriptRoot\..\build\app\refresh-library\topo-refresh_artefacts\RelWithDebInfo\topo-refresh.exe") |
                Where-Object { Test-Path $_ } | Select-Object -First 1
  if (-not $RefreshExe) { $RefreshExe = "topo-refresh" } # fall back to PATH
}

if ($Refresh) {
  $lib = $Refresh -replace '^~', $env:USERPROFILE
  Write-Host "Running topo-refresh on $lib ..."
  & $RefreshExe $lib
} else {
  $defaultLib = Join-Path $env:USERPROFILE "TopoLibrary"
  Write-Host "Next: run the refresh, e.g.   .\topo-refresh.exe `"$defaultLib`""
  Write-Host "Then launch Pomar Rack.exe. The packs are now visible to NKS hosts (Komplete Kontrol) too."
}
