---

- name: "Set custom values for PHP config (jessie)"
  ini_file:
    dest: /etc/php5/apache2/conf.d/zzz-evolinux-custom.ini
    section: PHP
    option: "disable_functions"
    value: "shell-exec,system,passthru,putenv,popen"
  notify: reload apache2
  when: ansible_distribution_release == "jessie"

- name: "Set custom values for PHP config (Debian 9)"
  ini_file:
    dest: /etc/php/7.0/apache2/conf.d/zzz-evolinux-custom.ini
    section: PHP
    option: "disable_functions"
    value: "shell-exec,system,passthru,putenv,popen"
  notify: reload apache2
  when: ansible_distribution_major_version is version('9', '=')

- name: "Set custom values for PHP config (Debian 10)"
  ini_file:
    dest: /etc/php/7.3/apache2/conf.d/zzz-evolinux-custom.ini
    section: PHP
    option: "disable_functions"
    value: "shell-exec,system,passthru,putenv,popen"
  notify: reload apache2
  when: ansible_distribution_major_version is version('10', '=')

- name: Install evoadmin VHost
  template:
    src: "{{ item }}"
    dest: /etc/apache2/sites-available/evoadmin.conf
    force: "{{ evoadmin_force_vhost }}"
  loop: "{{ query('first_found', templates) }}"
  vars:
    templates:
      - "templates/evoadmin-web/evoadmin.{{ inventory_hostname }}.conf.j2"
      - "templates/evoadmin-web/evoadmin.{{ host_group | default('all') }}.conf.j2"
      - "templates/evoadmin-web/evoadmin.conf.j2"
      - "templates/evoadmin.conf.j2"
  register: evoadmin_vhost_template
  notify: reload apache2

- name: Enable evoadmin vhost
  command: "a2ensite evoadmin.conf"
  register: cmd_a2ensite
  changed_when: "'Enabling site' in cmd_a2ensite.stdout"
  notify: reload apache2
  when: evoadmin_enable_vhost | bool

- name: Disable evoadmin vhost
  command: "a2dissite evoadmin.conf"
  register: cmd_a2dissite
  changed_when: "'Disabling site' in cmd_a2dissite.stdout"
  notify: reload apache2
  when: not (evoadmin_enable_vhost | bool)

- name: Copy htpasswd for evoadmin
  template:
    src: "{{ item }}"
    dest: "/var/www/.ansible_evoadmin_htpasswd"
    mode: "0644"
    owner: root
    group: www-data
    force: "{{ evoadmin_htpasswd_force }}"
  loop: "{{ query('first_found', templates) }}"
  vars:
    templates:
      - "templates/evoadmin-web/htpasswd.{{ inventory_hostname }}.j2"
      - "templates/evoadmin-web/htpasswd.{{ host_group | default('all') }}.j2"
      - "templates/evoadmin-web/htpasswd.j2"
      - "templates/htpasswd.j2"
  register: evoadmin_htpasswd_template
  when: evoadmin_htpasswd | bool

- name: Copy config file for evoadmin
  template:
    src: "{{ item }}"
    dest: "{{ evoadmin_document_root }}/conf/config.local.php"
    mode: "0640"
    owner: evoadmin
    group: evoadmin
    force: "{{ evoadmin_config_local_php_force }}"
  loop: "{{ query('first_found', templates) }}"
  vars:
    templates:
      - "templates/evoadmin-web/config.local.{{ inventory_hostname }}.php.j2"
      - "templates/evoadmin-web/config.local.{{ host_group | default('all') }}.php.j2"
      - "templates/evoadmin-web/config.local.php.j2"
      - "templates/config.local.php.j2"
  register: evoadmin_config_local_php_template
