---
- name: Create main Datadog agent configuration file
  win_template:
    #FIXME: should have permissions set to only be readable by ddagentuser
    src: datadog.yaml.j2
    dest: "{{ datadog_windows_config_root }}\\datadog.yaml"
  when: datadog_manage_config
  notify: restart datadog-agent-win

- name: Register all checks directories present in datadog
  win_find:
    paths: "{{ ansible_facts.env['ProgramData'] }}\\Datadog\\conf.d"
    patterns:
    - "*.d"
    file_type: directory
  register: datadog_conf_directories
  when: datadog_manage_config and (datadog_disable_untracked_checks or datadog_disable_default_checks)

- name: Delete checks not present in datadog_tracked_checks
  win_file:
    path: "{{ ansible_facts.env['ProgramData'] }}\\Datadog\\conf.d\\{{ item }}.d\\conf.yaml"
    state: absent
  loop: "{{ datadog_conf_directories.files | map(attribute='path') | list | map('win_basename') | list | map('regex_replace', '^(.*).d$', '\\1') | list }}"
  when: datadog_manage_config and datadog_disable_untracked_checks and item not in datadog_tracked_checks
  notify: restart datadog-agent-win

- name: Delete default checks
  win_file:
    path: "{{ ansible_facts.env['ProgramData'] }}\\Datadog\\conf.d\\{{ item }}.d\\conf.yaml.default"
    state: absent
  loop: "{{ datadog_conf_directories.files | map(attribute='path') | list | map('win_basename') | list | map('regex_replace', '^(.*).d$', '\\1') | list }}"
  when: datadog_manage_config and datadog_disable_default_checks and item not in datadog_tracked_checks
  notify: restart datadog-agent-win

- name: Ensure configuration directories are present for each Datadog check
  win_file:
    path: "{{ datadog_windows_config_root }}\\conf.d\\{{ item }}.d"
    state: directory
  with_items: '{{ datadog_checks|list }}'
  when: datadog_manage_config

- name: Create a configuration file for each Datadog check
  win_template:
    src: checks.yaml.j2
    dest: "{{ datadog_windows_config_root }}\\conf.d\\{{ item }}.d\\conf.yaml"
  with_items: "{{ datadog_checks|list }}"
  when: datadog_manage_config
  notify: restart datadog-agent-win

- name: Remove old configuration file for each Datadog check
  win_file:
    path: "{{ datadog_windows_config_root }}\\conf.d\\{{ item }}.yaml"
    state: absent
  with_items: "{{ datadog_checks|list }}"
  when: datadog_manage_config
  notify: restart datadog-agent-win

- name: Ensure datadog-trace-agent and datadog-process-agent are not disabled
  win_service:
    name: "{{ item }}"
    start_mode: manual
  when: not datadog_skip_running_check and datadog_enabled and not ansible_check_mode
  with_list:
    - datadog-trace-agent
    - datadog-process-agent

- name: Create system-probe configuration file
  win_template:
    src: system-probe.yaml.j2
    dest: "{{ datadog_windows_config_root }}\\system-probe.yaml"
  when: datadog_manage_config
  notify: restart datadog-agent-win

- name: Ensure datadog-agent is running
  win_service:
    name: datadogagent
    state: started
    start_mode: auto
  when: not datadog_skip_running_check and datadog_enabled and not ansible_check_mode

- name: Ensure datadog-agent is disabled
  win_service:
    name: "{{ item }}"
    state: stopped
    start_mode: disabled
  when: not datadog_skip_running_check and not datadog_enabled
  with_list:
    - datadog-trace-agent
    - datadog-process-agent
    - datadogagent    

- name: Create installation information file
  template:
    src: install_info.j2
    dest: "{{ datadog_windows_config_root }}\\install_info"
    mode: 0644
