<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
  <Product
    Id="__PRODUCT_ID__"
    Name="Farm Control"
    Language="1033"
    Version="__VERSION__"
    Manufacturer="Tom Butcher"
    UpgradeCode="__UPGRADE_CODE__">
    <Package
      InstallerVersion="500"
      Compressed="yes"
      InstallScope="perUser"
      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="PREVIOUS_UNINSTALL_CMD">
    <RegistrySearch
      Id="PreviousUninstallCmdSearch"
      Root="HKCU"
      Key="Software\Microsoft\Windows\CurrentVersion\Uninstall\Farm Control"
      Name="UninstallString"
      Type="raw" />
  </Property>

  <Property Id="PREVIOUS_INSTALL_DIR">
    <RegistrySearch
      Id="PreviousInstallDirSearch"
      Root="HKCU"
      Key="Software\Tom Butcher\Farm Control"
      Name="InstallDir"
      Type="directory" />
  </Property>

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

    <CustomAction
      Id="KillFarmControl"
      BinaryKey="WixCA"
      DllEntry="WixQuietExec"
      Execute="immediate"
      Impersonate="yes"
      Return="ignore" />

    <CustomAction
      Id="UninstallPrevious"
      BinaryKey="WixCA"
      DllEntry="WixQuietExec"
      Execute="immediate"
      Impersonate="yes"
      Return="ignore" />

    <CustomAction
      Id="RunInstaller"
      BinaryKey="WrappedExe"
      ExeCommand="/S"
      Execute="immediate"
      Impersonate="yes"
      Return="check" />

    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="TempFolder" Name="Temp">
        <Component Id="EmptyComponent" Guid="8A3F2E1D-9C4B-4A7E-B6D5-1F0E3C2B4A59">
          <CreateFolder />
        </Component>
      </Directory>
    </Directory>

    <Feature Id="EmptyFeature" Level="0">
      <ComponentRef Id="EmptyComponent" />
    </Feature>

    <CustomAction
      Id="SetKillFarmControl"
      Property="QtExecCmdLine"
      Value="taskkill.exe /F /IM launcher.exe /T"
      Execute="immediate" />

    <CustomAction
      Id="SetUninstallPreviousFromRegistry"
      Property="QtExecCmdLine"
      Value="&quot;[PREVIOUS_UNINSTALL_CMD]&quot; /S"
      Execute="immediate" />

    <CustomAction
      Id="SetUninstallPreviousFromInstallDir"
      Property="QtExecCmdLine"
      Value="&quot;[PREVIOUS_INSTALL_DIR]Uninstall.exe&quot; /S"
      Execute="immediate" />

    <InstallExecuteSequence>
      <Custom Action="SetKillFarmControl" Before="KillFarmControl">NOT Installed</Custom>
      <Custom Action="KillFarmControl" After="InstallInitialize">NOT Installed</Custom>
      <Custom Action="SetUninstallPreviousFromRegistry" Before="UninstallPrevious">NOT Installed AND PREVIOUS_UNINSTALL_CMD</Custom>
      <Custom Action="SetUninstallPreviousFromInstallDir" Before="UninstallPrevious">NOT Installed AND NOT PREVIOUS_UNINSTALL_CMD AND PREVIOUS_INSTALL_DIR</Custom>
      <Custom Action="UninstallPrevious" Before="RunInstaller">NOT Installed AND (PREVIOUS_UNINSTALL_CMD OR PREVIOUS_INSTALL_DIR)</Custom>
      <Custom Action="RunInstaller" After="KillFarmControl">NOT Installed</Custom>
    </InstallExecuteSequence>
  </Product>
</Wix>
