<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Product
        Id="*"
        Name="$(var.MyProductName) $(var.MyProductVersion)"
        Manufacturer="$(var.MyCompanyName)"
        Version="$(var.MyVersion)"
        Language="1033"
        UpgradeCode="82F812D9-4083-4EF1-8BC8-0F1EDA05B46B">

        <Package
            Description="$(var.MyProductName) - $(var.MyDescription)"
            InstallerVersion="200"
            Compressed="yes"
            InstallScope="perMachine" />
        <MajorUpgrade
            Disallow="yes"
            AllowDowngrades="no"
            AllowSameVersionUpgrades="no"
            DisallowUpgradeErrorMessage="An older version of $(var.MyProductName) is already installed. You must uninstall it before you can install this version."
            DowngradeErrorMessage="A newer version of $(var.MyProductName) is already installed." />
        <Media Id="1" Cabinet="WinFsp.cab" EmbedCab="yes" />

        <Property Id="P.LauncherName">$(var.MyProductName).Launcher</Property>
        <Property Id="P.LauncherRegistryKey">SYSTEM\\CurrentControlSet\\Services\\$(var.MyProductName).Launcher\\Services</Property>
        <Property Id="P.RegistryKey">Software\$(var.MyProductName)</Property>
        <Property Id="INSTALLDIR">
            <RegistrySearch
                Id="R.INSTALLDIR"
                Root="HKLM"
                Key="[P.RegistryKey]"
                Name="InstallDir"
                Type="raw" />
        </Property>

        <Directory Id="TARGETDIR" Name="SourceDir">
            <Directory Id="ProgramFilesFolder">
                <Directory Id="INSTALLDIR" Name="WinFsp">
                    <Directory Id="BINDIR" Name="bin" />
                    <Directory Id="INCDIR" Name="inc" />
                    <Directory Id="LIBDIR" Name="lib" />
                    <Directory Id="SMPDIR" Name="samples" />
                    <Directory Id="SYMDIR" Name="sym" />
                </Directory>
            </Directory>
        </Directory>

        <DirectoryRef Id="INSTALLDIR">
            <Component Id="C.INSTALLDIR" Guid="{F876F26E-5016-4AC6-93B3-653C0312A6CE}">
                <RegistryValue
                    Root="HKLM"
                    Key="[P.RegistryKey]"
                    Name="InstallDir"
                    Type="string"
                    Value="[INSTALLDIR]"
                    KeyPath="yes" />
            </Component>
        </DirectoryRef>
        <DirectoryRef Id="BINDIR" FileSource="..\build\$(var.Configuration)">
            <Component Id="C.winfsp_x64.sys">
                <File Name="winfsp-x64.sys" KeyPath="yes" />
            </Component>
            <Component Id="C.winfsp_x86.sys">
                <File Name="winfsp-x86.sys" KeyPath="yes" />
            </Component>

            <!-- On Win64 register winfsp-x64.dll -->
            <Component Id="C.winfsp_x64.dll.selfreg" Guid="F0A67746-1A9C-4976-8EC0-882E9407FA6D">
                <File Id="FILE.winfsp_x64.dll.selfreg" Name="winfsp-x64.dll" KeyPath="yes" SelfRegCost="1" />
                <Condition>VersionNT64</Condition>
            </Component>
            <Component Id="C.winfsp_x86.dll" Guid="950492FB-12F7-4E27-9124-8325A2BC9927">
                <File Name="winfsp-x86.dll" KeyPath="yes" />
                <Condition>VersionNT64</Condition>
            </Component>

            <!-- On Win32 register winfsp-x86.dll -->
            <Component Id="C.winfsp_x64.dll" Guid="4D6E7A8E-0CA6-49BE-B312-1EDADE725756">
                <File Name="winfsp-x64.dll" KeyPath="yes" />
                <Condition>NOT VersionNT64</Condition>
            </Component>
            <Component Id="C.winfsp_x86.dll.selfreg" Guid="F0DEF7A6-AF55-419F-A58A-DF4018C6FA73">
                <File Id="FILE.winfsp_x86.dll.selfreg" Name="winfsp-x86.dll" KeyPath="yes" SelfRegCost="1" />
                <Condition>NOT VersionNT64</Condition>
            </Component>

            <!-- On Win64 ServiceInstall launcher-x64.exe -->
            <Component Id="C.launcher_x64.exe.svcinst">
                <File Id="launcher_x64.exe.svcinst" Name="launcher-x64.exe" KeyPath="yes" />
                <ServiceInstall
                    Id="launcher_x64.exe.svcinst"
                    Name="[P.LauncherName]"
                    Description="$(var.MyDescription)"
                    Type="ownProcess"
                    Start="auto"
                    ErrorControl="ignore" />
                <ServiceControl
                    Id="launcher_x64.exe.svcinst"
                    Name="[P.LauncherName]"
                    Start="install"
                    Stop="both"
                    Remove="uninstall" />
                <Condition>VersionNT64</Condition>
            </Component>
            <Component Id="C.launcher_x86.exe">
                <File Name="launcher-x86.exe" KeyPath="yes" />
                <Condition>VersionNT64</Condition>
            </Component>

            <!-- On Win32 ServiceInstall launcher-x86.exe -->
            <Component Id="C.launcher_x64.exe" Guid="88CDBE92-8B67-485A-838F-FA4AD37F306F">
                <File Name="launcher-x64.exe" KeyPath="yes" />
                <Condition>NOT VersionNT64</Condition>
            </Component>
            <Component Id="C.launcher_x86.exe.svcinst" Guid="E995D906-0273-4758-9B26-99A3A8CD143A">
                <File Id="launcher_x86.exe.svcinst" Name="launcher-x86.exe" KeyPath="yes" />
                <ServiceInstall
                    Id="launcher_x86.exe.svcinst"
                    Name="[P.LauncherName]"
                    Description="$(var.MyDescription)"
                    Type="ownProcess"
                    Start="auto"
                    ErrorControl="ignore" />
                <ServiceControl
                    Id="launcher_x86.exe.svcinst"
                    Name="[P.LauncherName]"
                    Start="install"
                    Stop="both"
                    Remove="uninstall" />
                <Condition>NOT VersionNT64</Condition>
            </Component>

            <Component Id="C.launchctl_x64.exe" Guid="2753623B-66F1-4514-B9C7-F879178DFF49">
                <File Name="launchctl-x64.exe" KeyPath="yes" />
            </Component>
            <Component Id="C.launchctl_x86.exe" Guid="EBDEC4FB-07BB-47CA-BFFF-EB854CA2D22D">
                <File Name="launchctl-x86.exe" KeyPath="yes" />
            </Component>

            <Component Id="C.diag.bat">
                <File Name="diag.bat" Source="..\..\..\tools\diag.bat" KeyPath="yes" />
            </Component>

            <Component Id="C.memfs_x64.exe">
                <File Name="memfs-x64.exe" KeyPath="yes" />
                <RegistryKey
                    Root="HKLM"
                    Key="[P.LauncherRegistryKey]">
                    <RegistryKey
                        Key="memfs64">
                        <RegistryValue
                            Type="string"
                            Name="Executable"
                            Value="[BINDIR]memfs-x64.exe" />
                        <RegistryValue
                            Type="string"
                            Name="CommandLine"
                            Value="-i -F NTFS -n 65536 -s 67108864 -u %1 -m %2" />
                        <RegistryValue
                            Type="string"
                            Name="Security"
                            Value="D:P(A;;RPWPLC;;;WD)" />
                        <RegistryValue
                            Type="integer"
                            Name="JobControl"
                            Value="1" />
                    </RegistryKey>
                </RegistryKey>
            </Component>
            <Component Id="C.memfs_x86.exe">
                <File Name="memfs-x86.exe" KeyPath="yes" />
                <RegistryKey
                    Root="HKLM"
                    Key="[P.LauncherRegistryKey]">
                    <RegistryKey
                        Key="memfs32">
                        <RegistryValue
                            Type="string"
                            Name="Executable"
                            Value="[BINDIR]memfs-x86.exe" />
                        <RegistryValue
                            Type="string"
                            Name="CommandLine"
                            Value="-i -F NTFS -n 65536 -s 67108864 -u %1 -m %2" />
                        <RegistryValue
                            Type="string"
                            Name="Security"
                            Value="D:P(A;;RPWPLC;;;WD)" />
                        <RegistryValue
                            Type="integer"
                            Name="JobControl"
                            Value="1" />
                    </RegistryKey>
                </RegistryKey>
            </Component>
        </DirectoryRef>
        <DirectoryRef Id="INCDIR" FileSource="..\..\..\inc">
            <Directory Id="INCDIR.winfsp" Name="winfsp">
                <Component Id="C.fsctl.h">
                    <File Name="fsctl.h" KeyPath="yes" />
                </Component>
                <Component Id="C.winfsp.h">
                    <File Name="winfsp.h" KeyPath="yes" />
                </Component>
            </Directory>
            <Directory Id="INCDIR.fuse" Name="fuse">
                <Component Id="C.fuse.h">
                    <File Name="fuse.h" KeyPath="yes" />
                </Component>
                <Component Id="C.fuse_common.h">
                    <File Name="fuse_common.h" KeyPath="yes" />
                </Component>
                <Component Id="C.fuse_opt.h">
                    <File Name="fuse_opt.h" KeyPath="yes" />
                </Component>
                <Component Id="C.winfsp_fuse.h">
                    <File Name="winfsp_fuse.h" KeyPath="yes" />
                </Component>
            </Directory>
        </DirectoryRef>
        <DirectoryRef Id="LIBDIR" FileSource="..\build\$(var.Configuration)">
            <Component Id="C.winfsp_x64.lib">
                <File Name="winfsp-x64.lib" KeyPath="yes" />
            </Component>
            <Component Id="C.winfsp_x86.lib">
                <File Name="winfsp-x86.lib" KeyPath="yes" />
            </Component>

            <!-- On Win64 copy fuse-x64.pc -->
            <Component Id="C.fuse_x64.pc" Guid="407395D2-D076-411E-B1D0-D97E21E11A3C">
                <File
                    Id="FILE.fuse_x64.pc"
                    Name="fuse.pc"
                    Source="..\build\$(var.Configuration)\fuse-x64.pc"
                    KeyPath="yes" />
                <Condition>VersionNT64</Condition>
            </Component>

            <!-- On Win32 copy fuse-x86.pc -->
            <Component Id="C.fuse_x86.pc" Guid="0568EBCB-782E-4C17-9B64-BAFCC43F64ED">
                <File
                    Id="FILE.fuse_x86.pc"
                    Name="fuse.pc"
                    Source="..\build\$(var.Configuration)\fuse-x86.pc"
                    KeyPath="yes" />
                <Condition>NOT VersionNT64</Condition>
            </Component>
        </DirectoryRef>
        <DirectoryRef Id="SMPDIR" FileSource="..\..\..\tst">
            <Directory Id="SMPDIR.memfs" Name="memfs">
                <Component Id="C.memfs.h">
                    <File Name="memfs.h" KeyPath="yes" />
                </Component>
                <Component Id="C.memfs.cpp">
                    <File Name="memfs.cpp" KeyPath="yes" />
                </Component>
                <Component Id="C.memfs_main.c">
                    <File Name="memfs-main.c" KeyPath="yes" />
                </Component>
            </Directory>
        </DirectoryRef>
        <DirectoryRef Id="SYMDIR">
            <Component Id="C.winfsp_x64.sys.pdb">
                <File Name="winfsp-x64.sys.pdb" Source="..\build\$(var.Configuration)\winfsp-x64.sys.public.pdb" KeyPath="yes" />
            </Component>
            <Component Id="C.winfsp_x86.sys.pdb">
                <File Name="winfsp-x86.sys.pdb" Source="..\build\$(var.Configuration)\winfsp-x86.sys.public.pdb" KeyPath="yes" />
            </Component>
            <Component Id="C.winfsp_x64.dll.pdb">
                <File Name="winfsp-x64.dll.pdb" Source="..\build\$(var.Configuration)\winfsp-x64.dll.public.pdb" KeyPath="yes" />
            </Component>
            <Component Id="C.winfsp_x86.dll.pdb">
                <File Name="winfsp-x86.dll.pdb" Source="..\build\$(var.Configuration)\winfsp-x86.dll.public.pdb" KeyPath="yes" />
            </Component>
            <Component Id="C.launcher_x64.pdb">
                <File Name="launcher-x64.pdb" Source="..\build\$(var.Configuration)\launcher-x64.public.pdb" KeyPath="yes" />
            </Component>
            <Component Id="C.launcher_x86.pdb">
                <File Name="launcher-x86.pdb" Source="..\build\$(var.Configuration)\launcher-x86.public.pdb" KeyPath="yes" />
            </Component>
            <Component Id="C.launchctl_x64.pdb">
                <File Name="launchctl-x64.pdb" Source="..\build\$(var.Configuration)\launchctl-x64.public.pdb" KeyPath="yes" />
            </Component>
            <Component Id="C.launchctl_x86.pdb">
                <File Name="launchctl-x86.pdb" Source="..\build\$(var.Configuration)\launchctl-x86.public.pdb" KeyPath="yes" />
            </Component>
            <Component Id="C.memfs_x64.pdb">
                <File Name="memfs-x64.pdb" Source="..\build\$(var.Configuration)\memfs-x64.public.pdb" KeyPath="yes" />
            </Component>
            <Component Id="C.memfs_x86.pdb">
                <File Name="memfs-x86.pdb" Source="..\build\$(var.Configuration)\memfs-x86.public.pdb" KeyPath="yes" />
            </Component>
        </DirectoryRef>

        <ComponentGroup Id="C.WinFsp.bin">
            <ComponentRef Id="C.winfsp_x64.sys" />
            <ComponentRef Id="C.winfsp_x86.sys" />
            <ComponentRef Id="C.winfsp_x64.dll.selfreg" />
            <ComponentRef Id="C.winfsp_x86.dll" />
            <ComponentRef Id="C.winfsp_x64.dll" />
            <ComponentRef Id="C.winfsp_x86.dll.selfreg" />
            <ComponentRef Id="C.launcher_x64.exe.svcinst" />
            <ComponentRef Id="C.launcher_x86.exe" />
            <ComponentRef Id="C.launcher_x64.exe" />
            <ComponentRef Id="C.launcher_x86.exe.svcinst" />
            <ComponentRef Id="C.launchctl_x64.exe" />
            <ComponentRef Id="C.launchctl_x86.exe" />
            <ComponentRef Id="C.diag.bat" />
        </ComponentGroup>
        <ComponentGroup Id="C.WinFsp.inc">
            <ComponentRef Id="C.fsctl.h" />
            <ComponentRef Id="C.winfsp.h" />
            <ComponentRef Id="C.fuse.h" />
            <ComponentRef Id="C.fuse_common.h" />
            <ComponentRef Id="C.fuse_opt.h" />
            <ComponentRef Id="C.winfsp_fuse.h" />
        </ComponentGroup>
        <ComponentGroup Id="C.WinFsp.lib">
            <ComponentRef Id="C.winfsp_x64.lib" />
            <ComponentRef Id="C.winfsp_x86.lib" />
            <ComponentRef Id="C.fuse_x64.pc" />
            <ComponentRef Id="C.fuse_x86.pc" />
        </ComponentGroup>
        <ComponentGroup Id="C.WinFsp.smp">
            <ComponentRef Id="C.memfs_x64.exe" />
            <ComponentRef Id="C.memfs_x86.exe" />
            <ComponentRef Id="C.memfs.h" />
            <ComponentRef Id="C.memfs.cpp" />
            <ComponentRef Id="C.memfs_main.c" />
        </ComponentGroup>
        <ComponentGroup Id="C.WinFsp.sym">
            <ComponentRef Id="C.winfsp_x64.sys.pdb" />
            <ComponentRef Id="C.winfsp_x86.sys.pdb" />
            <ComponentRef Id="C.winfsp_x86.dll.pdb" />
            <ComponentRef Id="C.winfsp_x64.dll.pdb" />
            <ComponentRef Id="C.launcher_x86.pdb" />
            <ComponentRef Id="C.launcher_x64.pdb" />
            <ComponentRef Id="C.launchctl_x64.pdb" />
            <ComponentRef Id="C.launchctl_x86.pdb" />
            <ComponentRef Id="C.memfs_x64.pdb" />
            <ComponentRef Id="C.memfs_x86.pdb" />
        </ComponentGroup>

        <Feature
            Id="F.Main"
            Level="1"
            Title="$(var.MyProductName) $(var.MyProductVersion)"
            Description="$(var.MyDescription)"
            Display="expand"
            ConfigurableDirectory="INSTALLDIR"
            AllowAdvertise="no"
            InstallDefault="local"
            Absent="disallow">
            <ComponentRef Id="C.INSTALLDIR" />
            <Feature
                Id="F.User"
                Level="1"
                Title="Core"
                Description="The core $(var.MyProductName) files."
                AllowAdvertise="no"
                InstallDefault="local"
                Absent="disallow">
                <ComponentGroupRef Id="C.WinFsp.bin" />
            </Feature>
            <Feature
                Id="F.Developer"
                Level="1000"
                Title="Developer"
                Description="Additional files needed for development."
                AllowAdvertise="no"
                InstallDefault="local"
                Absent="allow">
                <ComponentGroupRef Id="C.WinFsp.inc" />
                <ComponentGroupRef Id="C.WinFsp.lib" />
                <ComponentGroupRef Id="C.WinFsp.smp" />
                <ComponentGroupRef Id="C.WinFsp.sym" />
            </Feature>
        </Feature>

        <WixVariable Id="WixUIBannerBmp" Value="wixbanner.bmp" />
        <WixVariable Id="WixUIDialogBmp" Value="wixdialog-$(var.MyProductStage).bmp" />
        <UI Id="FeatureTree">
            <UIRef Id="WixUI_FeatureTree" />
            <!-- skip the license agreement dialog; higher Order takes priority (weird) -->
            <Publish
                Dialog="WelcomeDlg"
                Control="Next"
                Event="NewDialog"
                Value="CustomizeDlg"
                Order="10">NOT Installed</Publish>
            <Publish
                Dialog="CustomizeDlg"
                Control="Back"
                Event="NewDialog"
                Value="WelcomeDlg"
                Order="10">NOT Installed</Publish>
        </UI>

        <Binary Id="CustomActions" SourceFile="..\build\$(var.Configuration)\CustomActions.dll" />
        <CustomAction
            Id="Params.ServiceRunning"
            Property="ServiceRunning"
            Value="$(var.MyProductName)" />
        <CustomAction
            Id="Action.ServiceRunning"
            BinaryKey="CustomActions"
            DllEntry="ServiceRunning"
            Execute="immediate"
            Return="ignore" />
        <CustomAction
            Id="Action.ServiceRunning.Error"
            Error="The $(var.MyProductName) service appears to be running. If you just uninstalled $(var.MyProductName) please restart your computer. If you are running a development version of $(var.MyProductName) please remove it before proceeding." />
        <InstallExecuteSequence>
            <Custom Action="Params.ServiceRunning" Before="Action.ServiceRunning" />
            <Custom Action="Action.ServiceRunning" Before="LaunchConditions" />
            <Custom Action="Action.ServiceRunning.Error" After="Action.ServiceRunning">
                <![CDATA[NOT Installed AND (0 <> ServiceRunning)]]>
            </Custom>
            <ScheduleReboot After="RemoveFiles">
                <![CDATA[(REMOVE ~= "ALL") AND (0 <> ServiceRunning)]]>
            </ScheduleReboot>
        </InstallExecuteSequence>
    </Product>
</Wix>