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

- name: Define php_xhprof_module_path.
  set_fact:
    php_xhprof_module_path: "{{ __php_xhprof_module_path }}"
  when: php_xhprof_module_path is not defined

- name: Define php_xhprof_config_filename.
  set_fact:
    php_xhprof_config_filename: "{{ __php_xhprof_config_filename }}"
  when: php_xhprof_config_filename is not defined

# Setup/install tasks.
- name: Ensure dependencies for installing XHProf are present.
  package: "name={{ item }} state=installed"
  with_items:
    - make
    - gcc
    - unzip
    - graphviz

- name: Download and untar XHProf.
  unarchive:
    src: "{{ xhprof_download_url }}"
    dest: "{{ workspace }}"
    copy: no
    creates: "{{ workspace }}/{{ xhprof_download_folder_name }}"

- name: Build XHProf.
  shell: >
    {{ item }}
    chdir={{ workspace }}/{{ xhprof_download_folder_name }}/extension
    creates={{ workspace }}/{{ xhprof_download_folder_name }}/extension/modules/xhprof.so
  with_items:
    - phpize
    - ./configure
    - make
  notify: restart webserver

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

- name: Move XHProf module into place.
  shell: >
    cp {{ workspace }}/{{ xhprof_download_folder_name }}/extension/modules/xhprof.so {{ php_xhprof_module_path }}/xhprof.so
    creates={{ php_xhprof_module_path }}/xhprof.so
  notify: restart webserver

- name: Move XHProf PHP library into place.
  shell: >
    cp -r {{ workspace }}/{{ xhprof_download_folder_name }}/xhprof_lib {{ php_xhprof_lib_dir }}
    creates={{ php_xhprof_lib_dir }}/utils/xhprof_lib.php

- name: Move XHProf UI into place.
  shell: >
    cp -r {{ workspace }}/{{ xhprof_download_folder_name }}/xhprof_html {{ php_xhprof_html_dir }}
    creates={{ php_xhprof_html_dir }}/index.php

- include: configure.yml

- name: Ensure XHProf output directory exists.
  file:
    path: "{{ xhprof_output_dir }}"
    state: directory
    mode: 0777