# Kraken Player - raw-zip Windows installer (the non-packaged path).
#
# Most customers use the single packaged installer KrakenPlayerSetup.exe. This
# script is the fallback for the plain .zip: it copies the player files into
# Program Files, then hands off to appliance-setup.ps1 (the shared system-config
# engine) to do the Defender exclusion, ffmpeg pre-place, current-user Scheduled
# Task, and fullscreen lockdown. It does not create a second Windows account.
#
# Run elevated (or double-click install-appliance.cmd, which self-elevates):
#   powershell -ExecutionPolicy Bypass -File .\install.ps1
#
# Advanced/legacy escape hatches pass straight through to the engine. The normal
# no-argument path is the same single-account installation as the packaged exe.
[CmdletBinding()]
param(
  [string]$KioskUser = "KrakenKiosk",
  [switch]$DedicatedKioskAccount,
  [switch]$NoKioskAccount,
  [switch]$EnableKioskShell,
  [switch]$SkipAutologon,
  [switch]$SkipDefenderExclusion,
  [switch]$NoReboot
)

$ErrorActionPreference = "Stop"

$principal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
if (-not $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
  Write-Error "Please run this installer elevated, or double-click install-appliance.cmd."
  exit 1
}

$installDir = Join-Path $env:ProgramFiles "KrakenPlayer"
$here       = Split-Path -Parent $MyInvocation.MyCommand.Path

Write-Host "==> Installing files to $installDir..."
New-Item -ItemType Directory -Force -Path $installDir | Out-Null

# Stop a running instance before replacing the binary.
Get-Process kraken-player -ErrorAction SilentlyContinue | Stop-Process -Force -ErrorAction SilentlyContinue
Start-Sleep -Seconds 1

# Copy the payload (binary + config template + bundled ffmpeg/Autologon/vision).
Copy-Item (Join-Path $here "kraken-player.exe") (Join-Path $installDir "kraken-player.exe") -Force
if (Test-Path (Join-Path $here "config.json")) { Copy-Item (Join-Path $here "config.json") (Join-Path $installDir "config.json") -Force }
foreach ($sub in @("bin", "tools", "vision")) {
  $src = Join-Path $here $sub
  if (Test-Path $src) { Copy-Item $src (Join-Path $installDir $sub) -Recurse -Force }
}

# Hand off to the shared system-config engine.
$engine = Join-Path $here "appliance-setup.ps1"
if (-not (Test-Path $engine)) { $engine = Join-Path $installDir "appliance-setup.ps1" }
if (-not (Test-Path $engine)) {
  Write-Error "appliance-setup.ps1 not found next to install.ps1."
  exit 1
}

$fwd = @{ InstallDir = $installDir; KioskUser = $KioskUser }
if (-not $DedicatedKioskAccount -or $NoKioskAccount) {
  $fwd.NoKioskAccount = $true
  $fwd.EnableKioskShell = $true
  $fwd.SkipAutologon = $true
  $fwd.NoReboot = $true
} else {
  if ($EnableKioskShell) { $fwd.EnableKioskShell = $true }
  if ($SkipAutologon)    { $fwd.SkipAutologon = $true }
}
if ($SkipDefenderExclusion) { $fwd.SkipDefenderExclusion = $true }
if ($NoReboot)              { $fwd.NoReboot = $true }

& $engine @fwd
exit $LASTEXITCODE
