# see https://www.howtoforge.com/how-to-use-multiple-php-versions-php-fpm-and-fastcgi-with-ispconfig-3-ubuntu-12.10

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

- name: Ensure PHP53 config directory
  file:
    dest: '{{ item }}'
    state: directory
  with_items:
    - /etc/{{ php_base_dir }}/fpm/pool.d/

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

- block:
  - name: Package Preferences
    template:
      src: preferences/{{ item }}
      dest: /etc/apt/preferences.d/{{ item }}-pin
      owner: root
      group: root
      mode: 0644
    with_items:
      - openssl
      - libssl

  - name: Install Packages
    apt:
      pkg: '{{ packages }}'
      state: present
    vars:
      packages:
        - autoconf2.13
        - automake1.4
        - build-essential
        - libc-client2007e
        - libc-client2007e-dev
        - libfcgi-dev
        - libfcgi0ldbl
        - libfreetype6-dev
        - libgif-dev
        - libgif4
        - libjpeg-progs
        - libjpeg62-dbg
        - libjpeg8
        - libjpeg8-dev
        - libmcrypt-dev
        - libmysqlclient-dev
        - libpcre++-dev
        - libpcre3-dev
        - libpng-dev
        - libssl-dev
        - libx11-dev
        - libxau-dev
        - libxcb1-dev
        - libxdmcp-dev
        - libxml2-dev
        - libxml2-dev
        - libxpm-dev
        - x11proto-core-dev
        - x11proto-input-dev
        - x11proto-kb-dev
        - xtrans-dev

  - name: Link Client Lib
    file:
      src: /usr/lib/libc-client.a
      dest: /usr/lib/x86_64-linux-gnu/libc-client.a
      state: link

  - 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 }}'
    args:
      chdir: /tmp/php53/php-5.3.29
    with_items:
      - ./configure --with-libdir=/lib/x86_64-linux-gnu --enable-fpm --enable-mbstring --enable-sockets --with-zlib --enable-zip --with-imap-ssl --with-imap --with-curl --with-mcrypt --with-gd --with-mysql --with-pdo-mysql --with-mysqli --with-gettext --with-jpeg-dir=/usr --with-png-dir=/usr --with-kerberos --with-openssl --disable-cgi
      - make
      - make install

  - name: Create PHP53 Start Script
    template:
      src: etc-init-d-php53-fpm
      dest: /etc/init.d/php-fpm
      owner: root
      group: root
      mode: 0755

  - name: Add PHP-FPM to Boot-List
    command: update-rc.d php-fpm defaults

  when: php_version_available and '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: 0644
  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: 0644
  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: 0644
  with_items: '{{ php53.liblinks }}'

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

- name: Create PHP53 INI Files
  template:
    src: etc-php5-apache2-php.ini
    dest: /etc/{{ php_base_dir }}/{{ item }}/php.ini
    owner: root
    group: root
    mode: 0644
  with_items:
    - cgi53
    - cli53
    - fpm
