- name: Download & install python-dateutil, sqlite3
  package:
    name: "{{ item }}"
    state: present
  with_items:
    - python-dateutil
    - sqlite3    # @georgehunt hopes to move this to 2-common (or more like stage 3-base-server, alongside MySQL) in October 2018

- name: Create directory /opt/iiab/captive-portal for scripts & templates
  file:
    path: /opt/iiab/captive-portal
    state: directory

- name: 'Copy scripts: checkurls, capture-wsgi.py'
  template:
    src: "{{ item.src }}"
    dest: /opt/iiab/captive-portal/
    mode: "{{ item.mode }}"
  with_items:
    - { src: roles/network/templates/captive-portal/checkurls, mode: '0644' }
    - { src: roles/network/templates/captive-portal/capture-wsgi.py, mode: '0755' }

- name: 'Copy templates: simple.template, mac.template'
  copy:
    src: "{{ item }}"
    dest: /opt/iiab/captive-portal/
  with_items:
    - roles/network/files/simple.template
    - roles/network/files/mac.template

- name: Copy iiab-catch & iiab-uncatch into /usr/bin/
  template:
    src: "{{ item }}"
    dest: /usr/bin/
    owner: root
    group: root
    mode: 0755
  with_items:
    - roles/network/templates/captive-portal/iiab-catch
    - roles/network/templates/captive-portal/iiab-uncatch

- name: Run iiab-uncatch to generate diversion lists for dnsmasq and apache2
  shell: /usr/bin/iiab-uncatch
     
- name: Install systemd unit file py-captive-portal.service from template
  template:
    src: roles/network/templates/captive-portal/py-captive-portal.service.j2
    dest: /etc/systemd/system/py-captive-portal.service
    owner: root
    group: root
    mode: 0644

- name: Install Apache's captive-portal.conf from template if py_captive_portal_enabled
  template:
    src: roles/network/templates/captive-portal/captive-portal.conf
    dest: /etc/{{ apache_config_dir }}/captive-portal.conf
    owner: root
    group: root
    mode: 0644
  when: py_captive_portal_enabled

- name: Enable Apache's captive-portal.conf if py_captive_portal_enabled (debuntu)
  file:
    src: /etc/apache2/sites-available/captive-portal.conf
    path: /etc/apache2/sites-enabled/captive-portal.conf
    state: link
  when: py_captive_portal_enabled and is_debuntu

- name: Enable Apache's default-ssl.conf if py_captive_portal_enabled (debuntu)
  file:
    src: /etc/apache2/sites-available/default-ssl.conf
    path: /etc/apache2/sites-enabled/default-ssl.conf
    state: link
  when: py_captive_portal_enabled and is_debuntu

- name: Enable & Start systemd service py-captive-portal.service if py_captive_portal_enabled
  systemd:
    name: py-captive-portal.service
    daemon-reload: yes
    enabled: yes
    state: started
  when: py_captive_portal_enabled

- name: Disable & Stop py-captive-portal.service if not py_captive_portal_enabled
  systemd:
    name: py-captive-portal.service
    enabled: no
    state: stopped
  when: not py_captive_portal_enabled

- name: Disable Apache's captive-portal.conf if not py_captive_portal_enabled (debuntu)
  file:
    path: /etc/apache2/sites-enabled/captive-portal.conf
    state: absent
  when: not py_captive_portal_enabled and is_debuntu

- name: Disable Apache's default-ssl.conf if not py_captive_portal_enabled (debuntu)
  file:
    path: /etc/apache2/sites-enabled/default-ssl.conf
    state: absent
  when: not py_captive_portal_enabled and is_debuntu

- name: Make sure dnsmasq is not diverting if not py_captive_portal_enabled
  file:
    path: /etc/dnsmasq.d/capture
    state: absent
  when: not py_captive_portal_enabled
