# 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 configuration file section edited
  block:

    - name: Debug the existing and proposed PHP configuration file variables
      ansible.builtin.debug:
        msg:
          - "File: {{ php_conf_file }}"
          - "Section: {{ php_conf_section.key }}"
          - "Existing {{ php_conf_variable_pair.key }}: '{{ php_conf_file_existing_vars | community.general.json_query(php_conf_variable_json_query) }}'"
          - "Proposed {{ php_conf_variable_pair.key }}: '{{ php_conf_variable_pair.value }};"
        verbosity: "{% if ansible_check_mode | bool or ansible_diff_mode | bool %}0{% else %}1{% endif %}"
      when: php_conf_variable_pair.value != php_conf_file_existing_vars | community.general.json_query(php_conf_variable_json_query)
      vars:
        php_conf_variable_json_query: '"{{ php_conf_section.key }}"."{{ php_conf_variable_pair.key }}"'
      loop: "{{ php_conf_section.value | ansible.builtin.dict2items }}"
      loop_control:
        loop_var: php_conf_variable_pair
        label: "{{ php_conf_variable_pair.key }}"

    # This is to ensure that variables are written to the same place as existing commented values
    # No longer necessary due to the modify_inactive_option for community.general.ini_file
    # - name: Uncomment commented variables in the PHP configuration file that are to be edited
    #   ansible.builtin.lineinfile:
    #     path: "{{ php_conf_file }}"
    #     line: "{{ php_conf_variable_pair.key }} = {{ php_conf_variable_pair.value }}"
    #     regex: "{{ php_conf_variable_regex }}"
    #     state: present
    #     backrefs: true
    #     mode: "0644"
    #     owner: root
    #     group: root
    #   when: php_conf_variable_pair.value != php_conf_file_existing_vars | community.general.json_query(php_conf_variable_json_query)
    #   vars:
    #     php_conf_variable_json_query: '"{{ php_conf_section.key }}"."{{ php_conf_variable_pair.key }}"'
    #     php_conf_variable_regex: "^[;]{{ php_conf_variable_pair.key }}[ =]"
    #   loop: "{{ php_conf_section.value | ansible.builtin.dict2items }}"
    #   loop_control:
    #     loop_var: php_conf_variable_pair
    #   register: php_conf_file_comments_edited

    - name: PHP configuration file edited
      community.general.ini_file:
        path: "{{ php_conf_file }}"
        section: "{{ php_conf_section.key }}"
        option: "{{ php_conf_variable_pair.key }}"
        value: "{{ php_conf_variable_pair.value }}"
        state: present
        modify_inactive_option: true
        no_extra_spaces: "{{ php_conf_file_no_extra_spaces }}"
        mode: "0644"
        owner: root
        group: root
      when: php_conf_variable_pair.value != php_conf_file_existing_vars | community.general.json_query(php_conf_variable_json_query)
      vars:
        php_conf_variable_json_query: '"{{ php_conf_section.key }}"."{{ php_conf_variable_pair.key }}"'
      loop: "{{ php_conf_section.value | ansible.builtin.dict2items }}"
      loop_control:
        loop_var: php_conf_variable_pair
        label: "{{ php_conf_variable_pair.key }}"
      register: php_conf_file_edited

    - name: Set a fact to indicate that the file has changed  # noqa: no-handler
      ansible.builtin.set_fact:
        php_conf_file_changed: true
      # when: (php_conf_file_comments_edited.changed ) or ( php_conf_file_edited.changed | bool )
      when: php_conf_file_edited.changed | bool

  tags:
    - php
    - php_cfg
    - php_conf
...
