---
- name: Ensure dependencies for building from source are installed (RedHat).
  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_os_family == 'RedHat'

- name: Update apt cache (Debian).
  apt: update_cache=yes cache_valid_time=86400
  when: ansible_os_family == 'Debian'

- name: Ensure dependencies for building from source are installed (Debian).
  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
      - libmariadbclient-dev
      - libpspell-dev
      - librecode-dev
      - libssl-dev
    state: present
  when: ansible_os_family == 'Debian'

- name: Define php_fpm_daemon (if not defined already).
  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.
  stat: path=/usr/include/gmp.h
  register: gmp_file

- name: Ensure gmp.h is symlinked into a location accessible to gcc.
  file:  # noqa 208
    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.
  command: which php
  changed_when: false
  failed_when: false
  register: php_installed

- name: Clone the PHP repository.
  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.
  file:
    path: "{{ php_source_install_path }}"
    state: directory
    mode: 0755
  when: php_installed.rc != 0

- name: Build configure script.
  command: >
    ./buildconf --force
    chdir={{ php_source_clone_dir }}
  when: php_installed.rc != 0

- name: Run configure script.
  command: >
    {{ php_source_configure_command }}
    chdir={{ php_source_clone_dir }}
  when: php_installed.rc != 0

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

- name: Ensure php executable is symlinked into a standard path.
  file:  # noqa 208
    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.
  file:  # noqa 208
    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.
  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.
  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.
  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