# Copyright 2019-2024 Chris Croome
#
# This file is part of the Webarchitects PHP Ansible role.
#
# The Webarchitects PHP Ansible role is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
#
# The Webarchitects PHP Ansible role is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with the Webarchitects PHP Ansible role. If not, see <https://www.gnu.org/licenses/>.
---
- name: PHP role skipped
  ansible.builtin.debug:
    msg: "The tasks in the PHP role are not being run since the php variable is not true."
  when: not php | bool
  tags:
    - php

- name: PHP
  block:

    - name: Include PHP verify variables tasks
      ansible.builtin.include_tasks: verify.yml
      when:
        - php_verify is defined
        - php_verify | bool
      tags:
        - php_verify

    - name: Include apt role local fact tasks
      ansible.builtin.include_role:
        name: apt
        tasks_from: local_facts.yml
      when: >-
        ( ansible_facts.ansible_local.dpkg.arch is not defined ) or
        ( ansible_facts.ansible_local.dpkg.installed is not defined ) or
        ( ansible_facts.ansible_local.gpg.version is not defined ) or
        ( ansible_facts.ansible_local.bash.path is not defined )
      tags:
        - php_apt

    - name: Include PHP variable check tasks
      ansible.builtin.include_tasks: check_vars.yml
      tags:
        - php_cfg
        - php_conf
        - php_pkg

    - name: Include the init system check tasks
      ansible.builtin.include_tasks: check_init.yml
      tags:
        - php_apt
        - php_cfg
        - php_conf
        - php_mods
        - php_pkg

    - name: Include Sury PHP apt repo tasks
      ansible.builtin.include_tasks: apt.yml
      when: php_sury is defined
      tags:
        - php_apt

    - name: Debug PHP versions that are to be installed
      ansible.builtin.debug:
        var: php_ver_present
        verbosity: "{% if ansible_check_mode | bool %}1{% else %}2{% endif %}"
      tags:
        - php_pkg

    - name: Include PHP package tasks
      ansible.builtin.include_tasks: pkg.yml
      when: php_versions is defined
      tags:
        - php_pkg

    - name: Include PHP check tasks
      ansible.builtin.include_tasks: checks.yml
      tags:
        - php_cfg
        - php_conf
        - php_mods

    - name: Configure PHP
      block:

        - name: Debug PHP versions that are to be configured
          ansible.builtin.debug:
            var: php_conf_ver_present
            verbosity: "{% if ansible_check_mode | bool %}1{% else %}2{% endif %}"

        - name: PHP versions that are not installed cannot be configured unless in check mode
          ansible.builtin.fail:
            msg: "PHP versions {% for php_v in php_conf_ver_present | difference(php_ver_installed) %}{{ php_v }}{{ ', ' if not loop.last }}{% endfor %} cannot be configured when they are not present."
          when:
            - not ansible_check_mode | bool
            - php_conf_ver_present | difference(php_ver_installed) != []

        - name: Include PHP configuration tasks when versions are present or when running in check mode
          ansible.builtin.include_tasks: conf.yml
          when: >-
            ( ansible_check_mode | bool ) or
            ( php_conf_ver_present | difference(php_ver_installed) == [] )

      when:
        - ( php_config is defined )
        - ( php_conf_ver_present != [] ) or ( php_conf_ver_absent != [] )
      tags:
        - php_cfg
        - php_conf

    - name: Configure PHP modules
      block:

        - name: Debug PHP versions that are to have modules configured
          ansible.builtin.debug:
            var: php_mods_ver_present
            verbosity: "{% if ansible_check_mode | bool %}1{% else %}2{% endif %}"

        - name: PHP versions that are not installed cannot have modules configured unless in check mode
          ansible.builtin.fail:
            msg: "PHP versions {% for php_v in php_mods_ver_present | difference(php_ver_installed) %}{{ php_v }}{{ ', ' if not loop.last }}{% endfor %} cannot be configured when they are not present."
          when:
            - not ansible_check_mode | bool
            - php_mods_ver_present | difference(php_ver_installed) != []

        - name: Include PHP modules configuration tasks
          ansible.builtin.include_tasks: mods.yml
          loop: "{{ php_modules }}"
          loop_control:
            loop_var: php_version
            label: "{{ php_version.version }}"
          when: >-
            ( ansible_check_mode | bool ) or
            ( php_mods_ver_present | difference(php_ver_installed) == [] )

      when:
        - ( php_modules is defined )
        - ( php_mods_ver_present != [] ) or ( php_mods_ver_absent != [] )
      tags:
        - php_mods

  when: php | bool
  tags:
    - php
...
