---

- name: Setup deb.sury.org repository - Install apt-transport-https
  ansible.builtin.apt:
    name: apt-transport-https
    state: present
  when: ansible_distribution_major_version is version('10', '<')

- name: "Ensure {{ apt_keyring_dir }} directory exists"
  file:
    path: "{{ apt_keyring_dir }}"
    state: directory
    mode: "755"
    owner: root
    group: root

- name: copy pub.evolix.org GPG key
  ansible.builtin.copy:
    src: pub_evolix.asc
    dest: "{{ apt_keyring_dir }}/pub_evolix.asc"
    mode: "0644"
    owner: root
    group: root

- name: Setup pub.evolix.org repository - Add source list
  ansible.builtin.apt_repository:
    repo: "deb [signed-by={{ apt_keyring_dir }}/pub_evolix.asc] http://pub.evolix.org/evolix {{ ansible_distribution_release }}-php{{ php_version | replace('.', '')}} main"
    filename: evolix-php
    state: present

- name: Setup deb.sury.org repository - Add GPG key
  ansible.builtin.copy:
    src: sury.gpg
    dest: "{{ apt_keyring_dir }}/sury.gpg"
    mode: "0644"
    owner: root
    group: root

- name: Add Sury repository (Debian <12)
  ansible.builtin.apt_repository:
    repo: "deb [signed-by={{ apt_keyring_dir }}/sury.gpg] https://packages.sury.org/php/ {{ ansible_distribution_release }} main"
    filename: sury
    state: present
    update_cache: yes
  when: ansible_distribution_major_version is version('12', '<')

- name: Add Sury repository (Debian >=12)
  ansible.builtin.template:
    src: sury.sources.j2
    dest: /etc/apt/sources.list.d/sury.sources
    mode: "0644"
    force: true
  register: sury_sources
  when: ansible_distribution_major_version is version('12', '>=')

- name: Update APT cache
  ansible.builtin.apt:
    update_cache: yes
  when: sury_sources is changed

- name: "Override package list for Sury (Debian 9 or later)"
  ansible.builtin.set_fact:
    php_stretch_packages:
      - php{{ php_version }}-cli
      - php{{ php_version }}-gd
      - php{{ php_version }}-intl
      - php{{ php_version }}-imap
      - php{{ php_version }}-ldap
      - php{{ php_version }}-mysql
      # php-mcrypt is no longer packaged for PHP 7.2
      - php{{ php_version }}-pgsql
      - php{{ php_version }}-gettext
      - php{{ php_version }}-curl
      - php{{ php_version }}-ssh2
#      - composer
#      - libphp-phpmailer
