---
- hosts:      localhost
  connection: local
  become:     true
  vars_files:
    - vars/default-core.yml

  tasks:
    - name: Checking gsdl3servlet_external.protocol is set to 'https'
      debug: var=gsdl3servlet_external.protocol
      
    - command: echo "gsdl3servlet_external.prototol = {{ gsdl3servlet_external.protocol }}"
      failed_when: gsdl3servlet_external.protocol != "https"
      
    - name: Debian/Ubuntu Install Apache2    
      apt:
        name:
          - apache2
        state:
          present      
      when: (ansible_distribution == 'Debian') or (ansible_distribution == 'Ubuntu')
      
    
    - name: Enable Apache2 Modules  
      shell: /usr/sbin/a2enmod rewrite headers proxy proxy_http proxy_wstunnel 

    - name: Deriving 'gsdl3servlet_internal_domain_url'
      set_fact:
        gsdl3servlet_internal_domain_url: "{{ gsdl3servlet_internal | gsdl3servlet_domain_url() }}"

    - name: Adding 'gsdl3servlet_internal_domain_url' to 'gsdl3servlet_internal'
      set_fact: 
        gsdl3servlet_internal: "{{ gsdl3servlet_internal | combine ({'domain_url': gsdl3servlet_internal_domain_url}) }}"

    - name: Deriving 'gsdl3servlet_external_domain_url'
      set_fact:
        gsdl3servlet_external_domain_url: "{{ gsdl3servlet_external | gsdl3servlet_domain_url() }}"

    - name: Adding 'gsdl3servlet_external_domain_url' to 'gsdl3servlet_external'
      set_fact: 
        gsdl3servlet_external: "{{ gsdl3servlet_external | combine ({'domain_url': gsdl3servlet_external_domain_url}) }}"

    #
    # Apache2 http setup
    #
    - include_tasks: reverse-proxy-http-tasks.yml

    - name: Activating revproxy properties in build.properties
      set_fact:
        build_properties_revproxy:
          - "revproxy.protocol"
          - "revproxy.domain"

    - name: Appending reproxy.opt_port
      set_fact:
        build_properties_revproxy: "{{ build_properties_revproxy + [ 'revproxy.opt_port' ] }}"
      when: gsdl3servlet_external.port is defined

    - name: Appending reproxy.context
      set_fact:
        build_properties_revproxy: "{{ build_properties_revproxy + [ 'revproxy.context' ] }}"
      when: gsdl3servlet_external.context is defined

    - debug:
        var: build_properties_revproxy

    - name: Setting revproxy property values in build.properties if apache2_as_revproxy is True
      ansible.builtin.lineinfile:
        path:   "{{ gsdl3srchome }}/build.properties"
        regexp: '^#*{{ item }}='
        line:   "{{ item }}={{ gsdl3_buildproperties_revproxy[item] }}"
      with_items: "{{ build_properties_revproxy }}"
      when: apache2_as_revproxy == true

    - name: Commenting out revproxy property values in build.properties if apache2_as_revproxy is False
      ansible.builtin.lineinfile:
        path:   "{{ gsdl3srchome }}/build.properties"
        regexp: '^#*{{ item }}='
        line:   "#{{ item }}={{ gsdl3_buildproperties_revproxy[item] }}"
      with_items: "{{ gsdl3_buildproperties_revproxy.keys() | list }}"
      when: apache2_as_revproxy != true


  handlers:
    - name: Reload Apache
      service:
        name: apache2
        state: reloaded

    - name: Restart Apache
      service:
        name: apache2
        state: restarted
