# Registers the `outlookaddin://` URL protocol in HKCU so that
# clicking a link like `outlookaddin://compose?caseId=…` launches the
# OutlookAddinProtocolHandler.exe shim, which forwards the request over
# a named pipe to the running VSTO add-in.
#
# Usage:
#   .\install-protocol.ps1 -ExePath "C:\Path\To\OutlookAddinProtocolHandler.exe"
#
# Or, to derive the path automatically from a ClickOnce install:
#   .\install-protocol.ps1

[CmdletBinding()]
param(
    [string]$ExePath
)

$ErrorActionPreference = 'Stop'

if (-not $ExePath) {
    $candidate = Join-Path $env:LOCALAPPDATA "Apps\OutlookAddinProtocolHandler\OutlookAddinProtocolHandler.exe"
    if (Test-Path $candidate) {
        $ExePath = $candidate
    } else {
        throw "ExePath not supplied and default location not found: $candidate"
    }
}

if (-not (Test-Path $ExePath)) {
    throw "Protocol handler not found: $ExePath"
}

$root = "HKCU:\Software\Classes\outlookaddin"
New-Item -Path $root -Force | Out-Null
Set-ItemProperty -Path $root -Name "(default)" -Value "URL:OutlookAddin Protocol"
Set-ItemProperty -Path $root -Name "URL Protocol" -Value ""

$shellCmd = Join-Path $root "shell\open\command"
New-Item -Path $shellCmd -Force | Out-Null
Set-ItemProperty -Path $shellCmd -Name "(default)" -Value ("`"" + $ExePath + "`" `"%1`"")

Write-Host "Registered outlookaddin:// -> $ExePath" -ForegroundColor Green
