---
- name: Define php_fpm_daemon.
  ansible.builtin.set_fact:
    php_fpm_daemon: "{{ __php_fpm_daemon }}"
  when: php_fpm_daemon is not defined

- name: Define php_fpm_pool_conf_path.
  ansible.builtin.set_fact:
    php_fpm_pool_conf_path: "{{ __php_fpm_pool_conf_path }}"
  when: php_fpm_pool_conf_path is not defined

- name: Define php_fpm_pool_user.
  ansible.builtin.set_fact:
    php_fpm_pool_user: "{{ __php_fpm_pool_user }}"
  when: php_fpm_pool_user is not defined

- name: Define php_fpm_pool_group.
  ansible.builtin.set_fact:
    php_fpm_pool_group: "{{ __php_fpm_pool_group }}"
  when: php_fpm_pool_group is not defined

- name: Stat php_fpm_pool_conf_path
  ansible.builtin.stat:
    path: "{{ php_fpm_pool_conf_path | dirname }}"
  register: php_fpm_pool_conf_path_dir_stat

- name: Ensure the default pool directory exists.
  ansible.builtin.file:
    path: "{{ php_fpm_pool_conf_path | dirname }}"
    state: directory
    owner: root
    group: root
    mode: '0755'
  when: php_fpm_pool_conf_path_dir_stat.stat.islnk is not defined

- name: Create fpm pools.
  ansible.builtin.template:
    src: "{{ item.pool_template | default('www.conf.j2', true) }}"
    dest: "{{ php_fpm_pool_conf_path | dirname }}/{{ item.pool_name }}.conf"
    owner: root
    group: root
    mode: '0644'
    force: true
  loop: "{{ php_fpm_pools | default([], true) }}"
  when: php_enable_php_fpm
  notify: Restart php-fpm

- name: Ensure php-fpm is started and enabled at boot (if configured).
  ansible.builtin.service:
    name: "{{ php_fpm_daemon }}"
    state: "{{ php_fpm_state }}"
    enabled: "{{ php_fpm_enabled_on_boot }}"
  when:
    - php_enable_php_fpm
    - ansible_facts.distribution != "Debian"
    - not ansible_check_mode
