---
- block:
    - name: Remove the resources updater
      file: path={{ resource_updater_dest_dir }} state=absent

    - name: Remove the cron job that runs the resources updater script where is not needed
      cron: name="Resources Updater" special_time=daily job="/usr/local/bin/resources_updater > {{ resource_updater_dest_dir }}/resources_updater.log 2>&1" state=absent

    - name: Remove the resources updater script
      become_user: root
      file: dest=/usr/local/bin/resources_updater state=absent

  become: True
  become_user: '{{ d4science_user }}'
  when: not resource_updater_install
  tags: [ 'smartgears', 'resource_updater' ]

- block:
    - name: Get the resource updater jar file
      maven_artifact: artifact_id={{ resource_updater_name }} version={{ resource_updater_version }} group_id={{ resource_updater_group_id }} classifier={{ resource_updater_classifier }} extension={{ resource_updater_extension }} repository_url={{ smartgears_resource_updater_base_url }} dest={{ smartgears_downloads_dir }}/{{ resource_updater_deps_file }} verify_checksum=always
      register: resource_updater_download

    - name: Remove the old resources updater files
      file: path={{ resource_updater_dest_dir }} state=absent
      when: resource_updater_download is changed

    - name: Create the resources updater destination directory
      file: dest={{ resource_updater_dest_dir }} state=directory

    - name: Copy the resources updater jar file
      copy: src={{ smartgears_downloads_dir }}/{{ resource_updater_deps_file }} dest={{ resource_updater_dest_dir }}/{{ resource_updater_file }} remote_src=yes force=yes

    - name: Install the resources updater properties
      template: src=resources_updater.properties dest={{ resource_updater_dest_dir }}/resources_updater.properties
      register: resource_data_changes

    - name: Install the resources updater script
      become_user: root
      template: src=resources_updater.sh dest=/usr/local/bin/resources_updater owner=root group=root mode=0755

    - name: Install a cron job that runs the resources updater script
      cron: name="Resources Updater" special_time=daily job="/usr/local/bin/resources_updater > {{ resource_updater_dest_dir }}/resources_updater.log 2>&1" state=present
      when: resource_updater_master

    - name: Remove the cron job that runs the resources updater script where is not needed
      cron: name="Resources Updater" special_time=daily job="/usr/local/bin/resources_updater > {{ resource_updater_dest_dir }}/resources_updater.log 2>&1" state=absent
      when: not resource_updater_master

    - name: Run the resources_updater script if the properties file changed
      shell: /usr/local/bin/resources_updater > {{ resource_updater_dest_dir }}/resources_updater.log 2>&1
      when:
        - resource_updater_master
        - resource_data_changes is changed

  become: True
  become_user: '{{ d4science_user }}'
  when: resource_updater_install
  tags: [ 'smartgears', 'resource_updater', 'tomcat' ]