---
- block:
    - name: Remove the old service files
      file: path={{ item }} state=absent
      with_items:
        - '{{ smartgears_instance_path }}/webapps/{{ smartgears_generic_service_name }}'
        - '{{ smartgears_instance_path }}/webapps/{{ smartgears_generic_service_name }}.{{ smartgears_generic_service_extension }}'
      notify: Restart smartgears
      check_mode: yes

  become: True
  become_user: '{{ d4science_user }}'
  tags: [ 'smartgears_generic_service' ]
  when: not smartgears_generic_service_install 

- block:
    - name: Get the smartgears-service war
      maven_artifact: artifact_id={{ smartgears_generic_service_name }} version={{ smartgears_generic_service_version | default(omit) }} group_id={{ smartgears_generic_service_group_id }} extension={{ smartgears_generic_service_extension | default('war') }} repository_url={{ smartgears_global_base_url }} dest={{ smartgears_downloads_dir }}/{{ smartgears_generic_service_file }} verify_checksum=always
      register: smartgears_generic_service_download
  
    - name: Remove the old service files
      file: path={{ smartgears_instance_path }}/webapps/{{ smartgears_generic_service_name }} state=absent
      notify: Restart smartgears
      when: smartgears_generic_service_download is changed
      check_mode: yes
  
    - name: Copy the service under the webapps directory
      copy: src={{ smartgears_downloads_dir }}/{{ smartgears_generic_service_file }} dest={{ smartgears_instance_path }}/webapps/{{ smartgears_generic_service_name }}.{{ smartgears_generic_service_extension }} remote_src=yes force=yes
      notify: Restart smartgears
      check_mode: yes

    - name: Create the service webapp directory
      file: dest={{ smartgears_instance_path }}/webapps/{{ smartgears_generic_service_name }} state=directory
      when:  smartgears_generic_service_download

    - name: Unarchive the service  war file
      unarchive: copy=no src={{ smartgears_downloads_dir }}/{{ smartgears_generic_service_file }} dest={{ smartgears_instance_path }}/webapps/{{ smartgears_generic_service_name }}
      notify: Restart smartgears


  become: True
  become_user: '{{ d4science_user }}'
  tags: [ 'smartgears_generic_service' ]
  when: smartgears_generic_service_install 
  check_mode: yes