---

# Load distribution specific variables
- 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
  tags: pma

# Detect current version (if any) and set install_mode (to none, upgrade or install)
- block:
    - import_tasks: ../includes/webapps_set_install_mode.yml
      vars:
        - root_dir: "{{ pma_root_dir }}"
        - version: "{{ pma_version }}"
    - set_fact: pma_install_mode={{ (install_mode == 'upgrade' and not pma_manage_upgrade) | ternary('none',install_mode) }}
    - set_fact: pma_current_version={{ current_version | default('') }}
  tags: pma

# Generate a random key for cookie signing
- block:
    - import_tasks: ../includes/get_rand_pass.yml
      vars:
        - pass_file: "{{ pma_root_dir }}/meta/key.txt"
    - set_fact: pma_key={{ rand_pass }}
  tags: pma

# Generate a DB password if not defined
- when: pma_db_pass is not defined
  block:
    - import_tasks: ../includes/get_rand_pass.yml
      vars:
        - pass_file: "{{pma_root_dir }}/meta/ansible_dbpass"
    - set_fact: pma_db_pass={{ rand_pass }}
  tags: pma

- name: Encrypt SQL user passwords
  command: php{{ pma_php_version }} -r "echo openssl_encrypt('{{ item.sql_password }}','aes-128-cbc','{{ pma_sso_encryption_token }}',0,substr(hash('sha256','{{ item.user }}'),0,16));"
  register: pma_sso_users_encrypted
  changed_when: False
  loop: "{{ pma_sso_users | default([]) }}"
  when: pma_sso_encryption_token is defined
  tags: pma

- name: Encrypt SQL group passwords
  command: php{{ pma_php_version }} -r "echo openssl_encrypt('{{ item.sql_password }}','aes-128-cbc','{{ pma_sso_encryption_token }}',0,substr(hash('sha256','{{ item.group }}'),0,16));"
  register: pma_sso_groups_encrypted
  changed_when: False
  loop: "{{ pma_sso_groups | default([]) }}"
  when: pma_sso_encryption_token is defined
  tags: pma

