<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
    <Product Name="Datadog Agent" Id="*" UpgradeCode="82210ed1-bbe4-4051-aa15-002ea31dde15"
    Language="1033" Codepage="1252" Version="$(var.AgentVersion)" Manufacturer="Datadog Inc.">
        <Package Id="*" Keywords="Installer" Description="Datadog Agent Installer"
          Comments="Copyright 2013 Datadog, Inc." Manufacturer="Datadog Inc."
          InstallerVersion="100" Languages="1033" Compressed="yes" SummaryCodepage="1252" />

        <Property Id="PREVIOUSVERSIONSINSTALLED" Secure="yes" />

        <Upgrade Id="82210ed1-bbe4-4051-aa15-002ea31dde15">
           <UpgradeVersion
              Minimum="1.0.0.0" Maximum="99.0.0.0"
              Property="PREVIOUSVERSIONSINSTALLED"
              IncludeMinimum="yes" IncludeMaximum="no" />
        </Upgrade>

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

        <Directory Id="TARGETDIR" Name="SourceDir">
            <Directory Id="ProgramFilesFolder">
                <Directory Id="APPLICATIONROOTDIRECTORY" Name="Datadog"/>
            </Directory>
            <Directory Id="CommonAppDataFolder" SourceName="CommonAppData">
                <Directory Id="APPLIDATIONDATADIRECTORY" Name="Datadog" />
            </Directory>
            <Directory Id="ProgramMenuFolder">
                <Directory Id="ApplicationProgramsFolder" Name="Datadog"/>
            </Directory>
        </Directory>

        <Binary Id="FindReplace" SourceFile="$(var.WixRoot)\FindReplace.exe" />
        <CustomAction
            Id="ReplaceAPIKey"
            BinaryKey="FindReplace"
            ExeCommand='"[CommonAppDataFolder]\Datadog\datadog.conf" APIKEYHERE [APIKEY]'
            Execute="deferred"
            Return="check"
            Impersonate="no" />

        <CustomAction
            Id="ReplaceTags"
            BinaryKey="FindReplace"
            ExeCommand='"[CommonAppDataFolder]\Datadog\datadog.conf" "#tags: mytag0, mytag1" "tags: [TAGS]"'
            Execute="deferred"
            Return="check"
            Impersonate="no" />

        <CustomAction
            Id="ReplaceHostname"
            BinaryKey="FindReplace"
            ExeCommand='"[CommonAppDataFolder]\Datadog\datadog.conf" "#hostname: mymachine.mydomain" "hostname: [HOSTNAME]"'
            Execute="deferred"
            Return="check"
            Impersonate="no" />

        <DirectoryRef Id="APPLICATIONROOTDIRECTORY">
            <Directory Id="INSTALLDIR" Name="Datadog Agent">
                <Component Id="license.txt" Guid="4B59F7AE-01AC-11E2-9AF6-28EC6088709B">
                    <File Id="license.txt" Source="$(var.InstallFiles)\license.txt" KeyPath="yes"></File>
                </Component>
                <Component Id="datadogagentstatus.url" Guid="B8F673AA-F5F7-4273-947A-21391678D6BD">
                    <File Id="datadogagentstatus.url" Source="$(var.InstallFiles)\datadog-agent-status.url" KeyPath="yes"></File>
                </Component>
                <Directory Id="FILESDIR" Name="files">
                    <Component Id="cacertificates.crt" Guid="514F0A5A-01AC-11E2-B0F9-2CEC6088709B">
                        <File Id="cacertificates.crt" Name="ca-certificates.crt" Source="$(var.InstallFiles)\ca-certificates.crt" KeyPath="yes"></File>
                    </Component>
                    <Component Id="datadogcert.pem" Guid="9d2c44f7-21b2-45c6-a05f-e544d5f7d239">
                        <File Id="datadogcert.pem" Name="datadog-cert.pem" Source="$(var.InstallFiles)\datadog-cert.pem" KeyPath="yes"></File>
                    </Component>

                    <Component Id="Microsoft.VC90.CRT.manifest" Guid="491C4E02-10C9-11E2-AC50-DD0B6288709B">
                    <File Id="Microsoft.VC90.CRT.manifest" Source="$(var.InstallFiles)\Microsoft.VC90.CRT\Microsoft.VC90.CRT.manifest" KeyPath="yes"></File>
                    </Component>
                    <Component Id="msvcp90.dll" Guid="9142E11E-10C9-11E2-94FC-4B0C6288709B">
                        <File Id="msvcp90.dll" Source="$(var.InstallFiles)\Microsoft.VC90.CRT\msvcp90.dll" KeyPath="yes"></File>
                    </Component>
                    <Component Id="msvcr90.dll" Guid="9A9A9342-10C9-11E2-A0B9-4D0C6288709B">
                        <File Id="msvcr90.dll" Source="$(var.InstallFiles)\Microsoft.VC90.CRT\msvcr90.dll" KeyPath="yes"></File>
                    </Component>
                </Directory>


            </Directory>

        </DirectoryRef>

        <DirectoryRef Id="ApplicationProgramsFolder">
            <Component Id="ApplicationShortcut" Guid="05206095-f4a0-4bb7-9ebd-80c7488dd7f9">
                <Shortcut Id="ApplicationStartMenuShortcut" 
                          Name="Datadog Agent Manager"
                          Description="Manage your Datadog Agent"
                          Target="[FILESDIR]agent-manager.exe"
                          WorkingDirectory="FILESDIR"/>
                <RemoveFolder Id="ApplicationProgramsFolder" On="uninstall"/>
                <RegistryValue Root="HKCU" Key="Software\Microsoft\Datadog" Name="installed" Type="integer" Value="1" KeyPath="yes"/>
            </Component>
        </DirectoryRef>

        <DirectoryRef Id="APPLIDATIONDATADIRECTORY">
            <Component Id="datadog.conf" Guid="83461594-01AC-11E2-BE35-37EC6088709B" NeverOverwrite="yes" Permanent="yes">
                <File Id="datadog.conf" Name="datadog.conf" Source="$(var.InstallFiles)\datadog_win32.conf"></File>
            </Component>
            <Directory Id="logs" Name="logs">
                        <Component Id="logs" Guid="e194d05a-6dc7-40be-a626-6a15b43c456b"
                        SharedDllRefCount="no" KeyPath="no" NeverOverwrite="no" Permanent="no" Transitive="no"
                        Win64="no" Location="either">
                        <CreateFolder/>
                        </Component>
                    </Directory>
        </DirectoryRef>



        <Feature Id="MainApplication" Title="Main Application" Level="1">
            <ComponentRef Id="cacertificates.crt" />
            <ComponentRef Id="datadogcert.pem" />
            <ComponentRef Id="license.txt" />
            <ComponentRef Id="datadogagentstatus.url" />
            <ComponentRef Id="Microsoft.VC90.CRT.manifest" />
            <ComponentRef Id="msvcp90.dll" />
            <ComponentRef Id="msvcr90.dll" />
            <ComponentRef Id="datadog.conf" />
            <ComponentGroupRef Id="checks.d" />
            <ComponentGroupRef Id="conf.d" />
            <ComponentGroupRef Id="files" />
            <ComponentRef Id="ApplicationShortcut" />
            <ComponentRef Id="logs" />
        </Feature>


        <InstallExecuteSequence>
            <RemoveExistingProducts Before="InstallInitialize" />
            <Custom Action="ReplaceAPIKey" Before="StartServices" />
            <Custom Action="ReplaceTags" Before="StartServices" />
            <Custom Action="ReplaceHostname" Before="StartServices" />
           <Custom Action='LaunchApp' After='InstallFinalize'>NOT Installed</Custom>
        </InstallExecuteSequence>

<CustomAction Id="LaunchApp" Directory="FILESDIR" ExeCommand="[SystemFolder]cmd.exe /C start agent-manager.exe" />
    </Product>
</Wix>