- hosts: localhost
  connection: local
  vars:
    build_dir: "{{playbook_dir}}/build/tmp/rc/"
  tasks:

    - name: mkdir build folder
      file:
        path: "{{playbook_dir}}/build/tmp"
        state: directory

    - name: clear checkout dir if exists
      file:
        path: "{{build_dir}}"
        state: absent

    - name: checkout 
      hg:
        repo: "{{rc_repo}}"
        version: "{{rc_version}}"
        force: yes
        dest: "{{build_dir}}"

- hosts: remote
  vars:
    src_dir: "{{playbook_dir}}/build/tmp/rc/src/"
  tasks:

    - name: create the destination directory
      file:
        path: "{{remote_path}}"
        state: directory
        owner: "{{ansible_user_id}}"
      become: yes

    - name: sync the sources
      synchronize:
        src: "{{src_dir}}"
        dest: "{{remote_path}}"

    - name: clean previous version of composer
      file:
        path: /tmp/composer/composer
        state: absent

    - name: create the destination directory
      file:
        path: /tmp/composer
        state: directory

    - name: download new version of composer
      get_url:
        url: https://getcomposer.org/composer.phar
        dest: /tmp/composer/composer
        mode: 0755

    - name: clean composer
      file:
        path: "{{remote_path}}/vendor"
        state: absent

    - name: call composer
      composer:
        command: install
        working_dir: "{{remote_path}}"
      environment:
        PATH: /tmp/composer:/usr/bin

    - name: create env file if needed
      template:
        src: env.j2
        dest: "{{remote_path}}/.env"