---
# Variable setup.
- name: Include OS-specific variables.
  ansible.builtin.include_vars: "{{ ansible_facts.os_family }}.yml"

- name: Include distribution and version-specific vars.
  ansible.builtin.include_vars: "{{ item }}"
  with_first_found:
    - files:
        - "{{ ansible_facts.distribution }}-{{ ansible_facts.distribution_major_version }}.yml"
      skip: true

- name: Set the default PHP version for Debian-based OSes.
  ansible.builtin.set_fact:
    php_default_version_debian: "{{ __php_default_version_debian }}"
  when: php_default_version_debian is not defined and ansible_facts.os_family == 'Debian'

- name: Define the name of the JSON extension package on Debian for PHP <8.
  ansible.builtin.set_fact:
    __php_json_package_debian: "{{ 'php' + php_default_version_debian + '-json' }}"
  when: ansible_facts.os_family == 'Debian' and php_default_version_debian is version('8.0', '<')

- name: Add the JSON extension on Debian for PHP <8.
  ansible.builtin.set_fact:
    __php_packages: "{{ __php_packages + [__php_json_package_debian] }}"
  when: __php_json_package_debian is defined and __php_json_package_debian not in __php_packages

- name: Define php_packages.
  ansible.builtin.set_fact:
    php_packages: "{{ __php_packages | list }}"
  when: php_packages is not defined

- name: Define php_webserver_daemon.
  ansible.builtin.set_fact:
    php_webserver_daemon: "{{ __php_webserver_daemon }}"
  when: php_webserver_daemon is not defined

- name: Define php_conf_paths.
  ansible.builtin.set_fact:
    php_conf_paths: "{{ __php_conf_paths }}"
  when: php_conf_paths is not defined

- name: Define php_extension_conf_paths.
  ansible.builtin.set_fact:
    php_extension_conf_paths: "{{ __php_extension_conf_paths }}"
  when: php_extension_conf_paths is not defined

- name: Define php_apc_conf_filename.
  ansible.builtin.set_fact:
    php_apc_conf_filename: "{{ __php_apc_conf_filename }}"
  when: php_apc_conf_filename is not defined

- name: Define php_opcache_conf_filename (Ubuntu 16.04).
  ansible.builtin.set_fact:
    php_opcache_conf_filename: "10-opcache.ini"
  when: php_opcache_conf_filename is not defined and ansible_facts.distribution_version == "16.04"

- name: Define php_opcache_conf_filename.
  ansible.builtin.set_fact:
    php_opcache_conf_filename: "{{ __php_opcache_conf_filename }}"
  when: php_opcache_conf_filename is not defined

- name: Define php_fpm_conf_path.
  ansible.builtin.set_fact:
    php_fpm_conf_path: "{{ __php_fpm_conf_path }}"
  when: php_fpm_conf_path is not defined

# Setup/install tasks.
- name: Setup RedHat
  ansible.builtin.include_tasks: setup-RedHat.yml
  when:
    - not php_install_from_source
    - ansible_facts.os_family == 'RedHat'

- name: Setup Debian
  ansible.builtin.include_tasks: setup-Debian.yml
  when:
    - not php_install_from_source
    - ansible_facts.os_family == 'Debian'

# Install PHP from source when php_install_from_source is true.
- name: Install from source
  ansible.builtin.include_tasks: install-from-source.yml
  when: php_install_from_source

# Configure PHP.
- name: Configure PHP
  ansible.builtin.include_tasks: configure.yml
- name: Configure APCu
  ansible.builtin.include_tasks: configure-apcu.yml
- name: Configure OpCache
  ansible.builtin.include_tasks: configure-opcache.yml
- name: Configure FPM
  ansible.builtin.include_tasks: configure-fpm.yml
