# ncp - PowerShell entry point. The CLI itself is management/ncp.py, the same
# file the Linux shell wrapper runs; this only picks an interpreter and forwards
# the arguments.
#
# On Windows, ncp comes from management/ncp.cmd on the machine PATH instead (a
# .cmd is not subject to the execution policy and works outside PowerShell too) -
# install-windows.ps1 sets that up. This file is what gives ncp to pwsh on Linux,
# where restore-linux.sh registers it in $PROFILE.CurrentUserAllHosts as:
#
#     function ncp { & "<repo>/management/ncp.ps1" @args }
#
# ASCII only, no exceptions: PowerShell 5.1 decodes BOM-less files as ANSI, so a
# single Unicode dash eats a quote and the script dies at parse time. A test in
# tests/test_smoke.py fails if any .ps1 in this repo gains a non-ASCII byte.

$Root = Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Path)

# Forward slashes and per-platform venv layout: backslash is a literal character
# on Linux, not a separator, so "Promethean\Scripts\python.exe" would resolve to
# a single nonsense filename under pwsh on Linux. Windows accepts "/" happily.
# $env:OS is the 5.1-safe check; the automatic $IsWindows only exists on PS 6+.
$OnWindows = ($env:OS -eq "Windows_NT")
if ($OnWindows) {
    $Py = Join-Path $Root "Promethean/Scripts/python.exe"
} else {
    $Py = Join-Path $Root "Promethean/bin/python3"
}

# Fall back to a system Python so backup/restore still work before the venv is
# built - those delegate to the stdlib-only bin/sync.py.
if (-not (Test-Path $Py)) {
    foreach ($candidate in @("python3", "python")) {
        $found = Get-Command $candidate -ErrorAction SilentlyContinue
        if ($found) { $Py = $found.Source; break }
    }
}
if (-not (Test-Path $Py)) {
    Write-Error "No Python found. Run install.sh (Linux) or install-windows.ps1 first."
    exit 1
}

& $Py (Join-Path $Root "management/ncp.py") @args
exit $LASTEXITCODE
