---
# NOTE: Install as the smartgears user so we do not mess with the permissions
- name: Get the 52 north application war file
  remote_user: root
  become: True
  become_user: '{{ smartgears_user }}'
  get_url: url={{ dataminer_52north_webapp_url }} dest={{ smartgears_user_home }}/{{ dataminer_52north_webapp_file }}
  when: dataminer_app_install
  tags: [ 'tomcat', 'dataminer', 'wps' ]
  
- name: Create the 52 north webapp directory and subdirectories
  remote_user: root
  become: True
  become_user: '{{ smartgears_user }}'
  file: dest={{ smartgears_instance_path }}/webapps/{{ item }} state=directory mode=0755
  with_items:
    - '{{ dataminer_52north_webapp_name }}'
    - '{{ dataminer_52north_webapp_name }}/persistence'
#    - '{{ dataminer_52north_webapp_name }}/ecocfg'
  when: dataminer_app_install
  tags: [ 'tomcat', 'dataminer', 'wps' ]

- name: Unarchive the 52 north webapp distribution
  remote_user: root
  become: True
  become_user: '{{ smartgears_user }}'
  unarchive: copy=no src={{ smartgears_user_home }}/{{ dataminer_52north_webapp_file }} dest={{ smartgears_instance_path }}/webapps/{{ dataminer_52north_webapp_name }}
  args:
    creates: '{{ smartgears_instance_path }}/webapps/{{ dataminer_52north_webapp_name }}/WEB-INF/classes/52N_license.txt'
  register: install_52_north
  notify: Restart smartgears
  when: dataminer_app_install
  tags: [ 'tomcat', 'dataminer', 'wps' ]

- name: Remove the original web.xml file
  remote_user: root
  become: True
  become_user: '{{ smartgears_user }}'
  file: dest={{ smartgears_instance_path }}/webapps/{{ dataminer_52north_webapp_name }}/WEB-INF/web.xml state=absent
  notify: Restart smartgears
  when: ( install_52_north | changed )
  tags: [ 'tomcat', 'dataminer', 'wps' ]

# Transform the 52n-wps app in a gcube enabled app
- name: Download the gcube web.xml config file
  remote_user: root
  become: True
  become_user: '{{ smartgears_user }}'
  get_url: url={{ dataminer_gcube_web_xml }} dest={{ smartgears_instance_path }}/webapps/{{ dataminer_52north_webapp_name }}/WEB-INF/web.xml
  notify: Restart smartgears
  when: dataminer_app_install
  tags: [ 'tomcat', 'dataminer', 'wps' ]

- name: Download the gcube gcube-app.xml config file
  remote_user: root
  become: True
  become_user: '{{ smartgears_user }}'
  get_url: url={{ dataminer_gcube_app_xml }} dest={{ smartgears_instance_path }}/webapps/{{ dataminer_52north_webapp_name }}/WEB-INF/gcube-app.xml
  notify: Restart smartgears
  when: dataminer_app_install
  tags: [ 'tomcat', 'dataminer', 'wps' ]