param(
    [string]$InstallDirectory = (Split-Path -Path $PSScriptRoot -Parent)
)

Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"

# Per-user logon persistence for the agent (owner-approved 2026-07-28 after
# reboots silently killed the agent on two machines in one day). HKCU Run key:
# no elevation, no service, removable per user, survives agent self-update
# swaps because it points at the stable scripts path.
$startScript = Join-Path $InstallDirectory "scripts\Start-Agent.ps1"
if (-not (Test-Path -Path $startScript)) {
    throw "Start-Agent.ps1 was not found under '$InstallDirectory\scripts'."
}

$runKeyPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Run"
$command = "powershell.exe -NoProfile -WindowStyle Hidden -ExecutionPolicy Bypass -File `"$startScript`" -IgnoreAlreadyRunning"
Set-ItemProperty -Path $runKeyPath -Name "ImageAnywhereAgent" -Value $command
Write-Host "Registered ImageAnywhereAgent logon startup for this user."
Write-Host "Command: $command"
