---
- name: Include OS-specific variables.
  include_vars: "{{ ansible_os_family }}.yml"

- name: Define php_xdebug_module_path.
  set_fact:
    php_xdebug_module_path: "{{ __php_xdebug_module_path }}"
  when: php_xdebug_module_path is not defined

- name: Define php_xdebug_config_filename.
  set_fact:
    php_xdebug_config_filename: "{{ __php_xdebug_config_filename }}"
  when: php_xdebug_config_filename is not defined

- name: Ensure dependencies for building from source are installed (RedHat).
  yum: "name={{ item }} state=installed"
  with_items:
    - '@development'
  when: ansible_os_family == 'RedHat'

- name: Ensure dependencies for building from source are installed (Debian).
  apt: "name={{ item }} state=installed"
  with_items:
    - build-essential
  when: ansible_os_family == 'Debian'

- name: Untar Xdebug.
  unarchive:
    src: "https://xdebug.org/files/xdebug-{{ php_xdebug_version }}.tgz"
    dest: "{{ workspace }}"
    creates: "{{ workspace }}/xdebug-{{ php_xdebug_version }}"
    copy: no

- name: Build Xdebug.
  shell: >
    {{ item }}
    chdir={{ workspace }}/xdebug-{{ php_xdebug_version }}
    creates={{ workspace }}/xdebug-{{ php_xdebug_version }}/modules/xdebug.so
  with_items:
    - phpize
    - ./configure
    - make

- name: Ensure Xdebug module path exists.
  file:
    path: "{{ php_xdebug_module_path }}"
    state: directory
    owner: root
    group: root
    mode: 0755

- name: Move Xdebug module into place.
  copy:
    src: "{{ workspace }}/xdebug-{{ php_xdebug_version }}/modules/xdebug.so"
    dest: "{{ php_xdebug_module_path }}/xdebug-{{ php_xdebug_version }}.so"
    remote_src: True
  notify: restart webserver

- include: configure.yml
