<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
  <!--
    MSI wrapper around the compiled NSIS setup.exe.
    Runs the bundled installer with the same silent in-app update flags used by
    src/desktop/winappupdate.js: /S /UPDATE /RESTARTFC /LOG=...

    ALLUSERS=2 + MSIINSTALLPERUSER=1 defaults to per-user (matching NSIS) and is
    not blocked by DisableMsi=1 / error 1625 the way a pure InstallScope=perUser
    package is.
  -->
  <Product
    Id="*"
    Name="Farm Control"
    Language="1033"
    Version="__VERSION__"
    Manufacturer="Tom Butcher"
    UpgradeCode="__UPGRADE_CODE__">
    <Package
      InstallerVersion="500"
      Compressed="yes"
      InstallPrivileges="limited"
      Platform="x64" />

    <Condition Message="Windows 7 and above is required"><![CDATA[Installed OR VersionNT >= 601]]></Condition>

    <MajorUpgrade
      AllowSameVersionUpgrades="yes"
      DowngradeErrorMessage="A newer version of [ProductName] is already installed." />
    <MediaTemplate EmbedCab="yes" CompressionLevel="high" />

    <Icon Id="InstallerIcon" SourceFile="__INSTALLER_ICON__" />
    <Property Id="ARPPRODUCTICON" Value="InstallerIcon" />

    <Property Id="DISABLEADVTSHORTCUTS" Value="1" />
    <Property Id="ALLUSERS" Secure="yes" Value="2" />
    <Property Id="MSIINSTALLPERUSER" Secure="yes" Value="1" />
    <Property Id="REBOOT" Value="ReallySuppress" />

    <Binary Id="WrappedExe" SourceFile="__SETUP_EXE__" />

    <!-- ExeCommand is a Formatted field, so [FarmControlUpdatesDir] resolves to the
         real per-user path here. An intermediate Property would not resolve it
         (property values are not recursively formatted). -->
    <CustomAction
      Id="RunInstaller"
      BinaryKey="WrappedExe"
      ExeCommand="/S /UPDATE /RESTARTFC /LOG=&quot;[FarmControlUpdatesDir]install.log&quot;"
      Execute="immediate"
      Impersonate="yes"
      Return="check" />

    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="LocalAppDataFolder">
        <Directory Id="FarmControlAppDataDir" Name="FarmControl">
          <Directory Id="FarmControlUpdatesDir" Name="Updates">
            <Component Id="UpdatesFolderComponent" Guid="A1B2C3D4-E5F6-7890-ABCD-EF1234567890">
              <CreateFolder />
              <!-- ICE64: per-user directories must be scheduled for removal on uninstall. -->
              <RemoveFile Id="RemoveUpdatesFiles" Directory="FarmControlUpdatesDir" Name="*" On="uninstall" />
              <RemoveFolder Id="RemoveFarmControlUpdatesDir" Directory="FarmControlUpdatesDir" On="uninstall" />
              <RemoveFolder Id="RemoveFarmControlAppDataDir" Directory="FarmControlAppDataDir" On="uninstall" />
              <RegistryValue
                Root="HKCU"
                Key="Software\Tom Butcher\Farm Control"
                Name="UpdatesDir"
                Type="string"
                Value="[FarmControlUpdatesDir]"
                KeyPath="yes" />
            </Component>
          </Directory>
        </Directory>
      </Directory>
      <Component Id="PerUserMarker" Guid="8A3F2E1D-9C4B-4A7E-B6D5-1F0E3C2B4A59">
        <RegistryValue
          Root="HKCU"
          Key="Software\Tom Butcher\Farm Control"
          Name="MsiWrapper"
          Type="integer"
          Value="1"
          KeyPath="yes" />
      </Component>
    </Directory>

    <Feature Id="MainFeature" Title="Farm Control" Level="1">
      <ComponentRef Id="UpdatesFolderComponent" />
      <ComponentRef Id="PerUserMarker" />
    </Feature>

    <InstallExecuteSequence>
      <Custom Action="RunInstaller" After="InstallFiles">NOT Installed</Custom>
    </InstallExecuteSequence>
  </Product>
</Wix>
