param(
    [string] $LibreBibleDataRoot = (Resolve-Path "$PSScriptRoot\..\..\..\libre-bible-data").Path,
    [string] $SiteRoot = (Resolve-Path "$PSScriptRoot\..").Path
)

$sourceCatalogRoot = Join-Path $LibreBibleDataRoot "packages\json"
$targetCatalogRoot = Join-Path $SiteRoot "librebible\catalog"

if (-not (Test-Path (Join-Path $sourceCatalogRoot "catalog.json"))) {
    throw "LibreBible catalog not found at $sourceCatalogRoot"
}

New-Item -ItemType Directory -Force -Path $targetCatalogRoot | Out-Null
Copy-Item -LiteralPath (Join-Path $sourceCatalogRoot "catalog.json") -Destination (Join-Path $targetCatalogRoot "catalog.json") -Force

Get-ChildItem -LiteralPath $sourceCatalogRoot -Directory | ForEach-Object {
    $resourceCatalog = Join-Path $_.FullName "catalog.json"
    if (Test-Path $resourceCatalog) {
        $targetResourceDir = Join-Path $targetCatalogRoot $_.Name
        New-Item -ItemType Directory -Force -Path $targetResourceDir | Out-Null
        Copy-Item -LiteralPath $resourceCatalog -Destination (Join-Path $targetResourceDir "catalog.json") -Force
    }
}

Write-Host "Synced LibreBible catalog metadata to $targetCatalogRoot"
