<?xml version="1.0" encoding="utf-8" ?>

<!--
  Installer uses the following icons licensed under the MIT license:
  * "assets\exclamation.ico" from https://icons.getbootstrap.com/icons/exclamation-circle/
  * "assets\information.ico" from https://icons.getbootstrap.com/icons/info-circle/
  * "assets\new.ico" from https://icons.getbootstrap.com/icons/folder-plus/
  * "assets\up.ico" from https://icons.getbootstrap.com/icons/folder-symlink/

  The project logo is based on the "Fox SVG Vector" icon and the community-introduced PWA logo:
  * https://www.svgrepo.com/svg/40267/fox (CC0)
  * https://github.com/webmaxru/progressive-web-apps-logo (CC0)

  Firefox and the Firefox logo are trademarks of the Mozilla Foundation in the U.S. and other countries.
  This project is not affiliated with Mozilla Foundation in any way.
-->

<!--
  Important: Generate UserChrome component group before running cargo wix!
  $ heat dir userchrome -o packages/wix/userchrome.wxs -scom -srd -sreg -gg -cg UserChrome -dr UserChromeDir -var wix.UserChromeSource
-->

<?if $(sys.BUILDARCH) = x64 or $(sys.BUILDARCH) = arm64 ?>
    <?define PlatformProgramFilesFolder = "ProgramFiles64Folder" ?>
<?else ?>
    <?define PlatformProgramFilesFolder = "ProgramFilesFolder" ?>
<?endif ?>

<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Product
        Id="*"
        UpgradeCode="E1CD921B-369E-47CB-AD2E-3BCA94142512"

        Name="PWAsForFirefox"
        Manufacturer="filips"
        Version="$(var.Version)"

        Language="1033"
        Codepage="1252"
    >
        <!-- Installer package that installs PWAsForFirefox native components -->
        <Package
            Id="*"

            Keywords="PWAsForFirefox Installer"
            Description="The native part of the PWAsForFirefox project"
            Manufacturer="filips"

            InstallerVersion="500"
            Languages="1033"
            SummaryCodepage="1252"
            Compressed="yes"
        />

        <!-- Handle removing old version when upgrading  -->
        <MajorUpgrade Schedule="afterInstallInitialize" DowngradeErrorMessage="A newer version of [ProductName] is already installed. Setup will now exit." />

        <!-- Apparently needed for WiX to work -->
        <Media Id="1" Cabinet="media1.cab" EmbedCab="yes" DiskPrompt="CD-ROM #1" />
        <Property Id="DiskPrompt" Value="PWAsForFirefox Installation [1]" />

        <!-- Define all directories and components -->
        <Directory Id="TARGETDIR" Name="SourceDir">
            <Directory Id="$(var.PlatformProgramFilesFolder)" Name="PFiles">
                <Directory Id="APPLICATIONFOLDER" Name="FirefoxPWA">
                    <!-- Contains the license file -->
                    <Component Id="License" Guid="B0F7D2BD-47D2-44DE-9F9F-AE5D5CEFE760">
                        <File
                            Id="LicenseFile"
                            Name="LICENSE"
                            Source="LICENSE"
                            DiskId="1"
                            KeyPath="yes"
                        />
                    </Component>

                    <!-- Contains the native app manifest for communication with the extension -->
                    <Component Id="Manifest" Guid="44990C1D-C76C-4CFC-8A36-B94CFD277910">
                        <File
                            Id="ManifestFile"
                            Name="firefoxpwa.json"
                            Source="manifests/windows.json"
                            DiskId="1"
                            KeyPath="yes"
                        />
                        <RegistryKey Root="HKMU" Key="SOFTWARE\Mozilla\NativeMessagingHosts\firefoxpwa">
                            <RegistryValue Type="string" Value="[#ManifestFile]" />
                        </RegistryKey>
                    </Component>

                    <!-- Contains the main console program -->
                    <Component Id="MainBinary" Guid="711E74CE-248B-48FA-B4B3-BD46B748DB46">
                        <File
                            Id="MainBinaryFile"
                            Name="firefoxpwa.exe"
                            Source="$(var.CargoTargetBinDir)\firefoxpwa.exe"
                            DiskId="1"
                            KeyPath="yes"
                        />
                    </Component>

                    <!-- Contains the connector program for the extension -->
                    <Component Id="ConnectorBinary" Guid="1FEAF44F-C68F-46C6-93A7-AB3278FEFCBB">
                        <File
                            Id="ConnectorBinaryFile"
                            Name="firefoxpwa-connector.exe"
                            Source="$(var.CargoTargetBinDir)\firefoxpwa-connector.exe"
                            DiskId="1"
                            KeyPath="yes"
                        />
                    </Component>

                    <!-- Contains all shell completions for the console program -->
                    <!-- Users can manually add them to their shells if they want -->
                    <Directory Id="CompletionsDir" Name="completions">
                        <Component Id="BashCompletions" Guid="5E35D355-756E-4E0F-9F99-20F4E21534B2">
                            <File
                                Id="BashCompletionsFile"
                                Name="firefoxpwa.bash"
                                Source="$(var.CargoTargetBinDir)\completions\firefoxpwa.bash"
                                DiskId="1"
                                KeyPath="yes"
                            />
                        </Component>
                        <Component Id="ElvishCompletions" Guid="336E7C03-E919-47EA-A206-A8FD0D9441A9">
                            <File
                                Id="ElvishCompletionsFile"
                                Name="firefoxpwa.elv"
                                Source="$(var.CargoTargetBinDir)\completions\firefoxpwa.elv"
                                DiskId="1"
                                KeyPath="yes"
                            />
                        </Component>
                        <Component Id="FishCompletions" Guid="FF7D9ACA-FDC9-44EF-8204-905EAA8B2BA0">
                            <File
                                Id="FishCompletionsFile"
                                Name="firefoxpwa.fish"
                                Source="$(var.CargoTargetBinDir)\completions\firefoxpwa.fish"
                                DiskId="1"
                                KeyPath="yes"
                            />
                        </Component>
                        <Component Id="PowerShellCompletions" Guid="352140E6-2977-4AF4-AB26-8FDF771A8B2E">
                            <File
                                Id="PowerShellCompletionsFile"
                                Name="_firefoxpwa.ps1"
                                Source="$(var.CargoTargetBinDir)\completions\_firefoxpwa.ps1"
                                DiskId="1"
                                KeyPath="yes"
                            />
                        </Component>
                        <Component Id="ZshCompletions" Guid="60CDAC18-2A12-48FB-89EF-E4C293081307">
                            <File
                                Id="ZshCompletionsFile"
                                Name="_firefoxpwa"
                                Source="$(var.CargoTargetBinDir)\completions\_firefoxpwa"
                                DiskId="1"
                                KeyPath="yes"
                            />
                        </Component>
                    </Directory>

                    <!-- Directory where UserChrome modifications are stored -->
                    <!-- All files are added by a component group that needs to be manually generated by running heat before building the installer -->
                    <Directory Id="UserChromeDir" Name="userchrome" />

                    <!-- Adds installation directory to the PATH environment variable -->
                    <!-- We need two components to support both per-user and per-machine installs -->
                    <Component Id="PathEntryUser" Guid="20C5BA9A-0C8E-4974-9E43-7B3BBC7165EF">
                        <Condition>MSIINSTALLPERUSER = 1</Condition>
                        <CreateFolder />
                        <Environment
                            Id="PathUser"
                            Name="PATH"
                            Value="[APPLICATIONFOLDER]"
                            Part="last"
                            Action="set"
                            Permanent="yes"
                            System="no"
                        />
                    </Component>
                    <Component Id="PathEntryMachine" Guid="BC3E15AF-181A-4160-9ACD-F7A39583872A">
                        <Condition>NOT MSIINSTALLPERUSER = 1</Condition>
                        <CreateFolder />
                        <Environment
                            Id="PathMachine"
                            Name="PATH"
                            Value="[APPLICATIONFOLDER]"
                            Part="last"
                            Action="set"
                            Permanent="yes"
                            System="yes"
                        />
                    </Component>

                    <!-- Stores installation directory and version in known registry location so the program can read it -->
                    <!-- Also marks binaries as a host processes for a better integration with the taskbar -->
                    <Component Id="RegistryEntries" Guid="C0B6535A-9A56-411E-B8D5-F1Ee901Ec440">
                        <RegistryKey Root="HKMU" Key="Software\filips\FirefoxPWA">
                            <RegistryValue Type="string" Name="Path" Value="[APPLICATIONFOLDER]" KeyPath="yes" />
                            <RegistryValue Type="string" Name="Version" Value="$(var.Version)" />
                        </RegistryKey>
                        <RegistryKey Root="HKCR" Key="Applications\firefoxpwa.exe">
                            <RegistryValue Type="string" Name="IsHostApp" Value="" />
                            <RegistryValue Type="string" Name="NoOpenWith" Value="" />
                        </RegistryKey>
                        <RegistryKey Root="HKCR" Key="Applications\firefoxpwa-connector.exe">
                            <RegistryValue Type="string" Name="IsHostApp" Value="" />
                            <RegistryValue Type="string" Name="NoOpenWith" Value="" />
                        </RegistryKey>
                    </Component>
                </Directory>
            </Directory>
        </Directory>

        <!-- Everything is packed into the same feature -->
        <Feature
            Id="Binaries"
            Level="1"

            Title="PWAsForFirefox"
            Description="Installs the native part of the PWAsForFirefox project"

            ConfigurableDirectory="APPLICATIONFOLDER"
            Display="expand"
            AllowAdvertise="no"
            Absent="disallow"
        >
            <ComponentRef Id="RegistryEntries" />
            <ComponentRef Id="PathEntryUser" />
            <ComponentRef Id="PathEntryMachine" />
            <ComponentRef Id="License" />
            <ComponentRef Id="Manifest" />
            <ComponentRef Id="MainBinary" />
            <ComponentRef Id="ConnectorBinary" />
            <ComponentRef Id="BashCompletions" />
            <ComponentRef Id="ElvishCompletions" />
            <ComponentRef Id="FishCompletions" />
            <ComponentRef Id="PowerShellCompletions" />
            <ComponentRef Id="ZshCompletions" />
            <ComponentGroupRef Id="UserChrome" />
        </Feature>

        <!-- Configure info that are shown on Windows ARP page -->
        <Icon Id="ProductICO" SourceFile="packages\wix\assets\product.ico" />
        <Property Id="ARPPRODUCTICON" Value="ProductICO" />
        <Property Id="ARPHELPLINK" Value="https://pwasforfirefox.filips.si/" />
        <Property Id="ARPURLINFOABOUT" Value="https://pwasforfirefox.filips.si/" />
        <Property Id="ARPURLUPDATEINFO" Value="https://github.com/filips123/PWAsForFirefox/releases" />
        <SetProperty Id="ARPINSTALLLOCATION" Value="[APPLICATIONFOLDER]" After="CostFinalize" />

        <!-- Configure license, images and icons that are displayed in installer -->
        <WixVariable Id="WixUILicenseRtf" Value="packages\wix\assets\license.rtf" />
        <WixVariable Id="WixUIBannerBmp" Value="packages\wix\assets\banner.png" />
        <WixVariable Id="WixUIDialogBmp" Value="packages\wix\assets\dialog.png" />
        <WixVariable Id="WixUIExclamationIco" Value="packages\wix\assets\exclamation.ico" />
        <WixVariable Id="WixUIInfoIco" Value="packages\wix\assets\information.ico" />
        <WixVariable Id="WixUINewIco" Value="packages\wix\assets\new.ico" />
        <WixVariable Id="WixUIUpIco" Value="packages\wix\assets\up.ico" />

        <!-- Configure WiX to support per-user and per-machine installs -->
        <WixVariable Id="WixUISupportPerUser" Value="1" />
        <WixVariable Id="WixUISupportPerMachine" Value="1" />
        <Property Id="WIXUI_INSTALLDIR" Value="APPLICATIONFOLDER" />
        <Property Id="MSIINSTALLPERUSER" Secure="yes" />
        <Property Id="ALLUSERS" Value="2" Secure="yes" />

        <!-- Configure default installation folders for per-user and per-machine installs -->
        <Property Id="ApplicationFolderName" Value="FirefoxPWA" />
        <CustomAction Id="SetDefaultPerUserFolder" Property="WixPerUserFolder" Value="[LocalAppDataFolder]Programs\[ApplicationFolderName]\" Execute="immediate" />
        <CustomAction Id="SetDefaultPerMachineFolder" Property="WixPerMachineFolder" Value="[$(var.PlatformProgramFilesFolder)][ApplicationFolderName]\" Execute="immediate" />
        <CustomAction Id="SetPerUserFolder" Property="APPLICATIONFOLDER" Value="[WixPerUserFolder]" Execute="immediate" />
        <CustomAction Id="SetPerMachineFolder" Property="APPLICATIONFOLDER" Value="[WixPerMachineFolder]" Execute="immediate" />
        <Property Id="WixAppFolder" Value="WixPerMachineFolder" />

        <!-- Configure execute and UI sequences to set the correct directory -->
        <InstallExecuteSequence>
            <Custom Action="SetDefaultPerUserFolder" Before="CostFinalize" />
            <Custom Action="SetDefaultPerMachineFolder" After="SetDefaultPerUserFolder" />
            <Custom Action="SetPerUserFolder" After="SetDefaultPerMachineFolder">ACTION="INSTALL" AND APPLICATIONFOLDER="" AND (ALLUSERS="" OR (ALLUSERS=2 AND (NOT Privileged)))</Custom>
            <Custom Action="SetPerMachineFolder" After="SetPerUserFolder">ACTION="INSTALL" AND APPLICATIONFOLDER="" AND (ALLUSERS=1 OR (ALLUSERS=2 AND Privileged))</Custom>
        </InstallExecuteSequence>
        <InstallUISequence>
            <Custom Action="SetDefaultPerUserFolder" Before="CostFinalize" />
            <Custom Action="SetDefaultPerMachineFolder" After="SetDefaultPerUserFolder" />
            <Custom Action="SetPerUserFolder" After="SetDefaultPerMachineFolder">ACTION="INSTALL" AND APPLICATIONFOLDER="" AND (ALLUSERS="" OR (ALLUSERS=2 AND (NOT Privileged)))</Custom>
            <Custom Action="SetPerMachineFolder" After="SetPerUserFolder">ACTION="INSTALL" AND APPLICATIONFOLDER="" AND (ALLUSERS=1 OR (ALLUSERS=2 AND Privileged))</Custom>
        </InstallUISequence>

        <!-- Modify the UI to allow the user to customize installation scope and directory -->
        <UI>
            <UIRef Id="WixUI_InstallDir" />
            <Publish Dialog="LicenseAgreementDlg" Control="Next" Event="NewDialog" Value="InstallScopeDlg">LicenseAccepted = "1"</Publish>
            <Publish Dialog="InstallScopeDlg" Control="Back" Event="NewDialog" Value="LicenseAgreementDlg">1</Publish>
            <Publish Dialog="InstallScopeDlg" Control="Next" Property="WixAppFolder" Value="WixPerUserFolder" Order="1">NOT Privileged</Publish>
            <Publish Dialog="InstallScopeDlg" Control="Next" Property="ALLUSERS" Value="{}" Order="2">WixAppFolder = "WixPerUserFolder"</Publish>
            <Publish Dialog="InstallScopeDlg" Control="Next" Property="ALLUSERS" Value="1" Order="2">WixAppFolder = "WixPerMachineFolder"</Publish>
            <Publish Dialog="InstallScopeDlg" Control="Next" Property="MSIINSTALLPERUSER" Value="1" Order="3">WixAppFolder = "WixPerUserFolder"</Publish>
            <Publish Dialog="InstallScopeDlg" Control="Next" Property="MSIINSTALLPERUSER" Value="{}" Order="3">WixAppFolder = "WixPerMachineFolder"</Publish>
            <Publish Dialog="InstallScopeDlg" Control="Next" Property="APPLICATIONFOLDER" Value="[WixPerUserFolder]" Order="4">WixAppFolder = "WixPerUserFolder"</Publish>
            <Publish Dialog="InstallScopeDlg" Control="Next" Property="APPLICATIONFOLDER" Value="[WixPerMachineFolder]" Order="4">WixAppFolder = "WixPerMachineFolder"</Publish>
            <Publish Dialog="InstallScopeDlg" Control="Next" Event="DoAction" Value="FindRelatedProducts" Order="5">1</Publish>
            <Publish Dialog="InstallScopeDlg" Control="Next" Event="NewDialog" Value="InstallDirDlg" Order="6">1</Publish>
            <Publish Dialog="InstallDirDlg" Control="Back" Event="NewDialog" Value="InstallScopeDlg">1</Publish>
        </UI>

        <!-- Needed so UserChrome component group can properly determine the correct source directory -->
        <WixVariable Id="UserChromeSource" Value="userchrome" />
    </Product>
</Wix>