# Merotools Manager - Windows installer (EXPERIMENTAL, unsigned).
# Run from inside the unpacked MerotoolsManager folder:
#     powershell -ExecutionPolicy Bypass -File .\install.ps1
#
#   -AllUsers   install to Program Files (needs an elevated prompt)
#   -Service    also register a logon Scheduled Task (autostart at sign-in)
#
# The background task is OPT-IN, matching the macOS/Linux builds. Note: this
# build has not yet been validated on a real Windows machine and is not
# code-signed, so SmartScreen will warn until an Authenticode cert is added.

param(
  [switch]$AllUsers,
  [switch]$Service
)
$ErrorActionPreference = "Stop"
$src = $PSScriptRoot

if ($AllUsers) {
  $installRoot = Join-Path (Join-Path $env:ProgramFiles "Merotools") "Manager"
} else {
  $installRoot = Join-Path (Join-Path $env:LOCALAPPDATA "Merotools") "Manager"
}
$bin = Join-Path $installRoot "merotools-manager.exe"

Write-Host "merotools: installing -> $installRoot"
New-Item -ItemType Directory -Force -Path $installRoot | Out-Null
# The staged tree is already the final layout (exe + runtime\ + bundle\); copy
# each top-level item verbatim.
Copy-Item (Join-Path $src "merotools-manager.exe") $bin -Force
foreach ($d in @("runtime","bundle")) {
  $dst = Join-Path $installRoot $d
  if (Test-Path $dst) { Remove-Item -Recurse -Force $dst }
  Copy-Item (Join-Path $src $d) $dst -Recurse -Force
}

if ($Service) {
  $action  = New-ScheduledTaskAction -Execute $bin
  $trigger = New-ScheduledTaskTrigger -AtLogOn
  $set     = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries
  Register-ScheduledTask -TaskName "MerotoolsManager" -Action $action -Trigger $trigger `
    -Settings $set -Description "Merotools Manager" -Force | Out-Null
  Write-Host "merotools: autostart registered (logon Scheduled Task 'MerotoolsManager')"
} else {
  Write-Host "merotools: installed (manual launch). Run: `"$bin`""
  Write-Host "merotools: to autostart later, re-run with -Service"
}
