param([string]$Destination = (Join-Path $env:LOCALAPPDATA 'SSHMCP\releases\5.1.1'))
$ErrorActionPreference = 'Stop'
if (-not [System.IO.Path]::IsPathRooted($Destination)) { throw 'Destination must be absolute.' }
$packageDirectory = $PSScriptRoot
foreach ($line in Get-Content -LiteralPath (Join-Path $packageDirectory 'SHA256SUMS')) {
    $expected, $name = $line -split '  ', 2
    if ($name -notin @('mcp-ssh-manager.exe', 'sshmcp-file-agent', 'Install.ps1')) { throw 'Unexpected manifest entry.' }
    $actual = (Get-FileHash -LiteralPath (Join-Path $packageDirectory $name) -Algorithm SHA256).Hash.ToLowerInvariant()
    if ($actual -ne $expected) { throw "Checksum mismatch: $name" }
}
New-Item -ItemType Directory -Path $Destination -Force | Out-Null
# Keep immutable versioned files so an update never replaces a client's open EXE.
foreach ($name in @('mcp-ssh-manager.exe', 'sshmcp-file-agent')) {
    $source = Join-Path $packageDirectory $name
    $target = Join-Path $Destination $name
    if (Test-Path -LiteralPath $target) {
        if ((Get-FileHash -LiteralPath $source).Hash -ne (Get-FileHash -LiteralPath $target).Hash) { throw "Existing file differs: $target" }
    } else { Copy-Item -LiteralPath $source -Destination $target }
}
$binary = Join-Path $Destination 'mcp-ssh-manager.exe'
$version = & $binary --version
if ($LASTEXITCODE -ne 0) { throw 'Installed executable did not start.' }
@{ success = $true; version = $version; executable = $binary; helper = (Join-Path $Destination 'sshmcp-file-agent'); client_configuration = 'Follow docs/INSTALL.md to merge the MCP entry.' } | ConvertTo-Json
