---
- hosts: all
  vars:
    http_port: 80
    max_clients: 200
  sudo: yes

  tasks:

  - name: "(VisualEditor) Authentication for parsoid installation"
    sudo: yes
    command: "{{ item }}"
    with_items:
    - gpg --keyserver keys.gnupg.net --recv-keys 5C927F7C
    - gpg -a --export 5C927F7C | apt-key add -

  - name: "(VisualEditor) Add parsoid source url to sources.list"
    apt_repository: repo='deb [arch=amd64] http://parsoid.wmflabs.org:8080/deb wmf-production main' state=present

  - name: "apt-get update"
    sudo: yes
    apt: update_cache=yes

#  - name: "apt-get upgrade"
#    apt: upgrade=dist

  - name: "(VisualEditor) Ensure NodeJS is installed with the latest version"
    apt: name=nodejs state=latest

  - name: "(VisualEditor) Ensure Parsoid is installed with the latest version"
    apt: name=parsoid state=latest force=yes

  - name: "(VisualEditor) Write the Parsoid settings file"
    sudo: yes
    template: src=configuration/parsoid_settings.js dest=/etc/mediawiki/parsoid/settings.js

  - name: "(VisualEditor) Restarting Parsoid"
    sudo: yes
    service: name=parsoid state=restarted

  - name: "Ensure Apache is installed with the lastest version"
    apt: name=apache2 state=latest

  - name: "Enabling Apache Mod Rewrite"
    sudo: yes
    action: command a2enmod rewrite

  - name: "Ensure MySQL is installed with the latest version"
    apt: name=mysql-server state=latest

  - name: "Installing PHP and dependencies"
    action: apt pkg={{item}} state=installed
    with_items:
    - php5
    - php5-cli
    - php5-common
    - php5-curl
    - php5-dev
    - php5-gd
    - php5-geoip
    - php5-imagick
    - php5-intl
    - php5-json
    - php5-mcrypt
    - php5-memcache
    - php5-memcached
    - php5-mysql
    - php5-oauth
    - php5-sqlite
    - php5-xcache
    - php5-xdebug
    - php5-xsl
    - php5-xmlrpc

  - name: "Ensure GIT is installed with the latest version"
    apt: name=git state=latest

  - name: "Write the Apache Virtual Host"
    sudo: yes
    template: src=configuration/apache-virtual-host-model.conf dest=/etc/apache2/sites-available/basefwiki.conf

  - name: "Enabling the Apache Virtual Host"
    sudo: yes
    action: command a2ensite basefwiki.conf

  - name: "Disabling default Virtual Host"
    sudo: yes
    action: command a2dissite 000-default.conf

  - name: "Replacing the Apache User"
    sudo: yes
    replace: dest=/etc/apache2/envvars regexp='APACHE_RUN_USER=www-data' replace='APACHE_RUN_USER=vagrant'

  - name: "Replacing the Apache Group"
    sudo: yes
    replace: dest=/etc/apache2/envvars regexp='APACHE_RUN_GROUP=www-data' replace='APACHE_RUN_GROUP=vagrant'

  - name: install required packages
    apt: pkg={{ item }} state=latest force=yes update_cache=yes
    with_items:
      - php5-dev
      - php5-mysql
      - gcc
      - libpcre3-dev
      - make

  - name: "Restarting Apache"
    sudo: yes
    service: name=apache2 state=restarted

  - name: "Creating database"
    sudo: yes
    shell: mysql --defaults-file=/etc/mysql/debian.cnf --execute="CREATE DATABASE basefwiki"
    register: create_database_task   
    ignore_errors: True

  - name: "Copying database dump to inside the Virtual Machine"
    sudo: yes
    copy: src=database/database_dump.sql dest=/tmp/database_dump.sql
    when: create_database_task|success

  - name: "Importing MySQL Database"
    shell: mysql --defaults-file=/etc/mysql/debian.cnf basefwiki < /tmp/database_dump.sql
    when: create_database_task|success

  - name: "Changing database credentials to user, pass and db 'basefwiki'"
    shell: mysql --defaults-file=/etc/mysql/debian.cnf --execute="GRANT ALL PRIVILEGES ON basefwiki.* TO basefwiki@localhost IDENTIFIED BY 'basefwiki'"
    when: create_database_task|success

