<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"
     xmlns:fw="http://schemas.microsoft.com/wix/FirewallExtension">
  <?if $(sys.BUILDARCH)=x64 ?>
    <?define PlatformProgramFiles = "ProgramFiles64Folder" ?>
  <?else ?>
    <?define PlatformProgramFiles = "ProgramFilesFolder" ?>
  <?endif ?>

  <Product Id="*" UpgradeCode="66a6eb5b-1fc2-4b14-a362-5ceec6413308" 
           Name="WMI Exporter" Version="$(var.Version)" Manufacturer="Martin Lindhe" 
           Language="1033" Codepage="1252">
    <Package Id="*" Manufacturer="Martin Lindhe" InstallScope="perMachine" 
             Description="WMI Exporter $(var.Version) installer" Compressed="yes" />
    <Media Id="1" Cabinet="wmi_exporter.cab" EmbedCab="yes"/>
    <MajorUpgrade Schedule="afterInstallExecute" DowngradeErrorMessage="A later version of [ProductName] is already installed. Setup will now exit." />

    <Property Id="ENABLED_COLLECTORS" Secure="yes"/>
    <SetProperty Id="CollectorsFlag" After="InstallFiles" Sequence="execute" Value="-collectors.enabled [ENABLED_COLLECTORS]">ENABLED_COLLECTORS</SetProperty>

    <Property Id="LISTEN_ADDR" Secure="yes" />
    <Property Id="LISTEN_PORT" Secure="yes" Value="9182" />
    <SetProperty Id="ListenFlag" After="InstallFiles" Sequence="execute" Value="-telemetry.addr [LISTEN_ADDR]:[LISTEN_PORT]">LISTEN_ADDR OR LISTEN_PORT</SetProperty>

    <Property Id="METRICS_PATH" Secure="yes"/>
    <SetProperty Id="MetricsPathFlag" After="InstallFiles" Sequence="execute" Value="-telemetry.path [METRICS_PATH]">METRICS_PATH</SetProperty>

    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="$(var.PlatformProgramFiles)">
        <Directory Id="APPLICATIONROOTDIRECTORY" Name="wmi_exporter" />
      </Directory>
    </Directory>

    <ComponentGroup Id="Files" Directory="APPLICATIONROOTDIRECTORY">
      <Component>
        <File Id="wmi_exporter.exe" Name="wmi_exporter.exe" Source="Work\wmi_exporter.exe" KeyPath="yes">
          <fw:FirewallException Id="MetricsEndpoint" Name="WMI Exporter (HTTP [LISTEN_PORT])" Description="WMI Exporter HTTP endpoint" Port="[LISTEN_PORT]" Protocol="tcp" Scope="any" IgnoreFailure="yes" />
        </File>
        <ServiceInstall Id="InstallExporterService" Name="wmi_exporter" DisplayName="WMI exporter" Description="Exports Prometheus metrics from WMI queries" ErrorControl="normal" Start="auto" Type="ownProcess" Arguments="-log.format logger:eventlog?name=wmi_exporter [CollectorsFlag] [ListenFlag] [MetricsPathFlag]" />
        <ServiceControl Id="ServiceStateControl" Name="wmi_exporter" Remove="uninstall" Start="install" Stop="both" />
      </Component>
    </ComponentGroup>

    <Feature Id="DefaultFeature" Level="1">
      <ComponentGroupRef Id="Files" />
    </Feature>
  </Product>
</Wix>