---

- name: "Set php version to 7.4 if Sury repo is not enabled"
  ansible.builtin.set_fact:
    php_version: "7.4"
  when: not (php_sury_enable | bool)
  check_mode: no

- name: "Set variables (Debian 11)"
  ansible.builtin.set_fact:
    php_cli_defaults_ini_file: /etc/php/{{ php_version }}/cli/conf.d/z-evolinux-defaults.ini
    php_cli_custom_ini_file: /etc/php/{{ php_version }}/cli/conf.d/zzz-evolinux-custom.ini
    php_apache_defaults_ini_file: /etc/php/{{ php_version }}/apache2/conf.d/z-evolinux-defaults.ini
    php_apache_custom_ini_file: /etc/php/{{ php_version }}/apache2/conf.d/zzz-evolinux-custom.ini
    php_fpm_defaults_ini_file: /etc/php/{{ php_version }}/fpm/conf.d/z-evolinux-defaults.ini
    php_fpm_custom_ini_file: /etc/php/{{ php_version }}/fpm/conf.d/zzz-evolinux-custom.ini
    php_fpm_debian_default_pool_file: /etc/php/{{ php_version }}/fpm/pool.d/www.conf
    php_fpm_default_pool_file: /etc/php/{{ php_version }}/fpm/pool.d/www-evolinux-defaults.conf
    php_fpm_default_pool_custom_file: /etc/php/{{ php_version }}/fpm/pool.d/www-evolinux-zcustom.conf
    php_fpm_default_pool_socket: /var/run/php/php{{ php_version }}-fpm.sock
    php_fpm_service_name: php{{ php_version }}-fpm

# Packages

- name: "Set package list (Debian 11)"
  ansible.builtin.set_fact:
    php_stretch_packages:
      - php-cli
      - php-gd
      - php-intl
      - php-imap
      - php-ldap
      - php-mysql
      - php-pgsql
      - php-sqlite3
      - php-curl
      - php-ssh2
      - php-xml
      - php-zip
      - composer
      - libphp-phpmailer

- ansible.builtin.include: sury_pre.yml
  when: php_sury_enable

- name: "Install PHP packages (Debian 11)"
  ansible.builtin.apt:
    name: '{{ php_stretch_packages }}'
    state: present

- name: "Install mod_php packages (Debian 11)"
  ansible.builtin.apt:
    name:
      - libapache2-mod-php
      - php
    state: present
  when: php_apache_enable

- name: "Install PHP FPM packages (Debian 11)"
  ansible.builtin.apt:
    name:
      - php{{ php_version }}-fpm
      - php{{ php_version }}
    state: present
  when: php_fpm_enable

# Configuration

- name: "Enforce permissions on PHP directory (Debian 11)"
  ansible.builtin.file:
    dest: "{{ item }}"
    mode: "0755"
  with_items:
    - /etc/php
    - /etc/php/{{ php_version }}

- ansible.builtin.include: config_cli.yml
- name: "Enforce permissions on PHP cli directory (Debian 11)"
  ansible.builtin.file:
    dest: /etc/php/{{ php_version }}/cli
    mode: "0755"

- ansible.builtin.include: config_fpm.yml
  when: php_fpm_enable

- name: "Enforce permissions on PHP fpm directory (Debian 11)"
  ansible.builtin.file:
    dest: /etc/php/{{ php_version }}/fpm
    mode: "0755"
  when: php_fpm_enable

- ansible.builtin.include: config_apache.yml
  when: php_apache_enable

- name: "Enforce permissions on PHP apache2 directory (Debian 11)"
  ansible.builtin.file:
    dest: /etc/php/{{ php_version }}/apache2
    mode: "0755"
  when: php_apache_enable

#- include: sury_post.yml
#  when: php_sury_enable
