# Iverilog 文件自动复制脚本
# 用于将已安装的 iverilog 复制到插件目录

param(
    [string]$IverilogPath = "C:\iverilog"
)

# 设置插件工具路径
$PluginPath = Split-Path -Parent $PSScriptRoot
$ToolsPath = Join-Path $PSScriptRoot ""

Write-Host "=====================================" -ForegroundColor Cyan
Write-Host "Iverilog 文件复制脚本" -ForegroundColor Cyan
Write-Host "=====================================" -ForegroundColor Cyan
Write-Host ""

# 检查源路径是否存在
if (-not (Test-Path $IverilogPath)) {
    Write-Host "错误: 找不到 iverilog 安装目录: $IverilogPath" -ForegroundColor Red
    Write-Host ""
    Write-Host "请指定正确的 iverilog 安装路径，例如：" -ForegroundColor Yellow
    Write-Host "  .\copy-iverilog.ps1 -IverilogPath 'C:\Program Files\iverilog'" -ForegroundColor Yellow
    Write-Host ""

    # 尝试查找常见安装位置
    $CommonPaths = @(
        "C:\iverilog",
        "C:\Program Files\iverilog",
        "C:\Program Files (x86)\iverilog",
        "$env:LOCALAPPDATA\iverilog"
    )

    Write-Host "正在搜索常见安装位置..." -ForegroundColor Yellow
    foreach ($path in $CommonPaths) {
        if (Test-Path $path) {
            Write-Host "找到: $path" -ForegroundColor Green
            $IverilogPath = $path
            break
        }
    }

    if (-not (Test-Path $IverilogPath)) {
        Write-Host "未找到 iverilog 安装目录" -ForegroundColor Red
        exit 1
    }
}

Write-Host "源路径: $IverilogPath" -ForegroundColor Green
Write-Host "目标路径: $ToolsPath" -ForegroundColor Green
Write-Host ""

# 创建目标目录
Write-Host "创建目录结构..." -ForegroundColor Cyan
New-Item -ItemType Directory -Force -Path "$ToolsPath\bin" | Out-Null
New-Item -ItemType Directory -Force -Path "$ToolsPath\lib" | Out-Null

# 复制可执行文件
Write-Host "复制可执行文件..." -ForegroundColor Cyan

$executables = @("iverilog.exe", "vvp.exe")
foreach ($exe in $executables) {
    $sourcePath = Join-Path "$IverilogPath\bin" $exe
    if (Test-Path $sourcePath) {
        Copy-Item $sourcePath "$ToolsPath\bin\" -Force
        Write-Host "  ✓ $exe" -ForegroundColor Green
    } else {
        Write-Host "  ✗ 未找到 $exe" -ForegroundColor Red
    }
}

# 复制 DLL 文件
Write-Host "复制 DLL 文件..." -ForegroundColor Cyan

$dlls = Get-ChildItem "$IverilogPath\bin\*.dll" -ErrorAction SilentlyContinue
if ($dlls) {
    foreach ($dll in $dlls) {
        Copy-Item $dll.FullName "$ToolsPath\bin\" -Force
        Write-Host "  ✓ $($dll.Name)" -ForegroundColor Green
    }
} else {
    Write-Host "  ! 未找到 DLL 文件" -ForegroundColor Yellow
}

# 复制库文件
Write-Host "复制库文件..." -ForegroundColor Cyan

$libPath = Join-Path $IverilogPath "lib"
if (Test-Path $libPath) {
    Copy-Item "$libPath\*" "$ToolsPath\lib\" -Recurse -Force

    # 统计复制的文件
    $vpiFiles = Get-ChildItem "$ToolsPath\lib\ivl\*.vpi" -ErrorAction SilentlyContinue
    $vplFiles = Get-ChildItem "$ToolsPath\lib\ivl\*.vpl" -ErrorAction SilentlyContinue

    Write-Host "  ✓ 复制了 $($vpiFiles.Count) 个 .vpi 文件" -ForegroundColor Green
    Write-Host "  ✓ 复制了 $($vplFiles.Count) 个 .vpl 文件" -ForegroundColor Green
} else {
    Write-Host "  ✗ 未找到 lib 目录" -ForegroundColor Red
}

Write-Host ""
Write-Host "=====================================" -ForegroundColor Cyan
Write-Host "验证安装..." -ForegroundColor Cyan
Write-Host "=====================================" -ForegroundColor Cyan

# 验证 iverilog
$iverilogExe = Join-Path "$ToolsPath\bin" "iverilog.exe"
if (Test-Path $iverilogExe) {
    Write-Host ""
    Write-Host "运行 iverilog -V:" -ForegroundColor Yellow
    & $iverilogExe -V
    Write-Host ""
} else {
    Write-Host "错误: iverilog.exe 未找到" -ForegroundColor Red
}

# 显示文件大小统计
Write-Host "=====================================" -ForegroundColor Cyan
Write-Host "文件大小统计" -ForegroundColor Cyan
Write-Host "=====================================" -ForegroundColor Cyan

$binSize = (Get-ChildItem "$ToolsPath\bin" -Recurse -File | Measure-Object -Property Length -Sum).Sum
$libSize = (Get-ChildItem "$ToolsPath\lib" -Recurse -File -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum).Sum
$totalSize = $binSize + $libSize

Write-Host "bin/ 目录: $([math]::Round($binSize / 1MB, 2)) MB" -ForegroundColor Green
Write-Host "lib/ 目录: $([math]::Round($libSize / 1MB, 2)) MB" -ForegroundColor Green
Write-Host "总计: $([math]::Round($totalSize / 1MB, 2)) MB" -ForegroundColor Cyan

Write-Host ""
Write-Host "=====================================" -ForegroundColor Cyan
Write-Host "复制完成！" -ForegroundColor Green
Write-Host "=====================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "现在可以使用插件的 VCD 生成功能了。" -ForegroundColor Green
Write-Host ""
