---
- block:
    - name: Stop the service when upgrading
      #service: name='tomcat-instance-{{ authorization_http_port }}' state=stopped      
      service: name=tomcat-instance-8080 state=stopped      
      when: authorization_service_upgrade

    - name: Remove the installed authorization service before upgrading
      file: dest={{ item }} state=absent
      with_items:
        - '{{ auth_instance_path }}/webapps/authorization-service'
        - '{{ auth_instance_path }}/webapps/authorization-service.war'
      when: authorization_service_upgrade or not authorization_service_install

    # - name: Get the authorization service war file
    #   get_url: url={{ authorization_service_url }} dest={{ auth_instance_path }}/webapps/{{ authorization_service_file }}

    # - name: Unpack the authorization service war file
    #   shell: mkdir {{ auth_instance_path }}/webapps/authorization-service ; cd {{ auth_instance_path }}/webapps/authorization-service ; jar xf {{ auth_instance_path }}/webapps/{{ authorization_service_file }}
    #   args:
    #     creates: '{{ auth_instance_path }}/webapps/authorization-service/WEB-INF/AuthorizationConfiguration.xml'

    - name: Create the authorization service webapp directory
      file: dest={{ auth_instance_path }}/webapps/authorization-service state=directory
    
    - name: Get and unpack the authorization war file
      unarchive: copy=no src={{ authorization_service_url }} dest={{ auth_instance_path }}/webapps/authorization-service
      args:
        creates: '{{ auth_instance_path }}/webapps/authorization-service/WEB-INF/AuthorizationConfiguration.xml'

    - name: Install the authorization service AuthorizationConfiguration.xml template
      template: src=AuthorizationConfiguration.xml.j2 dest={{ auth_instance_path }}/webapps/authorization-service/{{ authorization_service_config_dest }} mode=0440
      notify: Restart the authorization service

    - name: Install the authorization service persistence.xml template
      template: src=persistence.xml.j2 dest={{ auth_instance_path }}/webapps/authorization-service/{{ authorization_service_persistence_dest }} mode=0440
      notify: Restart the authorization service

    - name: Install the logback configuration
      template: src=logback.xml.j2 dest={{ auth_instance_path }}/lib/logback.xml mode=0644
      notify: Restart the authorization service

  become: True
  become_user: '{{ auth_user }}'
  when: authorization_service_install
  tags: ['authorization_service', 'tomcat' ]