---
# Install the nginx virtualhosts
# If bot smartgears_install_generic_virthost and smartgears_install_common_nginx_virthualhost are True, we prefer the latter
- block:
  - name: Install the nginx generic virtualhost
    template: src=nginx-smartgears-virtualhost.j2 dest=/etc/nginx/sites-available/{{ item.virthost_name }} owner=root group=root mode=0444
    with_items: '{{ smartgears_nginx_virtualhosts }}'
    notify: Reload nginx

  - name: Enable the smartgears nginx virtualhosts
    file: src=/etc/nginx/sites-available/{{ item.virthost_name }} dest=/etc/nginx/sites-enabled/smartgears_{{ item.smartgears_id }}.conf state=link
    with_items: '{{ smartgears_nginx_virtualhosts }}'
    when: item.smartgears_id is defined
    notify: Reload nginx

  - name: Enable the non smartgears nginx virtualhosts
    file: src=/etc/nginx/sites-available/{{ item.virthost_name }} dest=/etc/nginx/sites-enabled/smartgears_{{ item.virthost_name }}.conf state=link
    with_items: '{{ smartgears_nginx_virtualhosts }}'
    when: item.smartgears_id is not defined
    notify: Reload nginx

  - name: Give the smartgears user access to the document root, and create it if needed
    file: dest={{ item.root}} state=directory owner={{ item.user }} group={{ item.user }} mode=0755
    with_items: '{{ smartgears_nginx_virtualhosts }}'
    when:
      - smartgears_nginx_rw_html_root
      - item.user is defined
    notify: Reload nginx
    tags: [ 'nginx', 'virtualhost', 'web_root' ]

  tags: [ 'nginx', 'virtualhost' ]
  when: smartgears_install_common_nginx_virthualhost

- block:
  - name: Install the nginx generic virtualhost (old, to be replaced by a newer template)
    template: src=generic-smartgears-virtualhost.j2 dest=/etc/nginx/sites-available/{{ item.servername }} owner=root group=root mode=0444
    with_items: '{{ tomcat_m_instances }}'
    when: smartgears_install_generic_virthost
    notify: Reload nginx
    tags: [ 'nginx', 'virtualhost' ]

  - name: Install the nginx virtualhost, if a specific one has been defined
    template: src={{ item.servername }}-virtualhost.j2 dest=/etc/nginx/sites-available/{{ item.servername }} owner=root group=root mode=0444
    with_items: '{{ tomcat_m_instances }}'
    when: not smartgears_install_generic_virthost
    notify: Reload nginx
    tags: [ 'nginx', 'virtualhost' ]

  - name: Enable the nginx virtualhosts (old style)
    file: src=/etc/nginx/sites-available/{{ item.servername }} dest=/etc/nginx/sites-enabled/smartgears_{{ item.smartgears_id }}.conf state=link
    with_items: '{{ tomcat_m_instances }}'
    notify: Reload nginx
    tags: [ 'nginx', 'virtualhost' ]

  - name: Give the smartgears user access to the document root, and create it if needed (old style)
    file: dest={{ item.web_document_root}} state=directory owner={{ item.user }} group={{ item.user }} mode=0755
    with_items: '{{ tomcat_m_instances }}'
    when: smartgears_nginx_rw_html_root
    notify: Reload nginx
    tags: [ 'nginx', 'virtualhost', 'web_root' ]

  when: not smartgears_install_common_nginx_virthualhost
  tags: [ 'nginx', 'virtualhost' ]


- block:
  - name: Remove the old hostname based virtualhost filename
    file: dest=/etc/nginx/sites-enabled/{{ item.servername }}.conf state=absent
    with_items: '{{ tomcat_m_instances }}'
    notify: Reload nginx

  - name: Remove the old ubuntu.conf virtualhost filename
    file: dest=/etc/nginx/sites-enabled/ubuntu.conf state=absent
    with_items: '{{ tomcat_m_instances }}'
    notify: Reload nginx

  tags: [ 'nginx', 'virtualhost' ]