---
- name: Parse Agent version
  set_fact:
    agent_version: "{{ datadog_agent_version | regex_search(regexp, '\\g<epoch>', '\\g<major>', '\\g<minor>', '\\g<bugfix>', '\\g<suffix>', '\\g<release>') }}"
  vars:
    regexp: '(?:(?P<epoch>[0-9]+):)?(?P<major>[0-9]+)\.(?P<minor>[0-9]+)\.(?P<bugfix>[0-9]+)(?P<suffix>(?:~|-)[^0-9\s-]+[^-\s]*)?(?:-(?P<release>[0-9]+))?'

- name: Set version vars
  set_fact:
    datadog_epoch: "{{ agent_version.0 }}"
    datadog_major: "{{ agent_version.1 }}"
    datadog_minor: "{{ agent_version.2 }}"
    datadog_bugfix: "{{ agent_version.3 }}"
    datadog_suffix: "{{ agent_version.4 }}"
    datadog_release: "{{ agent_version.5 }}"

- name: Fill empty version epoch with default
  set_fact:
    datadog_epoch: "1"
  when: datadog_epoch | length == 0

- name: Fill empty version release with default
  set_fact:
    datadog_release: "1"
  when: datadog_release | length == 0

- name: Stop play if datadog_agent_version and datadog_agent_major_version are not compatible
  fail:
    msg: "The provided major version {{ datadog_agent_major_version }} is not compatible with the
      version {{ datadog_major }} deduced from datadog_agent_version ({{ datadog_agent_version }}).
      Aborting play."
  when: datadog_agent_major_version | length > 0 and datadog_major != datadog_agent_major_version

- name: Set datadog_agent_major_version to deduced value from datadog_agent_version
  set_fact:
    datadog_agent_major_version: "{{ datadog_major }}"

- name: Set OS-specific versions
  # NOTE: if changing these, make sure the format correspond with values in datadog_version_finding_cmds below
  set_fact:
    datadog_agent_debian_version: "{{ datadog_epoch }}:{{ datadog_major }}.{{ datadog_minor }}.{{ datadog_bugfix }}{{ datadog_suffix }}-{{ datadog_release }}"
    datadog_agent_redhat_version: "{{ datadog_major }}.{{ datadog_minor }}.{{ datadog_bugfix }}{{ datadog_suffix }}-{{ datadog_release }}"
    datadog_agent_suse_version: "{{ datadog_epoch }}:{{ datadog_major }}.{{ datadog_minor }}.{{ datadog_bugfix }}{{ datadog_suffix }}-{{ datadog_release }}"
    datadog_agent_windows_version: "{{ datadog_major }}.{{ datadog_minor }}.{{ datadog_bugfix }}{{ datadog_suffix }}"

- name: Construct commands to find Agent version
  set_fact:
    datadog_version_finding_cmds:
      Debian: "dpkg -s {{ datadog_agent_flavor }} | grep '^Version:' | awk '{print $2}'"
      RedHat: "rpm -q --qf '%{VERSION}-%{RELEASE}' {{ datadog_agent_flavor }}"
      Suse: "rpm -q --qf '%{EPOCH}:%{VERSION}-%{RELEASE}' {{ datadog_agent_flavor }}"

- name: Create OS-specific version dict
  set_fact:
    datadog_agent_os2version:
      Debian: "{{ datadog_agent_debian_version }}"
      RedHat: "{{ datadog_agent_redhat_version }}"
      Suse: "{{ datadog_agent_suse_version }}"
      Windows: "{{ datadog_agent_windows_version }}"

- name: Get Linux Agent version
  shell: "{{ datadog_version_finding_cmds[ansible_facts.os_family] }}"  # noqa 305 - Ansible lint thinks we could use command, but we need shell because some of the cmds have pipes
  register: datadog_version_check_linux
  changed_when: false
  failed_when: false
  check_mode: no
  when: ansible_facts.system is defined and ansible_facts.system == "Linux"

# NOTE: This won't work with rc / beta builds.
- name: Get Windows Agent version
  win_shell: |
    $product_name = "Datadog Agent"
    $query = "Select Name,IdentifyingNumber,InstallDate,InstallLocation,ProductID,Version FROM Win32_Product where Name like '$product_name%'"
    $installs = Get-WmiObject -query $query

    if (!$installs -or ($installs.Count -eq 0) -or ($installs.Count -gt 1)) {
      Write-Host ""
    } else {
      $ddmaj, $ddmin, $ddpatch, $ddbuild = $installs.Version.split(".")
      Write-Host "$($ddmaj).$($ddmin).$($ddpatch)"
    }
  register: datadog_version_check_win
  changed_when: false
  failed_when: false
  check_mode: no
  when: ansible_facts.os_family == "Windows"

- name: Set skip install flag if version already installed (Linux)
  set_fact:
    datadog_skip_install: "{{ datadog_version_check_linux.stdout | trim == datadog_agent_os2version[ansible_facts.os_family] }}"
  when: ansible_facts.system is defined and ansible_facts.system == "Linux"

- name: Set skip install flag if version already installed (Windows)
  set_fact:
    datadog_skip_install: "{{ datadog_version_check_win.stdout | trim == datadog_agent_os2version[ansible_facts.os_family] }}"
  when: ansible_facts.os_family == "Windows"
