---
- hosts: all

  vars:

  tasks:

#    - name: ensure correct locale LC_ALL
#      lineinfile: dest=/etc/sysconfig/i18n regexp=^LC_ALL= line=LC_ALL="en_US.UTF-8"
#    - name: ensure correct locale LANG
#      lineinfile: dest=/etc/sysconfig/i18n regexp=^LANG= line=LANG="en_US.UTF-8"

    # - name: set .bashrc
    #   copy: src=files/.bashrc dest=/home/vagrant/.bashrc force=yes
    # - name: set .profile
    #   copy: src=files/.profile dest=/home/vagrant/.profile force=yes
    - name: set hostname
      hostname: name=corpus-build

    - name: check yum base last update
      stat: path=/var/cache/yum/x86_64/6/base/cachecookie
      register: yum_cache_stat_base

    - name: additional repos install
      yum: name={{item}} state=latest enablerepo=extras
      with_items:
        - epel-release

    # Remi yum repository.
    - stat: path=/etc/yum.repos.d/remi.repo
      register: remi_repo

    - name: Download Remi repo.
      get_url: url=http://rpms.famillecollet.com/enterprise/remi-release-6.rpm dest=/tmp/
      when: remi_repo.stat.exists == False

    - name: Install Remi repo.
      command: rpm -Uvh --force /tmp/remi-release-6.rpm creates=/etc/yum.repos.d/remi.repo
      when: remi_repo.stat.exists == False

    - name : delete remi rpm
      file: path=/tmp/remi-release-6.rpm state=absent

    - name: additional repos install epel
      ini_file:
        dest: /etc/yum.repos.d/epel.repo
        section: epel
        option: enabled
        value: 1

    - name: yum update
      yum: name=* update_cache=yes state=latest
#      when: ansible_date_time.epoch|float - yum_cache_stat_base.stat.mtime > 60*60*12*24

    - name: install packages
      yum: name={{item}} state=latest enablerepo=remi
      with_items:
        - git-core
        - zlib
        - zlib-devel
        - gcc-c++
        - patch
        - readline
        - readline-devel
        - libyaml-devel
        - libffi-devel
        - openssl-devel
        - make
        - bzip2
        - autoconf
        - automake
        - libtool
        - bison
        - curl
        - sqlite-devel
        - rpm-build
        - redhat-rpm-config
        - php56
        - php56-php-pdo
        - php56-php-mbstring
        - php56-php-mcrypt

    - name: activate remi php scl
      lineinfile: dest=/home/vagrant/.bash_profile regexp="^source \/opt\/remi\/php56\/enable$" line="source /opt/remi/php56/enable"

    - stat: path=/home/vagrant/.rbenv
      register: rbenv_stat

    - name: clone rbenv github repo
      git: repo=https://github.com/rbenv/rbenv.git dest=/home/vagrant/.rbenv
      become: yes
      become_user: vagrant
      when: rbenv_stat.stat.exists == False

    - name: configure rbenv
      command: chdir=/home/vagrant/.rbenv {{ item }}
      with_items:
          - src/configure
          - /usr/bin/make -C src
      become: yes
      become_user: vagrant
      when: rbenv_stat.stat.exists == False

    - name: add rbenv to path
      lineinfile: dest=/home/vagrant/.bash_profile regexp="^PATH=" line="PATH=$PATH:$HOME/.local/bin:$HOME/bin:$HOME/.rbenv/bin"

    - name: add rbenv init to profile
      lineinfile: dest=/home/vagrant/.bash_profile regexp="^eval \"\$\(rbenv init \-\)\"$" line="eval \"$(rbenv init -)\""

    - name: clone ruby-build github repo
      git: repo=https://github.com/rbenv/ruby-build.git dest=/home/vagrant/.rbenv/plugins/ruby-build
      become: yes
      become_user: vagrant
      when: rbenv_stat.stat.exists == False

    - name: install ruby 2.3.0
      command: chdir=/home/vagrant/.rbenv creates=/home/vagrant/.rbenv/versions/2.3.0 /home/vagrant/.rbenv/bin/rbenv install 2.3.0
      become: yes
      become_user: vagrant

    - name: make ruby 2.3.0 global
      command: chdir=/home/vagrant/.rbenv /home/vagrant/.rbenv/bin/rbenv global 2.3.0
      become: yes
      become_user: vagrant

    - name: install fpm
      shell: source ~/.bash_profile && gem install fpm
      args:
          chdir: /home/vagrant
          executable: /usr/bin/bash
          creates: /home/vagrant/.rbenv/shims/fpm
      become: yes
      become_user: vagrant
