---
- block:
    - name: Remove the installed home library whe we do not want it installed
      file: dest={{ item }} state=absent
      with_items:
        - '{{ smartgears_instance_path }}/webapps/{{ home_library_name }}'
        - '{{ smartgears_instance_path }}/webapps/{{ home_library_name }}.war'
      notify: Restart smartgears

  become: True
  become_user: '{{ smartgears_user }}'
  when: not home_library_install
  tags: [ 'smartgears', 'home_library', 'tomcat' ]

- block:
    - maven_artifact: artifact_id={{ home_library_name }} version={{ home_library_version | default(omit) }} group_id={{ home_library_group_id }} extension={{ home_library_extension | default('war') }} repository_url={{ smartgears_global_base_url }} dest={{ smartgears_downloads_dir }}/{{ home_library_file }}
      register: home_library_download

    - name: Remove the installed home library before upgrading
      file: dest={{ smartgears_instance_path }}/webapps/{{ home_library_name }} state=absent
      when: home_library_download is changed
      notify: Restart smartgears

    - name: Copy the home library war
      copy: src={{ smartgears_downloads_dir }}/{{ home_library_file }} dest={{ smartgears_instance_path }}/webapps/{{ home_library_name }}.{{ home_library_extension | default('war') }} remote_src=yes force=yes
      notify: Restart smartgears
      
    - name: Create the JackRabbit persistence directory
      become_user: root
      file: dest={{ jackrabbit_data_dir }} state=directory owner={{ smartgears_user }} group={{ smartgears_user }}

    - name: Verify if the JackRabbit persistence directory is in the place where the service expects it
      stat: path={{ smartgears_instance_path }}/jackrabbit
      register: jackrabbit_data_path

    - name: Link the data dir to the place where jackrabbit expects it
      file: src={{ jackrabbit_data_dir }} dest={{ smartgears_instance_path }}/jackrabbit state=link
      when: not jackrabbit_data_path.stat.exists

    - name: Install the repository configuration files
      template: src={{ item }}.j2 dest={{ jackrabbit_data_dir }}/{{ item }}
      with_items: '{{ jackrabbit_config_files }}'
      notify: Restart smartgears

  become: True
  become_user: '{{ smartgears_user }}'
  when: home_library_install
  tags: [ 'smartgears', 'home_library', 'tomcat' ]