---

- fail: msg="Variable '{{ mongod_source_template_dir }}' is not defined"
  when: mongod_source_template_dir is undefined

- name: Compute list of mongo nodes
  set_fact:
    mongo_nodes: "{{ mongo_nodes | default([]) + [ hostvars[item]['ip_service'] + ':'+ mongodb.mongod_port | string ] }}"
  loop: "{{ groups['hosts_vitamui_mongod'] }}"

- name: Set Mongo URI
  set_fact:
    mongod_uri: "{{ mongo_nodes| join(',') }}"

- name: Set mongod_output_dir_entry_point
  set_fact:
    mongod_output_dir_entry_point: "{{ vitamui_defaults.folder.root_path }}/app/mongod/"

- import_tasks: check_auth.yml

- name: Initialize directory if it doesn't exist.
  file:
    path: "{{ mongod_output_dir_entry_point }}"
    state: directory

- name: "Clean directory {{ mongod_output_dir_entry_point }}"
  shell: "rm -Rf {{ mongod_output_dir_entry_point }}/*"

# We sort directories by theirs versions
- name: List script files versions in the directory {{ mongod_source_template_dir }}
  delegate_to: localhost
  shell:
    cmd: find * -maxdepth 1 -type d  | sort -V
    chdir: "{{ mongod_source_template_dir }}"
  register: versions

# For each version, we apply a second sort on the index of the script file.
- name: List script files in the directory {{ mongod_source_template_dir }}
  delegate_to: localhost
  shell:
    cmd: find {{ version }}/* -type f  -print | sort -V -t '_' -k1
    chdir: "{{ mongod_source_template_dir }}"
  register: output
  loop: "{{ versions.stdout_lines }}"
  loop_control:
    loop_var: version
  
- name: "Compute file scripts"
  delegate_to: localhost
  set_fact:
    mongod_files: "{{ (mongod_files| default([])) + item.stdout_lines }}"
  loop: "{{ output.results }}"

# We apply regex for included and excludes files in order to compute the eligible scripts.
- name: Compute list of excluded files
  delegate_to: localhost
  set_fact:
    mongod_excluded_files : "{{ (mongod_excluded_files| default([])) + [ item.0 ] }}"
  when: item.0 is not match(item.1) or item.0 is match(item.2)
  with_nested: 
    - "{{ mongod_files }}"
    - "{{ mongodb.included_scripts }}"
    - "{{ mongodb.excluded_scripts }}"

- name: Compute list of eligible files
  delegate_to: localhost
  set_fact:
    mongod_eligible_files : "{{ (mongod_eligible_files| default([])) + [ {'name': item, 'version': item | regex_replace('^(.+)/(.+)$', '\\1') ,'finalname': item | regex_replace('/', '_') | basename | regex_replace('\\.j2$')} ] }}"
  loop: "{{ mongod_files | difference(mongod_excluded_files| default([])) }}"

# We generate scripts and upload on remote host
- name: Compute and copy script files
  template:
    src: "{{ mongod_source_template_dir }}/{{ item.name }}"
    dest: "{{ mongod_output_dir_entry_point }}/{{ item.finalname }}"
    owner: "{{ vitamui_defaults.users.vitamuidb }}"
    group: "{{ vitamui_defaults.users.group }}"
    mode: 0755
  loop: "{{ mongod_eligible_files | unique }}"

- name: "Execute file"
  include_tasks: "execute_script.yml"
  loop: "{{ mongod_eligible_files | unique }}"
  loop_control:
    loop_var: mongo_file