---

# Load distribution specific variables
- include_vars: "{{ item }}"
  with_first_found:
    - "{{ role_path }}/vars/{{ ansible_distribution }}-{{ ansible_distribution_major_version }}.yml"
    - "{{ role_path }}/vars/{{ ansible_os_family }}-{{ ansible_distribution_major_version }}.yml"
    - "{{ role_path }}/vars/{{ ansible_distribution }}.yml"
    - "{{ role_path }}/vars/{{ ansible_os_family }}.yml"
  tags: taiga

# Detect installed version (if any), and decide if ansible should install/upgrade
- set_fact: taiga_install_mode='none'
  tags: taiga

- block:
    - import_tasks: ../includes/webapps_set_install_mode.yml
      vars:
        - root_dir: "{{ taiga_root_dir }}"
        - version: "{{ taiga_archives['back'].version }}"
        - version_file: ansible_version_back
    - set_fact: taiga_install_mode={{ (install_mode == 'upgrade' and not taiga_manage_upgrade) | ternary('none',install_mode) }}
      when: taiga_install_mode == 'none'
    - set_fact: taiga_version_back={{ current_version }}
  tags: taiga

- block:
    - import_tasks: ../includes/webapps_set_install_mode.yml
      vars:
        - root_dir: "{{ taiga_root_dir }}"
        - version: "{{ taiga_archives['front'].version }}"
        - version_file: ansible_version_front
    - set_fact: taiga_install_mode={{ (install_mode == 'upgrade' and not taiga_manage_upgrade) | ternary('none',install_mode) }}
      when: taiga_install_mode == 'none'
    - set_fact: taiga_version_front={{ current_version }}
  tags: taiga

- block:
    - import_tasks: ../includes/webapps_set_install_mode.yml
      vars:
        - root_dir: "{{ taiga_root_dir }}"
        - version: "{{ taiga_archives['events'].version }}"
        - version_file: ansible_version_events
    - set_fact: taiga_install_mode={{ (install_mode == 'upgrade' and not taiga_manage_upgrade) | ternary('none',install_mode) }}
      when: taiga_install_mode == 'none'
    - set_fact: taiga_version_events={{ current_version }}
  tags: taiga

- block:
    - import_tasks: ../includes/webapps_set_install_mode.yml
      vars:
        - root_dir: "{{ taiga_root_dir }}"
        - version: "{{ taiga_archives['protected'].version }}"
        - version_file: ansible_version_protected
    - set_fact: taiga_install_mode={{ (install_mode == 'upgrade' and not taiga_manage_upgrade) | ternary('none',install_mode) }}
      when: taiga_install_mode == 'none'
    - set_fact: taiga_version_protected={{ current_version }}
  tags: taiga

# Generate a password for the database if needed
- when: taiga_db_pass is not defined
  block:
    - import_tasks: ../includes/get_rand_pass.yml
      vars:
        - pass_file: "{{ taiga_root_dir }}/meta/ansible_dbpass"
    - set_fact: taiga_db_pass={{ rand_pass }}
  tags: taiga

# Create a random secret key
- when: taiga_secret_key is not defined
  block:
    - import_tasks: ../includes/get_rand_pass.yml
      vars:
        - pass_file: "{{ taiga_root_dir }}/meta/ansible_secret_key"
    - set_fact: taiga_secret_key={{ rand_pass }}
  tags: taiga

# AMQP password
- when: taiga_amqp_pass is not defined
  block:
    - import_tasks: ../includes/get_rand_pass.yml
      vars:
        - pass_file: "{{ taiga_root_dir }}/meta/ansible_amqp_pass"
        - complex: False
    - set_fact: taiga_amqp_pass={{ rand_pass }}
  tags: taiga

# Default admin pass
- when: taiga_admin_pass is not defined
  block:
    - import_tasks: ../includes/get_rand_pass.yml
      vars:
        - pass_file: "{{ taiga_root_dir }}/meta/ansible_admin_pass"
    - set_fact: taiga_admin_pass={{ rand_pass }}
  tags: taiga

- name: Check if RabbitMQ user exists
  shell: rabbitmqctl list_users | grep -qP '^{{ taiga_amqp_user }}\s+'
  register: taiga_amqp_user_exists
  failed_when: False
  changed_when: False
  tags: taiga
