<Wix
	xmlns="http://wixtoolset.org/schemas/v4/wxs"
	xmlns:util="http://wixtoolset.org/schemas/v4/wxs/util">
	<Package Name="NetBird" Version="$(env.NETBIRD_VERSION)" Manufacturer="NetBird GmbH" Language="1033" UpgradeCode="6456ec4e-3ad6-4b9b-a2be-98e81cb21ccf"
        InstallerVersion="500" Compressed="yes"  Codepage="utf-8" >


		<MediaTemplate EmbedCab="yes" />

		<Feature Id="NetbirdFeature" Title="Netbird" Level="1">
			<ComponentGroupRef Id="NetbirdFilesComponent" />
		</Feature>

		<MajorUpgrade AllowSameVersionUpgrades='yes' DowngradeErrorMessage="A newer version of [ProductName] is already installed. Setup will now exit."/>

		<!-- Autostart: enabled by default, disable with AUTOSTART=0 on the msiexec command line -->
		<Property Id="AUTOSTART" Value="1" />

		<StandardDirectory Id="ProgramFiles64Folder">
			<Directory Id="NetbirdInstallDir" Name="Netbird">
				<Component Id="NetbirdFiles" Guid="db3165de-cc6e-4922-8396-9d892950e23e" Bitness="always64">
					<File ProcessorArchitecture="$(var.ProcessorArchitecture)" Source=".\dist\netbird_windows_$(var.ArchSuffix)\netbird.exe" KeyPath="yes" />
					<File ProcessorArchitecture="$(var.ProcessorArchitecture)" Source=".\dist\netbird_windows_$(var.ArchSuffix)\netbird-ui.exe">
						<Shortcut Id="NetbirdDesktopShortcut" Directory="DesktopFolder" Name="NetBird" WorkingDirectory="NetbirdInstallDir" Icon="NetbirdIcon">
							<ShortcutProperty Key="System.AppUserModel.ID" Value="NetBird" />
							<ShortcutProperty Key="System.AppUserModel.ToastActivatorCLSID" Value="{0E1B4DE7-E148-432B-9814-544F941826EC}" />
						</Shortcut>
						<Shortcut Id="NetbirdStartMenuShortcut" Directory="StartMenuFolder" Name="NetBird" WorkingDirectory="NetbirdInstallDir" Icon="NetbirdIcon">
							<ShortcutProperty Key="System.AppUserModel.ID" Value="NetBird" />
							<ShortcutProperty Key="System.AppUserModel.ToastActivatorCLSID" Value="{0E1B4DE7-E148-432B-9814-544F941826EC}" />
						</Shortcut>
					</File>
					<File ProcessorArchitecture="$(var.ProcessorArchitecture)" Source=".\dist\netbird_windows_$(var.ArchSuffix)\wintun.dll" />
					<File Id="NetbirdToastIcon" Name="netbird.png" Source=".\client\ui\assets\netbird.png" />

					<ServiceInstall
                                     Id="NetBirdService"
                                     Name="NetBird"
                                     DisplayName="NetBird"
                                     Description="Connect your devices into a secure WireGuard-based overlay network with SSO, MFA and granular access controls."
                                     Start="auto" Type="ownProcess"
                                     ErrorControl="normal"
                                     Account="LocalSystem"
                                     Vital="yes"
                                     Interactive="no"
                                     Arguments='service run config [CommonAppDataFolder]Netbird\config.json log-level info'
                        />
					<ServiceControl Id="NetBirdService" Name="NetBird" Start="install" Stop="both" Remove="uninstall" Wait="yes" />

					<Environment Id="UpdatePath" Name="PATH" Value="[NetbirdInstallDir]" Part="last" Action="set" System="yes" />

				</Component>
			</Directory>
		</StandardDirectory>

		<!-- Per-user component: HKCU keypath (auto GUID via "*"), separate from
		     the per-machine NetbirdFiles component to satisfy ICE57. -->
		<StandardDirectory Id="ProgramMenuFolder">
			<Component Id="NetbirdAumidRegistry" Guid="*">
				<RegistryKey Root="HKCU" Key="Software\Classes\AppUserModelId\NetBird" ForceDeleteOnUninstall="yes">
					<RegistryValue Name="InstalledByMSI" Type="integer" Value="1" KeyPath="yes" />
					<!-- Pre-seed the CLSID the Wails notifications service reads on
					     first startup (notifications_windows.go:getGUID looks for
					     the CustomActivator value under this key). Without this
					     the service generates a fresh per-install UUID, which
					     diverges from the ToastActivatorCLSID set on the Start
					     Menu / Desktop shortcuts above and the COM activator
					     never fires when a toast is clicked. -->
					<RegistryValue Name="CustomActivator" Type="string" Value="{0E1B4DE7-E148-432B-9814-544F941826EC}" />
				</RegistryKey>
			</Component>
		</StandardDirectory>

		<StandardDirectory Id="CommonAppDataFolder">
			<Directory Id="NetbirdAutoStartDir" Name="Netbird">
				<Component Id="NetbirdAutoStart" Guid="b199eaca-b0dd-4032-af19-679cfad48eb3" Bitness="always64" Condition='AUTOSTART = "1"'>
					<RegistryValue Root="HKLM" Key="Software\Microsoft\Windows\CurrentVersion\Run"
						Name="Netbird" Value="&quot;[NetbirdInstallDir]netbird-ui.exe&quot;"
						Type="string" KeyPath="yes" />
				</Component>
			</Directory>
		</StandardDirectory>

		<ComponentGroup Id="NetbirdFilesComponent">
			<ComponentRef Id="NetbirdFiles" />
			<ComponentRef Id="NetbirdAumidRegistry" />
			<ComponentRef Id="NetbirdAutoStart" />
		</ComponentGroup>

		<util:CloseApplication Id="CloseNetBird" CloseMessage="no" Target="netbird.exe" RebootPrompt="no" />
		<util:CloseApplication Id="CloseNetBirdUI" CloseMessage="no" Target="netbird-ui.exe" RebootPrompt="no" TerminateProcess="0" />

		<!-- WebView2 evergreen runtime detection.
		     Probe both the per-machine and per-user EdgeUpdate keys; if either
		     reports a non-empty `pv` value the runtime is already installed
		     and we skip the bootstrapper. -->
		<Property Id="WEBVIEW2_VERSION_HKLM">
			<RegistrySearch Id="WV2HKLM" Root="HKLM"
				Key="SOFTWARE\WOW6432Node\Microsoft\EdgeUpdate\Clients\{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}"
				Name="pv" Type="raw" Bitness="always64" />
		</Property>
		<Property Id="WEBVIEW2_VERSION_HKCU">
			<RegistrySearch Id="WV2HKCU" Root="HKCU"
				Key="Software\Microsoft\EdgeUpdate\Clients\{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}"
				Name="pv" Type="raw" />
		</Property>

		<!-- Embed the bootstrapper payload. Path is relative to the WiX
		     working directory; sign-pipelines stages it next to client/
		     via `wails3 generate webview2bootstrapper`. -->
		<Binary Id="WebView2Bootstrapper" SourceFile=".\client\MicrosoftEdgeWebview2Setup.exe" />

		<CustomAction Id="InstallWebView2"
			BinaryRef="WebView2Bootstrapper"
			ExeCommand="/silent /install"
			Execute="deferred"
			Impersonate="no"
			Return="check" />

		<InstallExecuteSequence>
			<Custom Action="InstallWebView2" Before="InstallFinalize"
				Condition="NOT WEBVIEW2_VERSION_HKLM AND NOT WEBVIEW2_VERSION_HKCU AND NOT REMOVE" />
		</InstallExecuteSequence>

		<!-- Icons -->
		<Icon Id="NetbirdIcon" SourceFile=".\client\ui\build\windows\icon.ico" />
		<Property Id="ARPPRODUCTICON" Value="NetbirdIcon" />

	</Package>
</Wix>
