---

- include_vars: "{{ item }}"
  with_first_found:
    - vars/{{ ansible_distribution }}-{{ ansible_distribution_major_version }}.yml
    - vars/{{ ansible_os_family }}-{{ ansible_distribution_major_version }}.yml
    - vars/{{ ansible_distribution }}.yml
    - vars/{{ ansible_os_family }}.yml
    - vars/defaults.yml
  tags: fpbx

# Detect if it's an install, upgrade, or none
- block:
    - import_tasks: ../includes/webapps_set_install_mode.yml
      vars:
        - root_dir: "{{ fpbx_root_dir }}"
        - version: "{{ fpbx_version }}"
        - manage_upgrade: False
    - set_fact: fpbx_install_mode={{ (install_mode == 'install') | ternary('install','none') }}
    - set_fact: fpbx_current_version={{ current_version | default('') }}
  tags: fpbx

- name: Build a list of music on hold format to install
  set_fact: fpbx_moh_pkg={{ fpbx_moh_pkg | default([ 'asterisk-moh-opsound' ]) + [ 'asterisk-moh-opsound-' ~ item ] }}
  loop:
    - alaw
    - g722
    - g729
    - gsm
    - siren14
    - siren7
    - sln16
    - ulaw
    - wav
  tags: fpbx

- name: Build a list of languages packages to install
  set_fact: fpbx_snd_pkg={{ fpbx_snd_pkg | default([]) + [ 'asterisk-sounds-core-' ~ item.0 ~ '-' ~ item.1 ] }}
  with_nested:
    - - en
      - es
      - fr
      - it
    - - alaw
      - g722
      - g729
      - gsm
      - siren14
      - siren7
      - sln16
      - ulaw
      - wav
  tags: fpbx

# Create a random pass for phone provisionning
- when: fpbxphone_pass is not defined
  block:
    - import_tasks: ../includes/get_rand_pass.yml
      vars:
        - pass_file: "{{ fpbx_root_dir }}/meta/ansible_phonepass"
        - pass_size: 12
        - complex: False
    - set_fact: fpbx_phone_pass={{ rand_pass }}
  tags: fpbx

# Generate a random pass for the database if needed
- when: fpbx_db_pass is not defined
  block:
    - import_tasks: ../includes/get_rand_pass.yml
      vars:
        - pass_file: "{{ fpbx_root_dir }}/meta/ansible_dbpass"
        - complex: False
    - set_fact: fpbx_db_pass={{ rand_pass }}
  tags: fpbx

# Generate a random pass for the manager if needed
- when: fpbx_manager_pass is not defined
  block:
    - import_tasks: ../includes/get_rand_pass.yml
      vars:
        - pass_file: "{{ fpbx_root_dir }}/meta/ansible_manager_pass"
        - complex: False
    - set_fact: fpbx_manager_pass={{ rand_pass }}
  tags: fpbx

