# Portable fast-sim environment for customers.
# Sets FAST_SIM_ROOT to this toolkit's own directory and locates the required
# open-source runtimes (Icarus / Verilator / cocotb / Python) from PATH or
# common installs. It does NOT assume any developer-machine toolchain layout.

$Script:FastSimRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
$env:FAST_SIM_ROOT = $Script:FastSimRoot

# Locate a command on PATH (or common install dirs) and expose it as an env var.
function Get-FastSimTool([string]$name, [string]$varName, [string[]]$extraDirs) {
    $cmd = Get-Command $name -ErrorAction SilentlyContinue
    if ($cmd) {
        Set-Item -Path "env:$varName" -Value $cmd.Source
        return
    }
    foreach ($dir in $extraDirs) {
        $cand = Join-Path $dir $name
        if (Test-Path -LiteralPath $cand) {
            Set-Item -Path "env:$varName" -Value $cand
            return
        }
    }
    Remove-Item "env:$varName" -ErrorAction SilentlyContinue
}

# Python (from PATH or common installs).
$py = Get-Command python -ErrorAction SilentlyContinue
if ($py) {
    $env:PYTHON_EXECUTABLE = $py.Source
} else {
    Remove-Item env:PYTHON_EXECUTABLE -ErrorAction SilentlyContinue
}

# Optional C++ toolchain for Verilator C++ builds.
$make = Get-Command mingw32-make, make -ErrorAction SilentlyContinue | Select-Object -First 1
if ($make) {
    $env:MAKE = $make.Name
} else {
    Remove-Item env:MAKE -ErrorAction SilentlyContinue
}

# Icarus / Verilator / yosys are found on PATH by the check script; nothing is
# bundled. Print what is available for visibility.
Write-Host "FAST_SIM_ROOT=$env:FAST_SIM_ROOT"
if ($env:PYTHON_EXECUTABLE) { Write-Host "PYTHON_EXECUTABLE=$env:PYTHON_EXECUTABLE" }
if ($env:MAKE) { Write-Host "MAKE=$env:MAKE" }
