<?xml version="1.0"?>

<?ifndef Version?>
<?error Version must be defined?>
<?endif?>
<?if $(sys.BUILDARCH) = x64 or $(sys.BUILDARCH) = arm64 ?>
<?define PlatformProgramFilesFolder = "ProgramFiles64Folder" ?>
<?else ?>
<?define PlatformProgramFilesFolder = "ProgramFilesFolder" ?>
<?endif?>

<!--
  ~ SPDX-License-Identifier: Apache-2.0
  ~
  ~ Copyright The Prometheus Authors
  ~ Licensed under the Apache License, Version 2.0 (the "License");
  ~ you may not use this file except in compliance with the License.
  ~ You may obtain a copy of the License at
  ~
  ~ http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
  -->

<Wix xmlns:fw="http://wixtoolset.org/schemas/v4/wxs/firewall"
     xmlns:ui="http://wixtoolset.org/schemas/v4/wxs/ui"
     xmlns="http://wixtoolset.org/schemas/v4/wxs">
    <Package UpgradeCode="66a6eb5b-1fc2-4b14-a362-5ceec6413308" Name="$(var.ProductName)" Version="$(var.Version)"
             Manufacturer="prometheus-community" Language="1033" Scope="perMachine">
        <SummaryInformation Manufacturer="prometheus-community" Description="$(var.ProductName) $(var.Version) installer" />

        <Upgrade Id="66a6eb5b-1fc2-4b14-a362-5ceec6413308">
            <UpgradeVersion IncludeMinimum="no"
                            Minimum="$(var.Version)"
                            OnlyDetect="no"
                            Property="NEWERVERSIONDETECTED" />
            <UpgradeVersion IncludeMaximum="yes"
                            IncludeMinimum="yes"
                            Maximum="$(var.Version)"
                            Minimum="0.0.0.0"
                            Property="OLDERVERSIONBEINGUPGRADED" />
        </Upgrade>

        <Media Id="1" Cabinet="windows_exporter.cab" EmbedCab="yes"  />
        <MajorUpgrade Schedule="afterInstallInitialize" DowngradeErrorMessage="A later version of [ProductName] is already installed. Setup will now exit." AllowSameVersionUpgrades="yes" />

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

        <Property Id="EXTRA_FLAGS" Secure="yes" />
        <SetProperty Id="ExtraFlags" After="InstallFiles" Sequence="execute" Value="[EXTRA_FLAGS]" Condition="EXTRA_FLAGS" />

        <Property Id="CONFIG_FILE" Secure="yes" Value="config.yaml" />
        <SetProperty Id="ConfigFile_NonDefault" After="InstallFiles" Sequence="execute" Value="[CONFIG_FILE]" Condition="CONFIG_FILE AND CONFIG_FILE&lt;&gt;&quot;config.yaml&quot;" />
        <SetProperty Id="ConfigFile_Default" After="InstallFiles" Sequence="execute" Value="[APPLICATIONFOLDER]config.yaml" Condition="CONFIG_FILE=&quot;config.yaml&quot;" />
        <SetProperty Id="ConfigFileFlag" After="InstallFiles" Sequence="execute" Value="--config.file=&quot;[ConfigFile_NonDefault][ConfigFile_Default]&quot;" Condition="ConfigFile_NonDefault OR ConfigFile_Default" />

        <Property Id="LISTEN_PORT" Secure="yes" Value="9182" />
        <SetProperty Id="ListenFlag" After="InstallFiles" Sequence="execute" Value="--web.listen-address=&quot;[LISTEN_ADDR]:[LISTEN_PORT]&quot;" Condition="LISTEN_ADDR&lt;&gt;&quot;&quot; OR LISTEN_PORT&lt;&gt;9182" />

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

        <Property Id="REMOTE_ADDR" Secure="yes" />
        <SetProperty Id="RemoteAddressFlag" After="InstallFiles" Sequence="execute" Value="[REMOTE_ADDR]" Condition="REMOTE_ADDR" />

        <Property Id="TEXTFILE_DIRS" Secure="yes" />
        <SetProperty Id="TextfileDirsFlag" After="InstallFiles" Sequence="execute" Value="--collector.textfile.directories=&quot;[TEXTFILE_DIRS]&quot;" Condition="TEXTFILE_DIRS" />

        <!-- Configuration for how the installer shows in Add/Remove Programs. -->
        <Icon Id="icon.ico" SourceFile=".\icon.ico"/>
        <Property Id="ARPPRODUCTICON" Value="icon.ico" />
        <Property Id="ARPHELPLINK" Value="https://github.com/prometheus-community/windows_exporter/issues" />
        <Property Id="ARPURLINFOABOUT" Value="https://github.com/prometheus-community/windows_exporter" />
        <Property Id="ARPCOMMENTS" Value="Prometheus exporter for Windows machines" />
        <!-- Disable the repair option; the functionality is available through Change instead. -->
        <Property Id="ARPNOREPAIR" Value="1" />
        <Property Id="ARPSIZE" Value="10000" />

        <Property Id="START_MENU_FOLDER" Value="0" />
        <Property Id="NOSTART" Value="0" />

        <CustomAction Id="CheckExtraFlags"
                      Error="The parameter '--config.file' must not be included in EXTRA_FLAGS. Use CONFIG_FILE instead. Please remove it and try again." />

        <CustomAction Id="set_maintenance" Property="MAINTENANCE" Value="true" />

        <!-- Set to reinstall all features. -->
        <CustomAction Id="set_reinstall_all_property"
                      Property="REINSTALL"
                      Value="ALL" />
        <!-- "amus" will force reinstall all files.
             See https://docs.microsoft.com/en-us/windows/desktop/Msi/reinstallmode -->
        <CustomAction Id="set_reinstallmode_property"
                      Property="REINSTALLMODE"
                      Value="amus" />
        <!-- START CUSTOM ACTION FOR CONFIG FILE CREATION -->
        <SetProperty
            Id="CreateConfigFile"
            Value="&quot;[%ComSpec]&quot; /c TYPE NUL >>&quot;[ConfigFile_NonDefault][ConfigFile_Default]&quot;"
            Before="CreateConfigFile"
            Sequence="execute"
        />
        <CustomAction
            Id="CreateConfigFile"
            BinaryRef="Wix4UtilCA_$(sys.BUILDARCHSHORT)"
            DllEntry="WixQuietExec"
            Execute="deferred"
            Return="check"
            Impersonate="no"
        />
        <!-- END CUSTOM ACTION FOR CONFIG FILE CREATION -->

        <!-- START CUSTOM ACTION FOR KILLING THE PROCESS -->
        <SetProperty
            Id="KillProcess"
            Value="&quot;[WindowsFolder]System32\taskkill.exe&quot; /T /F /IM windows_exporter.exe"
            Before="KillProcess"
            Sequence="execute"
        />
        <CustomAction
            Id="KillProcess"
            BinaryRef="Wix4UtilCA_$(sys.BUILDARCHSHORT)"
            DllEntry="WixQuietExec"
            Execute="deferred"
            Return="ignore"
            Impersonate="no"
        />
        <!-- END CUSTOM ACTION FOR KILLING THE PROCESS -->

        <!-- START CUSTOM ACTION FOR SET SERVICE FAILUREFLAG -->
        <SetProperty
            Id="ConfigureServiceRecovery"
            Value="&quot;[WindowsFolder]System32\sc.exe&quot; failureflag &quot;windows_exporter&quot; 1"
            Before="ConfigureServiceRecovery"
            Sequence="execute"
        />
        <CustomAction
            Id="ConfigureServiceRecovery"
            BinaryRef="Wix4UtilCA_$(sys.BUILDARCHSHORT)"
            DllEntry="WixQuietExec"
            Execute="deferred"
            Return="ignore"
            Impersonate="no"
        />
        <!-- END CUSTOM ACTION FFOR SET SERVICE FAILUREFLAG -->

        <InstallExecuteSequence>
            <!-- Set REINSTALL=all and REINSTALLMODE=amus if the user reruns the
                 MSI, which will force reinstalling all files and services.  -->
            <Custom Action="set_maintenance" Before="set_reinstall_all_property"
                    Condition="Installed AND (NOT REMOVE) AND (NOT UPGRADINGPRODUCTCODE)"/>
            <Custom Action="set_reinstall_all_property" Before="set_reinstallmode_property" Condition="MAINTENANCE"/>
            <Custom Action="set_reinstallmode_property" Before="LaunchConditions" Condition="MAINTENANCE"/>
            <Custom Action="CreateConfigFile" Before="InstallServices" Condition="ConfigFile_Default" />
            <Custom Action="ConfigureServiceRecovery" After="InstallServices" Condition="NOT REMOVE" />
            <Custom Action="KillProcess" Before="RemoveFiles" />

            <Custom Action="CheckExtraFlags" Before="InstallInitialize"
                    Condition="EXTRA_FLAGS AND (EXTRA_FLAGS&gt;&lt;&quot;--config.file&quot;)" />
        </InstallExecuteSequence>

        <Feature
            Id="DefaultFeature"
            Level="1"
            Title="$(var.ProductName) $(var.Version)"
            Description="The binary and configuration files for $(var.ProductName)"
            Display="expand"
            ConfigurableDirectory="APPLICATIONFOLDER"
            AllowAdvertise="no"
            InstallDefault="local"
            AllowAbsent="no"
        >
            <ComponentGroupRef Id="CG_Files" />

            <Feature
                Id="FirewallException"
                Level="2"
                Title="Firewall Exception"
                Description="Allow $(var.ProductName) to listen on a port"
                Display="expand"
                AllowAdvertise="no"
                AllowAbsent="yes">
                <ComponentGroupRef Id="CG_FirewallException" />
            </Feature>
        </Feature>

        <UI Id="FeatureTree">
            <ui:WixUI Id="WixUI_FeatureTree" />
            <UIRef Id="WixUI_ErrorProgressText" />
            <Dialog Id="CustomPropertiesDlg" Width="370" Height="270" Title="windows_exporter configuration">
                <Control Id="BannerBitmap" Type="Bitmap" X="0" Y="0" Width="370" Height="44" TabSkip="no" Text="!(loc.CustomizeDlgBannerBitmap)" />
                <!--<Control Id="Text" Type="Text" X="25" Y="55" Width="320" Height="20" Text="Text" />-->
                <Control Id="BannerLine" Type="Line" X="0" Y="44" Width="370" Height="0" />
                <Control Id="BottomLine" Type="Line" X="0" Y="234" Width="370" Height="0" />
                <Control Id="Description" Type="Text" X="25" Y="23" Width="280" Height="15" Transparent="yes" NoPrefix="yes" Text="This pages contains configuration related to windows_exporter" />
                <Control Id="Title" Type="Text" X="15" Y="6" Width="210" Height="15" Transparent="yes" NoPrefix="yes" Text="{\WixUI_Font_Title}windows_exporter configuration" />

                <!-- Edit box for property input -->
                <!-- cpu,logical_disk,physical_disk,net,os,service,system -->
                <Control Id="PropertyEdit_ENABLED_COLLECTORS_Title1" Type="Text" X="25" Y="55" Width="300" Height="15" Transparent="yes" NoPrefix="yes" Text="Comma-separated list of collectors to use. Use '[\[]defaults[\]]' as a placeholder for all" />
                <Control Id="PropertyEdit_ENABLED_COLLECTORS_Title2" Type="Text" X="25" Y="65" Width="300" Height="15" Transparent="yes" NoPrefix="yes" Text="the collectors enabled by default. If value is empty, the exporter default will be used." />
                <Control Id="PropertyEdit_ENABLED_COLLECTORS" Type="Edit" X="24" Y="77" Width="300" Height="18" Property="ENABLED_COLLECTORS" Text="[ENABLED_COLLECTORS]" Indirect="no" />

                <Control Id="PropertyEdit_EXTRA_FLAGS_Title" Type="Text" X="25" Y="100" Width="300" Height="15" Transparent="yes" NoPrefix="yes" Text="Additional command line flags" />
                <Control Id="PropertyEdit_EXTRA_FLAGS" Type="Edit" X="24" Y="112" Width="300" Height="18" Property="EXTRA_FLAGS" Text="[EXTRA_FLAGS]" Indirect="no" />

                <Control Id="PropertyEdit_LISTEN_PORT_Title" Type="Text" X="25" Y="135" Width="300" Height="15" Transparent="yes" NoPrefix="yes" Text="Port to listen" />
                <Control Id="PropertyEdit_LISTEN_PORT" Type="Edit" X="24" Y="147" Width="300" Height="18" Property="LISTEN_PORT" Text="[LISTEN_PORT]" Indirect="no" />

                <Control Id="PropertyEdit_CONFIG_FILE_Title1" Type="Text" X="25" Y="170" Width="300" Height="15" Transparent="yes" NoPrefix="yes" Text="Path to config file. If empty, no config will be used. If set to 'config.yaml', " />
                <Control Id="PropertyEdit_CONFIG_FILE_Title2" Type="Text" X="25" Y="180" Width="300" Height="15" Transparent="yes" NoPrefix="yes" Text="then the config.yaml at the install path will be used." />
                <Control Id="PropertyEdit_CONFIG_FILE" Type="Edit" X="24" Y="192" Width="300" Height="18" Property="CONFIG_FILE" Text="[CONFIG_FILE]" Indirect="no" />

                <Control Id="Next" Type="PushButton" X="236" Y="243" Width="56" Height="17" Default="yes" Text="!(loc.WixUINext)">
                    <Publish Event="NewDialog" Value="VerifyReadyDlg" />
                </Control>
                <Control Id="Back" Type="PushButton" X="180" Y="243" Width="56" Height="17" Text="!(loc.WixUIBack)">
                    <Publish Event="NewDialog" Value="CustomizeDlg" />
                </Control>
                <Control Id="Cancel" Type="PushButton" X="304" Y="243" Width="56" Height="17" Cancel="yes" Text="!(loc.WixUICancel)">
                    <Publish Event="SpawnDialog" Value="CancelDlg" />
                </Control>
            </Dialog>

            <!-- skip the license agreement dialog; higher Order takes priority (weird) -->

            <Publish
                Condition="NOT Installed"
                Dialog="WelcomeDlg"
                Control="Next"
                Event="NewDialog"
                Value="CustomizeDlg"
                Order="10"/>
            <Publish
                Condition="NOT Installed"
                Dialog="CustomizeDlg"
                Control="Back"
                Event="NewDialog"
                Value="WelcomeDlg"
                Order="10"/>
            <Publish
                Dialog="CustomizeDlg"
                Control="Next"
                Event="NewDialog"
                Value="CustomPropertiesDlg"
                Order="10"/>
            <Publish
                Dialog="VerifyReadyDlg"
                Control="Back"
                Event="NewDialog"
                Value="CustomPropertiesDlg"
                Order="10"/>


            <!--CustomPropertyDlg-->
        </UI>

        <!-- InstallLocation key -->
        <CustomAction Id="SetInstallLocation" Property="ARPINSTALLLOCATION" Value="[APPLICATIONFOLDER]" />

        <StandardDirectory Id="ProgramFiles64Folder">
            <Directory Id="APPLICATIONFOLDER" Name="windows_exporter">
                <Directory Id="textfile_inputs" Name="textfile_inputs" />
            </Directory>
        </StandardDirectory>

        <ComponentGroup Id="CG_FirewallException">
            <Component Directory="APPLICATIONFOLDER" Id="C_FirewallException" Guid="9f522655-ac0e-42d2-a512-a7b19ebec7f7">
                <fw:FirewallException
                    Id="MetricsEndpoint"
                    Name="$(var.ProductName)"
                    Description="$(var.ProductName) HTTP endpoint"
                    Program="[#windows_exporter.exe]"
                    Port="[LISTEN_PORT]"
                    Protocol="tcp">
                    <fw:RemoteAddress Value="[REMOTE_ADDR]" />
                </fw:FirewallException>
            </Component>
        </ComponentGroup>
    </Package>
</Wix>
