$ErrorActionPreference = "Stop"

$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path

$pluginName = "Pioneer MP Trigger.vst3"
$pluginSource = Join-Path (Join-Path $scriptDir "Plugins") $pluginName
$vst3Root = Join-Path $env:ProgramFiles "Common Files\VST3"
$pluginDest = Join-Path $vst3Root $pluginName

$appName = "Pioneer AD Sampler.exe"
$appSource = Join-Path (Join-Path $scriptDir "Standalone") $appName
$appRoot = Join-Path $env:ProgramFiles "Pioneer AD Sampler"
$appDest = Join-Path $appRoot $appName

function Test-IsAdministrator {
    $identity = [Security.Principal.WindowsIdentity]::GetCurrent()
    $principal = New-Object Security.Principal.WindowsPrincipal($identity)
    return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}

Write-Host "Pioneer Windows bundle installer"
Write-Host ""

if (-not (Test-Path -LiteralPath $pluginSource)) {
    throw "Cannot find $pluginName under the Plugins folder. Extract the whole ZIP archive first, then run the installer from the extracted folder."
}

if (-not (Test-Path -LiteralPath $appSource)) {
    throw "Cannot find $appName under the Standalone folder. Extract the whole ZIP archive first, then run the installer from the extracted folder."
}

if (-not (Test-IsAdministrator)) {
    Write-Host "Administrator rights are required to install into Program Files and Common Files."
    Write-Host "Requesting elevation..."
    Start-Process -FilePath "powershell.exe" -ArgumentList @("-NoProfile", "-ExecutionPolicy", "Bypass", "-File", $PSCommandPath) -Verb RunAs -Wait
    exit
}

New-Item -ItemType Directory -Force -Path $vst3Root | Out-Null
New-Item -ItemType Directory -Force -Path $appRoot | Out-Null

if (Test-Path -LiteralPath $pluginDest) {
    Write-Host "Replacing existing VST3 installation:"
    Write-Host "  $pluginDest"
    Remove-Item -LiteralPath $pluginDest -Recurse -Force
}

if (Test-Path -LiteralPath $appDest) {
    Write-Host "Replacing existing standalone installation:"
    Write-Host "  $appDest"
    Remove-Item -LiteralPath $appDest -Force
}

Copy-Item -LiteralPath $pluginSource -Destination $vst3Root -Recurse -Force
Copy-Item -LiteralPath $appSource -Destination $appRoot -Force

$shortcutTargets = @(
    (Join-Path ([Environment]::GetFolderPath("CommonPrograms")) "Pioneer AD Sampler\Pioneer AD Sampler.lnk"),
    (Join-Path ([Environment]::GetFolderPath("CommonDesktopDirectory")) "Pioneer AD Sampler.lnk")
)

$shell = New-Object -ComObject WScript.Shell
foreach ($shortcutPath in $shortcutTargets) {
    $shortcutDir = Split-Path -Parent $shortcutPath
    New-Item -ItemType Directory -Force -Path $shortcutDir | Out-Null

    $shortcut = $shell.CreateShortcut($shortcutPath)
    $shortcut.TargetPath = $appDest
    $shortcut.WorkingDirectory = $appRoot
    $shortcut.IconLocation = $appDest
    $shortcut.Save()
}

Write-Host ""
Write-Host "Installed successfully:"
Write-Host "  $pluginDest"
Write-Host "  $appDest"
Write-Host "Pioneer AD Sampler shortcuts were added to the Start Menu and Public Desktop."
Write-Host ""
Write-Host "Open your DAW and rescan VST3 plug-ins if Pioneer MP Trigger does not appear automatically."
