---
- name: Ensure dependencies for building from source are installed (RedHat).
  ansible.builtin.package:
    name:
      - autoconf
      - automake
      - libtool
      - bison
      - make
      - re2c
      - sqlite-devel
      - oniguruma-devel
      - curl-devel
      - recode-devel
      - aspell-devel
      - libxml2-devel
      - pkgconfig
      - libmcrypt-devel
      - t1lib-devel
      - libXpm-devel
      - libpng-devel
      - libjpeg-turbo-devel
      - bzip2-devel
      - openssl-devel
      - freetype-devel
      - libicu-devel
      - mariadb-devel
      - gmp-devel
    state: present
  when: ansible_facts.os_family == 'RedHat'

- name: Update apt cache (Debian).
  ansible.builtin.apt:
    update_cache: true
    cache_valid_time: 86400
  when: ansible_facts.os_family == 'Debian'

- name: Ensure dependencies for building from source are installed (Debian).
  ansible.builtin.apt:
    name:
      - build-essential
      - autoconf
      - automake
      - libtool
      - bison
      - pkg-config
      - re2c
      - libsqlite3-dev
      - libonig-dev
      - libxml2-dev
      - libcurl4-openssl-dev
      - libbz2-dev
      - libjpeg-dev
      - libpng-dev
      - libxpm-dev
      - libfreetype6-dev
      - libgmp3-dev
      - libmcrypt-dev
      - libmariadb-dev
      - libpspell-dev
      - librecode-dev
      - libssl-dev
    state: present
  when: ansible_facts.os_family == 'Debian'

- name: Define php_fpm_daemon (if not defined already).
  ansible.builtin.set_fact:
    php_fpm_daemon: "php-fpm"
  when: php_fpm_daemon is not defined

- name: Check if gmp.h is already in a location accessible to gcc.
  ansible.builtin.stat:
    path: /usr/include/gmp.h
  register: gmp_file

- name: Ensure gmp.h is symlinked into a location accessible to gcc.
  ansible.builtin.file: # noqa: risky-file-permissions
    src: "{{ php_source_install_gmp_path }}"
    dest: /usr/include/gmp.h
    state: link
  when: not gmp_file.stat.exists

- name: Check if PHP is installed.
  ansible.builtin.command: which php
  changed_when: false
  failed_when: false
  register: php_installed

- name: Clone the PHP repository.
  ansible.builtin.git:
    repo: "{{ php_source_repo }}"
    dest: "{{ php_source_clone_dir }}"
    version: "{{ php_source_version }}"
    accept_hostkey: true
    depth: "{{ php_source_clone_depth }}"
  when: php_installed.rc != 0

- name: Ensure PHP installation path exists.
  ansible.builtin.file:
    path: "{{ php_source_install_path }}"
    state: directory
    mode: '0755'
  when: php_installed.rc != 0

- name: Build configure script.
  ansible.builtin.command: ./buildconf --force
  args:
    chdir: "{{ php_source_clone_dir }}"
  changed_when: php_installed.rc != 0
  when: php_installed.rc != 0

- name: Run configure script.
  ansible.builtin.command: "{{ php_source_configure_command }}"
  args:
    chdir: "{{ php_source_clone_dir }}"
  changed_when: php_installed.rc != 0
  when: php_installed.rc != 0

- name: Make and install PHP.
  ansible.builtin.command: "{{ item }}"
  args:
    chdir: "{{ php_source_clone_dir }}"
  with_items:
    - "{{ php_source_make_command }}"
    - make install
  changed_when: php_installed.rc != 0
  when: php_installed.rc != 0

- name: Ensure php executable is symlinked into a standard path.
  ansible.builtin.file: # noqa: risky-file-permissions
    src: "{{ php_source_install_path }}/bin/php"
    dest: /usr/bin/php
    state: link

# PHP FPM configuration.
- name: Ensure php-fpm executable is symlinked into a standard path.
  ansible.builtin.file: # noqa: risky-file-permissions
    src: "{{ php_source_install_path }}/sbin/php-fpm"
    dest: "/usr/sbin/{{ php_fpm_daemon }}"
    state: link
  when: "'--enable-fpm' in php_source_configure_command"

- name: Ensure php-fpm init script is installed.
  ansible.builtin.template:
    src: fpm-init.j2
    dest: "/etc/init.d/{{ php_fpm_daemon }}"
    mode: '0755'
  when: "'--enable-fpm' in php_source_configure_command"
  notify: Restart php-fpm

- name: Ensure php-fpm config directory exists.
  ansible.builtin.file:
    path: "{{ php_fpm_conf_path }}"
    state: directory
    mode: '0755'
  when: "'--enable-fpm' in php_source_configure_command"

- name: Ensure php-fpm config file is installed.
  ansible.builtin.template:
    src: php-fpm.conf.j2
    dest: "{{ php_fpm_conf_path }}/php-fpm.conf"
    mode: '0644'
  when: "'--enable-fpm' in php_source_configure_command"
  notify: Restart php-fpm
