---

- name: APT | Install PHP-FPM for Debian based systems
  ansible.builtin.apt:
    pkg: "{{ php_fpm_service }}"
    state: "{{ 'present' if php_install_fpm else 'absent' }}"
  when: ansible_os_family == 'Debian'

- name: SERVICE | Enable service on FreeBSD
  ansible.builtin.service:
    name: "{{ php_fpm_service }}"
    enabled: "{{ 'true' if php_install_fpm else 'false' }}"
  when: ansible_os_family == 'FreeBSD'

- name: LINEINFILE | PHP configuration
  ansible.builtin.lineinfile:
    dest: '{{ php_fpm_ini }}'
    regexp: '^;?{{ item.key }}'
    line: '{{ item.key }} = {{ item.value }}'
    create: true
    owner: root
    group: root
    mode: 0644
  loop: "{{ php_ini | combine(php_ini_fpm) | dict2items }}"
  notify: Restart php-fpm

- name: TEMPLATE | Deploy pool configuration
  ansible.builtin.template:
    src: etc/__php__/fpm/pool.d/pool.conf.j2
    dest: '{{ php_fpm_pool_dir }}/{{ item.name }}.conf'
    owner: root
    group: root
    mode: 0644
  loop: "{{ ansible_local.hanxhx_php.fpm_pool }}"
  notify: Restart php-fpm

- name: FILE | Delete default pool if necessary
  ansible.builtin.file:
    path: "{{ php_fpm_pool_dir }}/www.conf"
    state: absent
  when: '"www" not in (ansible_local.hanxhx_php.fpm_pool | map(attribute="name") | list) and php_autoremove_default_pool'
  notify: Restart php-fpm