<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi" 
     xmlns:util="http://schemas.microsoft.com/wix/UtilExtension">
  <!-- http://wixtoolset.org/documentation/manual/v3/xsd/wix/product.html -->
  <Product Id="{{ProductCode}}" 
           UpgradeCode="{{UpgradeCode}}"
           Name = "{{ApplicationName}}"
           Version="{{Version}}"
           Manufacturer="{{Manufacturer}}"
           Language="{{Language}}">
    <!-- Only run this installer on Windows 7 or up (or if it"s already installed, I guess) -->
    <!-- <Condition Message="This application is only supported on Windows 7 or higher.">
      <![CDATA[Installed OR (VersionNT >= 601)]]>
    </Condition> -->
    <!-- http://wixtoolset.org/documentation/manual/v3/xsd/wix/package.html -->
    <Package InstallerVersion="405"
             Compressed="yes"
             Comments="Windows Installer Package"
             Platform="{{Platform}}"
             InstallScope="{{PackageScope}}"/>
    <!-- Don't allow downgrades -->
    <MajorUpgrade AllowSameVersionUpgrades="yes" DowngradeErrorMessage="A later version of this product is already installed. Setup will now exit."/>
    <!-- This will hide our Uninstall entry in Apps & Features. We doing this so
    we can write our own which we can better control. -->
    <Property Id="ARPSYSTEMCOMPONENT" Value="1" />
    <!-- While the MSI package is hidden in Apps & Features, it can still be queried
    via PowerShell and other means. To differentiate we give the public entry a slightly
    different name to make admins life easier.  -->
    <Property Id="VisibleProductName" Value="{{ApplicationName}}" />
    <!-- Tells the package to install perUser or perMachine. In case of perUser, all
    files will be redirected to the user profile and all registry entries to HKCU. -->
    <Property Id="MSIINSTALLPERUSER" Secure="yes" Value="{{InstallPerUser}}" />
    <!-- Overides the default install mode. It solves a problem where
    individual packaged files that have the same version as in previous
    installed App version will be deleted if the files are in use during
    this upgrade. Unfortunately this causes an ICE 40 warning during linking. -->
    <Property Id="REINSTALLMODE" Value="emus" />
    <!-- Overrides the default reboot behavior if files are in use during the upgrade.
    By default, this will be set to "ReallySuppress" to make sure no unexpected reboot will happpen.-->
    <Property Id="REBOOT" Value="{{RebootMode}}" />
    <!-- Installlation level to use that determines which features are installed.
    see guides/enduser.md to check which Install Level maps to which feature that will
    correspondingly get installed.
    If not set, this will default to "2" (Main Feature, Launch On Login) -->
    <Property Id="INSTALLLEVEL" Value="{{InstallLevel}}" />
    <!-- Allows to customize the Windows user group that gets access rights on
    the install folder in cas the auto-updater is installed. User that run the App
    must be part of that user group to be able to auto-update. -->
    <Property Id="UPDATERUSERGROUP" Value="Users" />
    <!-- A property to define whether the auto-updater is enabled when the
    feature gets installed. This way the update can be installed but also be disabled
    by overwriting the default value.  -->
    <Property Id="AUTOUPDATEENABLED" Value="1" />
    <!-- Necessary registry search to find the install path which is used by the
    PurgeOnUninstall action. Since this package can be installed perUser or perMachine,
    we have to look in both places. First successful search wins. -->
    <Property Id="INSTALLPATH">
      <RegistrySearch Key="SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{{{ProductCode}}}.msq"
                      Root="HKCU" 
                      Type="raw"
                      Id="INSTALLPATH_REGSEARCH_HKCU"
                      Name="InstallPath"
                      Win64="{{Win64YesNo}}"/>
      <RegistrySearch Key="SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{{{ProductCode}}}.msq"
                      Root="HKLM" 
                      Type="raw"
                      Id="INSTALLPATH_REGSEARCH_HKLM"
                      Name="InstallPath"
                      Win64="{{Win64YesNo}}"/>
    </Property>
    <!-- Lets change the product name depending on the perUser installMode. 
    This way the user and admins can see in which scope the MSI was installed. -->
    <SetProperty Action="SetVisibleProductName" Id="VisibleProductName" Sequence="both" Before="AppSearch"  Value="{{ApplicationName}} (User)">
      <![CDATA[MSIINSTALLPERUSER = "1"]]>
    </SetProperty>
    <!-- Again we give thee MSI generaten entry a slightly different name to help 
    differentiate between the public one and the invisible one. -->
    <SetProperty Action="SetProductName" Id="ProductName" Sequence="both" Before="AppSearch"  Value="{{ApplicationName}} (User - MSI)">
      <![CDATA[MSIINSTALLPERUSER = "1"]]>
    </SetProperty>

    <Media Id="1" Cabinet="product.cab" EmbedCab="yes"/>
<!-- {{Icon}}-->
<!-- {{UI}} -->

    <!-- Step 2: Add files and directories -->
    <Directory Id="TARGETDIR" Name="SourceDir">
       <!-- Installation files to %PROGRAMFILES% -->
      <Directory Id="{{ProgramFilesFolder}}">
<!-- {{Directories}} -->
      </Directory>

      <!-- Desktop -->
      <Directory Id="DesktopFolder" Name="Desktop" />

      <!-- Start Menu -->
      <Directory Id="ProgramMenuFolder">
        <Directory Id="ApplicationProgramsFolder" Name="{{ShortcutFolderName}}"/>
      </Directory>
    </Directory>

    <!-- Step 3: Add app to Start Menu -->
    <DirectoryRef Id="ApplicationProgramsFolder">
      <Component Id="ApplicationShortcut" Guid="{{ApplicationShortcutGuid}}" Win64="{{Win64YesNo}}">
        <Shortcut Id="ApplicationStartMenuShortcut"
                  Name="{{ShortcutName}}"
                  Description="{{ApplicationDescription}}"
                  Target="[APPLICATIONROOTDIRECTORY]{{ApplicationBinary}}.exe"
                  WorkingDirectory="APPLICATIONROOTDIRECTORY">
<!-- {{ShortcutProperties}} -->
        </Shortcut>
        <RemoveFolder Id="ApplicationProgramsFolder" On="uninstall"/>
        <RegistryValue Root="HKCU"
                       Key="Software\Microsoft\{{ApplicationShortName}}"
                       Name="installed"
                       Type="integer"
                       Value="1"
                       KeyPath="yes"/>
      </Component>
    </DirectoryRef>

   <!-- Step 4: Add app desktop shortcut --> 
    <DirectoryRef Id="DesktopFolder">
      <Component Id="DesktopShortcut" Guid="{{DesktopShortcutGuid}}" >
          <Shortcut Id="MyDesktopShortcut"
                    Name="{{ShortcutName}}"
                    Description="{{ApplicationDescription}}"
                    Target="[APPLICATIONROOTDIRECTORY]{{ApplicationBinary}}.exe"
                    WorkingDirectory="APPLICATIONROOTDIRECTORY"/>
          <RegistryValue Root="HKCU"
                    Key="Software\Microsoft\{{ApplicationShortName}}"
                    Name="installed"
                    Type="integer"
                    Value="1"
                    KeyPath="yes" />
      </Component>
    </DirectoryRef>

<!-- {{AutoUpdatePermissions}} -->

    <!-- Lets cleanup any files that are were not part of the initial install
    via this MSI. Such as newer versions installed by the auto-updater. -->
    <DirectoryRef Id="APPLICATIONROOTDIRECTORY">
      <Component Id="PurgeOnUninstall" Guid="{{RandomGuid}}" Win64="{{Win64YesNo}}">
        <CreateFolder/>
        <util:RemoveFolderEx On="uninstall" Property="INSTALLPATH" />
      </Component>
    </DirectoryRef>

    <Feature Id="Complete" Title="{{ApplicationName}} ({{SemanticVersion}})" Description="The complete package." Display="expand" Level="1" {{ConfigurableDirectory}}>
      <!-- Step 5: Tell WiX to install the files -->
      <Feature Id="MainApplication" Title="Main Application" Level="1" Description="The main components to run the applications." >
<!-- {{ComponentRefs}} -->
        <ComponentRef Id="ApplicationShortcut" />
        <ComponentRef Id="DesktopShortcut" />
        <ComponentRef Id="PurgeOnUninstall" />
      </Feature> 
<!-- {{AutoLaunchFeature}} -->
<!-- {{AutoUpdateFeature}} -->
    </Feature> 
<!-- {{AutoRun}} -->
  </Product>
</Wix>
