<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://wixtoolset.org/schemas/v4/wxs"
  xmlns:ui="http://wixtoolset.org/schemas/v4/wxs/ui"
  xmlns:util="http://wixtoolset.org/schemas/v4/wxs/util">

  <Package Name="Fibratus"
      Version="$(var.VERSION)"
      Manufacturer="Nedim Sabic Sabic &lt;https://www.fibratus.io&gt;"
      UpgradeCode="E81B8794-3277-47F9-BE58-2EF2D20930EC"
      InstallerVersion="200"
      Compressed="yes"
      Language="1033">
    <SummaryInformation Description="Fibratus Installer" />

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

    <MajorUpgrade Schedule="afterInstallInitialize" DowngradeErrorMessage="A later version of [ProductName] is already installed" AllowSameVersionUpgrades="yes" />

    <UI Id="UI">
      <ui:WixUI Id="WixUI_InstallDir" InstallDirectory="INSTALLDIR" />
    </UI>

    <!-- Custom banners -->
    <WixVariable Id="WixUIDialogBmp" Value="background.png" /> <!-- Background bitmap used on the welcome and completion dialogs, should be 493 × 312 pixels -->
    <WixVariable Id="WixUIBannerBmp" Value="banner.png" />  <!-- Top banner, should be 493 × 58 pixels -->

    <Property Id="WIXUI_EXITDIALOGOPTIONALTEXT" Value="You're all set up! Fibratus is running and securing your endpoint." />

    <!-- License -->
    <WixVariable Id="WixUILicenseRtf" Value="LICENSE.rtf"/>

    <!--Files -->
    <ComponentGroup Id="Files" Directory="INSTALLDIR">
      <Files Include="!(bindpath.dir)**">
        <!-- Exclude from harvesting as its need fine-grained authoring in Windows Service -->
        <Exclude Files="!(bindpath.dir)Bin\fibratus.exe" />
      </Files>

      <!-- Windows Service -->
      <Component Directory="BINDIR" Guid="F2C06EDD-C830-4FCD-BAFA-0D15C697EE76">
        <File Source="!(bindpath.dir)Bin\fibratus.exe" />
        <ServiceInstall Name="Fibratus" DisplayName="Fibratus"
            Description="Adversary tradecraft detection, protection, and hunting"
            Account="LocalSystem"
            Type="ownProcess"
            Start="auto"
            ErrorControl="normal" Vital="yes">
          <util:ServiceConfig FirstFailureActionType="restart" SecondFailureActionType="restart"
              ThirdFailureActionType="restart" RestartServiceDelayInSeconds="60" />
        </ServiceInstall>
        <ServiceControl Id="Fibratus" Name="Fibratus" Start="install" Stop="both" Remove="uninstall" Wait="yes" />
      </Component>
    </ComponentGroup>

    <StandardDirectory Id="ProgramFiles64Folder">
      <Directory Id="INSTALLDIR" Name="Fibratus">
        <Component Id="Path" KeyPath="yes" Guid="E81B8794-3277-47F9-BE58-2EF2D20930EC">
          <Environment Id="PATH" Name="PATH" Value="[INSTALLDIR]Bin" Permanent="no" Part="last" Action="set" System="yes" />
        </Component>
        <Directory Id="BINDIR" Name="Bin" />
      </Directory>
    </StandardDirectory>

    <Feature Id="Fibratus" Description="Fibratus" Level="1" ConfigurableDirectory="INSTALLDIR">
      <ComponentGroupRef Id="Files" />
      <ComponentRef Id="Path" />
    </Feature>

    <!-- Enable recovery actions for the Windows Service -->
    <Property Id="ConfigureServiceRecovery" Value="&quot;SC.EXE&quot; failureflag fibratus 1" />
    <CustomAction Id="ConfigureServiceRecovery" BinaryRef="Wix4UtilCA_$(sys.BUILDARCHSHORT)" DllEntry="WixQuietExec" Impersonate="no" Execute="deferred" Return="ignore" />

    <util:CloseApplication Id="CloseSystray" CloseMessage="yes" Target="fibratus-systray.exe" RebootPrompt="no" />

    <InstallExecuteSequence>
      <Custom Action="ConfigureServiceRecovery" After="InstallServices" Condition="NOT REMOVE" />
    </InstallExecuteSequence>

  </Package>
</Wix>
