---
- block: 
    - name: Get and unpack the dataminer application
      maven_artifact: artifact_id={{ dataminer_wps_name }} version={{ dataminer_wps_version | default(omit) }} group_id={{ dataminer_wps_group_id }} extension={{ dataminer_wps_extension | default('war') }} repository_url={{ smartgears_global_base_url }} dest={{ smartgears_downloads_dir }}/{{ dataminer_wps_file }} verify_checksum=always
      register: dataminer_app_downloaded

    - name: Remove the old dataminer installation
      file: path={{ smartgears_instance_path }}/webapps/{{ dataminer_wps_name }} state=absent
      when: dataminer_app_downloaded is changed

    - name: Create the dataminer wps working directory
      file: path={{ smartgears_instance_path }}/webapps/{{ dataminer_wps_name }} state=directory owner={{ smartgears_user }} group={{ smartgears_user }}
  
    - name: Unarchive the dataminer_wps war
      unarchive: copy=no src={{ smartgears_downloads_dir }}/{{ dataminer_wps_file }} dest={{ smartgears_instance_path }}/webapps/{{ dataminer_wps_name }} creates='{{ smartgears_instance_path }}/webapps/{{ dataminer_wps_name }}/WEB-INF/lib'
      register: dataminer_updated
      notify: Restart smartgears

    - name: Install the dataminer web.xml
      template: src=web.xml.j2 dest={{ smartgears_instance_path }}/webapps/{{ dataminer_wps_name }}/WEB-INF/web.xml
      notify: Restart smartgears
      tags: [ 'tomcat', 'dataminer', 'wps', 'wps_webxml' ]

    - name: Install the old properties files
      copy: src={{ item }} dest={{ smartgears_instance_path }}/webapps/{{ dataminer_wps_name }}/ecocfg/{{ item }} force=yes
      with_items: '{{ dataminer_properties_files }}'
      tags: [ 'tomcat', 'dataminer', 'wps', 'wps_properties' ]

    - name: Install the script that fixes the WPS configuration
      become_user: "{{ d4science_ansible_become_user | default('root') }}"
      template: src=wps-config-fixer.j2 dest=/usr/local/bin/wps-config-fixer owner=root group=root mode=0555
      with_items: '{{ tomcat_m_instances }}'
      tags: [ 'tomcat', 'dataminer', 'wps', 'wps_config' ]

    - name: Fix the WPS config file
      shell: /usr/local/bin/wps-config-fixer
      notify: Restart smartgears
      when: dataminer_updated is changed 
      tags: [ 'tomcat', 'dataminer', 'wps', 'wps_config' ]

    - name: Force the fix of the WPS config file
      shell: /usr/local/bin/wps-config-fixer
      notify: Restart smartgears
      when: dataminer_force_reconfiguration is defined and dataminer_force_reconfiguration
      tags: [ 'tomcat', 'dataminer', 'wps', 'wps_config' ]

    - name: Remove the file that lists the installed algorithms, after an upgrade
      file: dest={{ smartgears_user_home }}/wps_algorithms_install_log/already_installed_algorithms.txt state=absent 
      register: reinstall_algorithms
      when: dataminer_updated is changed 

    - name: After an update, reinstall the algorithms
      shell: /usr/local/bin/algorithms-updater &   
      when: 
        - reinstall_algorithms is changed 
        - dataminer_algorithms_installer

  become: True
  become_user: '{{ smartgears_user }}'
  tags: [ 'tomcat', 'dataminer', 'wps' ]

- block: 
    - name: Reconfigure the Java environment for R
      shell: export JAVA_HOME=/usr/lib/jvm/java-8-oracle ; export J2SDKDIR=/usr/lib/jvm/java-8-oracle ; export J2REDIR=/usr/lib/jvm/java-8-oracle/jre ; R CMD javareconf

  become: True
  become_user: root
  tags: [ 'tomcat', 'dataminer', 'wps' ]