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

<?define UPGRADECODE="4d704a0b-91a2-4a2d-a3e6-885d91bccc97"?>

<!-- Most of the strings in this file starting with __... are placeholders,
     which are rewritten by the repacker. -->

<!-- Long filler text rewritten by the repacker, for padding purposes. -->
<?define FILLER="____________________________________________________________________________________________________" ?>
<?define FILLER5="$(var.FILLER)$(var.FILLER)$(var.FILLER)$(var.FILLER)$(var.FILLER)" ?>

<Wix
    xmlns="http://schemas.microsoft.com/wix/2006/wi"
    xmlns:util="http://schemas.microsoft.com/wix/UtilExtension">
  <!-- Product.Id will be replaced at repackaging time. -->
  <Product
      Name="__ProductName"
      Id="{66666666-6666-6666-6666-666666666666}"
      UpgradeCode="$(var.UPGRADECODE)"
      Language="1033"
      Version="$(var.VERSION)"
      Manufacturer="__ProductManufacturer">

    <!-- Package.Id will be replaced at repackaging time. -->
    <Package
        Id="{77777777-7777-7777-7777-777777777777}"
        Keywords="Installer"
        Description="Installer"
        InstallerVersion="200"
        InstallScope="perMachine"
        Languages="1033"
        Compressed="yes" />

    <!-- Allow rolling back to old versions. -->
    <!-- If an upgrade fails, roll back to existing version -->
    <MajorUpgrade AllowDowngrades="yes" Schedule="afterInstallInitialize" />

    <Property Id="Dummy" Value="MagicPropertyValueForPadding$(var.FILLER5)$(var.FILLER5)$(var.FILLER5)$(var.FILLER5)$(var.FILLER5)" />
    <!-- This is a deprecated string that used to be replaced at repacking time.
         Keeping it for compatibility with old repackers. -->
    <Property Id="Dummy2" Value="|__GrrVersion" />

    <Media Id="1" Cabinet="grr.cab" EmbedCab="yes" />

    <!-- Installation directories. -->
    <Directory Id="TARGETDIR" Name="SourceDir" DiskId="1">
      <Directory Id="System64Folder">
        <Directory Id="Grr" Name="__GrrDirectory">
          <Directory Id="INSTALLDIR" Name="$(var.VERSION)">
            <Component Id="CompDbgGrrExe" Guid="*">
              <File Id="FileDbgGrrExe" Source="$(var.InputDir)\dbg_grr-client.exe" Name="__dbg_grr-client.exe" />
            </Component>
            <Component Id="CompGrrExe">
               <File Id="FileGrrExe" Source="$(var.InputDir)\grr-client.exe" Name="__grr-client.exe" />
            </Component>
            <Component Id="CompGrrConfig">
              <File Id="FileGrrConfig" Source="$(var.InputDir)\grr-config.yaml" Name="grr-config.yaml" />
            </Component>
            <Component Id="CompFleetspeakServiceConfig">
              <File Id="FileFleetspeakServiceConfig" Source="$(var.InputDir)\fleetspeak-service-config.txt" Name="fleetspeak-service-config.txt" />
            </Component>
            <Component Id="CompFleetspeakServiceRestart">
              <!-- Having only a ServiceControl in a Component (without any artifact such as a File) doesn't seem to work. -->
              <File Id="RestartDummyFile" Source="$(var.InputDir)\restart-dummy.txt" Name="restart.txt" />
              <ServiceControl
                  Id="ServiceControlFleetspeakServiceConfig"
                  Name="FleetspeakClientService"
                  Start="both"
                  Stop="both"
                  Wait="no" />
            </Component>
            <Component Id="CompFleetspeakServiceReg" Guid="*">
              <RegistryKey Root="HKLM" Key="__FleetspeakServiceRegistryKey">
                <RegistryValue Type="string" Name="__FleetspeakServiceRegistryName" KeyPath="yes" Value="__FleetspeakServiceRegistryValue" />
              </RegistryKey>
            </Component>
            <Component Id="CompPaddingFile" Guid="*">
              <File Id="PaddingFile" Source="$(var.InputDir)\padding-file.bin" Name="padding-file.bin" />
            </Component>
            <Directory Id="FleetspeakInstallDir" Name="Fleetspeak">
              <Component Id="CompFleetspeakConfig">
                <File Id="FileFleetspeakConfig" Source="$(var.InputDir)\fleetspeak-client.config" Name="fleetspeak-client.config" />
              </Component>
            </Directory>
          </Directory>
        </Directory>
      </Directory>
      <Directory Id="FleetspeakLogDir" />
    </Directory>

    <CustomAction Id="GenFleetspeakServiceFile" ExeCommand="__GenFleetspeakServiceFileCmd" Directory="INSTALLDIR" Impersonate="no" Return="check" Execute="commit"/>

    <Feature Id="GrrBase" Level="1">
      <ComponentGroupRef Id="CompGrrAutoFiles" />
      <ComponentRef Id="CompGrrConfig"/>
    </Feature>

    <Feature Id="GrrExe" Level="0">
      <ComponentRef Id="CompGrrExe"/>
    </Feature>

    <Feature Id="DbgGrrExe" Level="0">
      <ComponentRef Id="CompDbgGrrExe"/>
    </Feature>

    <Feature Id="FleetspeakClient" Level="0">
      <ComponentGroupRef Id="FleetspeakComponentGroup" />
      <ComponentRef Id="CompFleetspeakConfig" />
    </Feature>

    <Feature Id="FleetspeakServiceRegistryEntry" Level="0">
      <ComponentRef Id="CompFleetspeakServiceReg"/>
      <ComponentRef Id="CompFleetspeakServiceConfig"/>
    </Feature>

    <Feature Id="FleetspeakServiceRestart" Level="0">
      <ComponentRef Id="CompFleetspeakServiceRestart"/>
    </Feature>

    <Feature Id="PaddingFile" Level="0">
      <ComponentRef Id="CompPaddingFile"/>
    </Feature>

    <InstallExecuteSequence>
      <Custom Action="GenFleetspeakServiceFile" After="InstallFiles">
        NOT Installed AND NOT REMOVE
      </Custom>
      <Custom Action="WixCloseApplications" Before="RemoveFiles" />
    </InstallExecuteSequence>

    <!-- Explicitly close executables before removing files. This should avoid the retry close application loop. Fleetspeak should be closed first to avoid it restarting GRR. -->
    <util:CloseApplication Id="CloseFleetspeakClient" Target="fleetspeak-client.exe" PromptToContinue="no" RebootPrompt="no" />
    <util:CloseApplication Id="CloseGRR" Target="__grr-client.exe" PromptToContinue="no" RebootPrompt="no" />
    <util:CloseApplication Id="CloseDbgGrr" Target="__dbg_grr-client.exe" PromptToContinue="no" RebootPrompt="no" />

  </Product>
</Wix>
