- name: Disable dhcpd service
  service:
    name: dhcpd
    enabled: no
  when: not dhcpd_enabled and dhcpd_install

# service is restarted with NM dispatcher.d script
- name: Enable dhcpd service
  service:
    name: dhcpd
    enabled: yes
  when: dhcpd_enabled and dhcpd_install

- name: Install /etc/sysconfig/dhcpd, /etc/dhcpd-iiab.conf from templates
  template:
    src: "{{ item.src }}"
    dest: "{{ item.dest }}"
    owner: root
    group: root
    mode: "{{ item.mode }}"
  with_items:
   - { src: 'dhcp/dhcpd-env.j2', dest: '/etc/sysconfig/dhcpd', mode: '0644' }
   - { src: 'dhcp/dhcpd-iiab.conf.j2', dest: '/etc/dhcpd-iiab.conf', mode: '0644' }
  when: dhcpd_enabled and dhcpd_install

- name: Install /etc/named-iiab.conf and two *.zone.db files into /var/named-iiab
  template:
    src: "{{ item.src }}"
    dest: "{{ item.dest }}"
    owner: root
    group: root
    mode: "{{ item.mode }}"
  with_items:
   - { src: 'named/named-iiab.conf.j2', dest: '/etc/named-iiab.conf', mode: '0644' }
   - { src: 'named/school.local.zone.db', dest: '/var/named-iiab/', mode: '0644' }
   - { src: 'named/school.internal.zone.db', dest: '/var/named-iiab/', mode: '0644' }
  when: named_enabled and named_install

- name: Enable named service ({{ dns_service }}) if named_enabled
  systemd:
    name: "{{ dns_service }}"
    enabled: yes
  when: named_enabled and named_install

- name: Disable named service ({{ dns_service }}) if not named_enabled
  systemd:
    name: "{{ dns_service }}"
    enabled: no
  when: not named_enabled and named_install

- name: Install /etc/dnsmasq.d/iiab.conf from template, when dnsmasq_enabled and isn't Appliance
  template:
    src: network/dnsmasq.conf.j2
    dest: /etc/dnsmasq.d/iiab.conf
  when: dnsmasq_enabled and dnsmasq_install and (iiab_network_mode != "Appliance")

- name: Remove /etc/dnsmasq.d/iiab.conf, when not dnsmasq_enabled or is Appliance
  file:
    path: /etc/dnsmasq.d/iiab.conf
    state: absent
  when: (not dnsmasq_enabled) or (iiab_network_mode == "Appliance")

- name: Enable iiab-dnsmasq systemd service, if dnsmasq_enabled
  systemd:
    name: iiab-dnsmasq
    enabled: yes
  when: dnsmasq_enabled and dnsmasq_install

- name: Disable iiab-dnsmasq, if not dnsmasq_enabled
  systemd:
    name: iiab-dnsmasq
    enabled: no
  when: not dnsmasq_enabled and dnsmasq_install

- name: Enable DansGuardian systemd service, if dansguardian_enabled
  systemd:
    name: dansguardian
    enabled: yes
  when: dansguardian_enabled and dansguardian_install

- name: Disable DansGuardian, if not dansguardian_enabled
  systemd:
    name: dansguardian
    enabled: no
  when: not dansguardian_enabled and dansguardian_install

- name: Mandate 'HTTPCACHE_ON=True' in {{ iiab_env_file }}, if squid_enabled
  lineinfile:
    path: "{{ iiab_env_file }}"
    regexp: '^HTTPCACHE_ON=*'
    line: 'HTTPCACHE_ON=True'
    state: present
  when: squid_enabled and squid_install

- name: Enable Squid systemd service ({{ proxy }}) if squid_enabled
  systemd:
    name: "{{ proxy }}"
    enabled: yes
  when: squid_enabled and squid_install

- name: Install /etc/{{ proxy }}/squid-iiab.conf from template, owned by {{ proxy_user }}:{{ proxy_user }}
  template:
    src: "{{ item.src }}"
    dest: "{{ item.dest }}"
    owner: "{{ item.owner }}"
    group: "{{ item.group }}"
    mode: "{{ item.mode }}"
  with_items:
    - src: squid/squid-iiab.conf.j2
      dest: "/etc/{{ proxy }}/squid-iiab.conf"
      owner: "{{ proxy_user }}"
      group: "{{ proxy_user }}"
      mode: 0644
  when: squid_enabled and squid_install

- name: Point to Squid config file from startup file, if squid_enabled (debuntu)
  lineinfile:
    regexp: '^CONFIG'
    line: "CONFIG=/etc/{{ proxy }}/squid-iiab.conf"
    path: "/etc/init.d/{{ proxy }}"
  when: squid_enabled and squid_install and is_debuntu

- name: Disable Squid service, if not squid_enabled
  systemd:
    name: "{{ proxy }}"
    enabled: no
  when: not squid_enabled and squid_install

- name: Revert to 'HTTPCACHE_ON=False' if not squid_enabled
  lineinfile:
    dest: "{{ iiab_env_file }}"
    regexp: '^HTTPCACHE_ON=*'
    line: 'HTTPCACHE_ON=False'
    state: present
  when: not squid_enabled

- name: Enable Wondershaper service, if wondershaper_enabled
  systemd:
    name: wondershaper
    enabled: yes
  when: wondershaper_enabled and wondershaper_install

- name: Disable Wondershaper service, if not wondershaper_enabled
  systemd:
    name: wondershaper
    enabled: no
  when: not wondershaper_enabled and wondershaper_install

# check-LAN should be iptables.yml remove later
- name: Install clean copy of /usr/bin/iiab-gen-iptables from template
  template:
    src: gateway/iiab-gen-iptables
    dest: /usr/bin/iiab-gen-iptables
    owner: root
    group: root
    mode: 0755

- name: Add 'squid' variable values to {{ iiab_ini_file }}
  ini_file:
    path: "{{ iiab_ini_file }}"
    section: squid
    option: "{{ item.option }}"
    value: "{{ item.value }}"
  with_items:
    - option: enabled
      value: "{{ squid_enabled }}"

- name: Add 'dansguardian' variable values to {{ iiab_ini_file }}
  ini_file:
    path: "{{ iiab_ini_file }}"
    section: dansguardian
    option: "{{ item.option }}"
    value: "{{ item.value }}"
  with_items:
    - option: enabled
      value: "{{ dansguardian_enabled }}"

- name: Add 'wondershaper' variable values to {{ iiab_ini_file }}
  ini_file:
    path: "{{ iiab_ini_file }}"
    section: wondershaper
    option: "{{ item.option }}"
    value: "{{ item.value }}"
  with_items:
    - option: enabled
      value: "{{ wondershaper_enabled }}"
