# 运行 rebuild-verify-master.vb (NX 2506 批处理): 重建含实体母模 + 验证导出
# 用法: powershell -NoProfile -ExecutionPolicy Bypass -File run-rebuild-verify.ps1
$ErrorActionPreference = 'Continue'

$nx = 'C:\Program Files\Siemens\NX2506\NXBIN\run_journal.exe'
$journal = 'd:\开发\OnebotCatalog\sample-data\tools\rebuild-verify-master.vb'
$workDir = 'd:\开发\OnebotCatalog\catalog-masters\KC'

if (-not (Test-Path $nx)) { Write-Output "[FAIL] NX run_journal 不存在: $nx"; exit 1 }
if (-not (Test-Path $journal)) { Write-Output "[FAIL] journal 不存在: $journal"; exit 1 }
if (-not (Test-Path $workDir)) { Write-Output "[FAIL] 工作目录不存在: $workDir"; exit 1 }

# 清理旧结果
Remove-Item (Join-Path $workDir 'rebuild-verify-master.txt') -ErrorAction SilentlyContinue
Remove-Item (Join-Path $workDir 'dbg_out\*') -ErrorAction SilentlyContinue

Write-Output "[RUN] run_journal 启动 (预计 30-90 秒, NX 会话冷启动)..."
$sw = [System.Diagnostics.Stopwatch]::StartNew()
$p = Start-Process -FilePath $nx -ArgumentList $journal -WorkingDirectory $workDir -PassThru -Wait
$sw.Stop()

Write-Output "[RUN] run_journal 退出, 用时 $([int]$sw.Elapsed.TotalSeconds) 秒"

$log = Join-Path $workDir 'rebuild-verify-master.txt'
if (Test-Path $log) {
    Write-Output "========== rebuild-verify-master.txt =========="
    Get-Content $log -Encoding UTF8
    Write-Output "==============================================="
} else {
    Write-Output "[FAIL] 未生成结果日志: $log (NX 可能启动失败或被 license 占用)"
}

Write-Output "========== dbg_out 产物 =========="
Get-ChildItem (Join-Path $workDir 'dbg_out') -ErrorAction SilentlyContinue | Select-Object Name, Length | Format-Table -AutoSize
