#Requires -RunAsAdministrator

[cmdletbinding(PositionalBinding = $false)]
param (
    [ValidateSet("Debug", "Release")][string]$BuildType = "Debug",
    [string]$BuildOutputPath = [string](Get-Location),
    [string]$PackageCertPath = $null,
    [parameter(ValueFromRemainingArguments = $true)]
    [string[]]$MsiArgs
)

$ErrorActionPreference = "Stop"

$processorArchitecture = (Get-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment').PROCESSOR_ARCHITECTURE
$Platform = switch -Wildcard ($processorArchitecture) {
    '*ARM64*' { 'arm64' }
    '*AMD64*' { 'X64' }
    default   { throw "Failed to determine system architecture: $processorArchitecture" }
}

$PackagePath = "$BuildOutputPath\bin\$Platform\$BuildType\wsl.msi"

# msiexec.exe doesn't like symlinks, so use the canonical path
$Target = (Get-ChildItem $PackagePath).Target
if ($Target)
{
    $PackagePath = $Target
}

Write-Host -ForegroundColor Green "Installing: $PackagePath "

$MSIArguments = @(
    "/i"
    $PackagePath
    "/qn"
    "/norestart"
)

if ($MsiArgs)
{
    $MSIArguments += $MsiArgs
}

$exitCode = (Start-Process -Wait "msiexec.exe" -ArgumentList $MSIArguments -NoNewWindow -PassThru).ExitCode
if ($exitCode -Ne 0)
{
    Write-Host "Failed to install package: $exitCode"
    exit 1
}

Write-Host -ForegroundColor Green "Package $PackagePath installed successfully"
