---
# file: roles/php/tasks/php53.yml

- name: "Ensure PHP53 config directory"
  file:
    dest='{{ item }}'
    state='directory'
  with_items:
    - '/etc/php5/cli53/conf.d/'
    - '/etc/php5/cgi53/conf.d/'

- name: "Check PHP 5.3 Requirement"
  shell: php --version
  register: php_version_available
  changed_when: false

- block:
  - name: "Make sure an empty directory /tmp/php53 exists"
    file:
      path='/tmp/php53'
      state='{{ item }}'
    with_items:
      - absent
      - directory

  - name: "Download PHP 5.3"
    get_url:
      url='http://in1.php.net/distributions/php-5.3.29.tar.bz2'
      dest='/tmp/php-5.3.29.tar.bz2'

  - name: "Unpack PHP 5.3"
    unarchive:
      src='/tmp/php-5.3.29.tar.bz2'
      dest='/tmp/php53'
      copy=no

  - name: "Compile PHP 5.3"
    shell: "{{ item }} chdir=/tmp/php53/php-5.3.29"
    with_items:
      - ./configure --enable-mbstring
      - make
      - make install

  when: '"PHP 5.3.29" not in php_version_available.stdout'

- name: "Copy PHP53 Libraries"
  copy:
    src='php53/libs/{{ item.file }}'
    dest='{{ php53.libdir }}/{{ item.file }}'
    owner='root'
    group='root'
    mode='644'
  with_items: '{{ php53.libraries }}'

- name: "Ensure PHP53 Library Links"
  file:
    src='{{ php53.libdir }}/{{ item.file }}'
    dest='{{ php53.libdir }}/{{ item.link }}'
    state='link'
    owner='root'
    group='root'
    mode='644'
  with_items: '{{ php53.libraries }}'

- name: "Ensure extra Library Links"
  file:
    src='{{ item.src }}'
    dest='{{ php53.libdir }}/{{ item.link }}'
    state='link'
    owner='root'
    group='root'
    mode='644'
  with_items: '{{ php53.liblinks }}'

- name: "Copy PHP53 Modules"
  copy:
    src='php53/{{ item }}.so'
    dest='{{ php53.extdir }}/{{ item }}.so'
    owner='root'
    group='root'
    mode='644'
  with_items: '{{ php53.modules }}'

- name: "Enable PHP53 Modules"
  file:
    src='/etc/php5/{{ php_conf_dir }}/{{ item.0 }}.ini'
    dest='/etc/php5/{{ item.1 }}/conf.d/{{ item.0 }}.ini'
    state='link'
    owner='root'
    group='root'
    mode='644'
  with_nested:
    - '{{ php53.modules }}'
    - ['cli53', 'cgi53']

- name: "Create PHP53 INI Files"
  template:
    src='etc-php5-apache2-php.ini'
    dest='/etc/php5/{{ item }}/php.ini'
    owner='root'
    group='root'
    mode='644'
  with_items:
    - 'cgi53'
    - 'cli53'
