# 2022-07-22: Moved from detected_network.yml to netplan.yml AND restart.yml (REMOVE DUPLICATE CODE LATER?!)
# - name: Figure out netplan file name
#   shell: ls /etc/netplan
#   register: netplan
#   #ignore_errors: True # pre 17.10 doesn't use netplan
#   when: is_ubuntu


- name: Restart wpa_supplicant service
  systemd:
    name: "{{ item }}"
    state: restarted
  with_items:
    - wpa_supplicant
  when: wifi_up_down and hostapd_enabled and not network_manager_active

- name: Enable & Restart networkd-dispatcher.service
  systemd:
    name: networkd-dispatcher
    state: restarted
    enabled: yes
    masked: no
  when: systemd_networkd_active

- name: Clone wifi if needed
  systemd:
    name: iiab-clone-wifi
    state: restarted
  when: wifi_up_down and can_be_ap and ansible_ap0 is undefined

- name: Waiting {{ hostapd_wait }} seconds for network to stabilize for ap0
  shell: sleep {{ hostapd_wait }}
  when: ansible_ap0 is undefined

- name: Restart hostapd when WiFi is present but not when using WiFi as gateway
  systemd:
    name: hostapd
    state: restarted
    daemon_reload: yes
  when: hostapd_enabled and not no_net_restart

# 2022-07-22: @jvonau suggests commenting this out as: "we really don't touch
# any of the config files... netplan.yml renames one file if it's a container
# build like on MATE, could possibly skip netplan.yml in future or toss that in
# the mix now and see what shakes up" [ok, but keep netplan.yml as is for now]
# - name: Reload netplan for Wifi gateway on Ubuntu 18+
#   shell: netplan apply
#   when: wifi_up_down and is_ubuntu and netplan.stdout.find("yaml") != -1

#- name: Start named service
#  systemd:
#    name: "{{ dns_service }}"
#    state: restarted
#  when: named_enabled and named_install

- name: Stop Squid service
  systemd:
    name: "{{ proxy }}"
    state: stopped
  async: 120
  when: squid_install or squid_installed is defined

# - name: Stop DansGuardian
#   systemd:
#     name: dansguardian
#     state: stopped
#   when: dansguardian_install or dansguardian_installed is defined

# - name: Restart DansGuardian service (dansguardian) except Ubuntu which needs reboot to activate
#   systemd:
#     name: dansguardian
#     state: restarted
#   when: dansguardian_enabled and dansguardian_install and ( not is_ubuntu and iiab_stage|int < 4 )

# Squid get re-loaded with dispatcher.d
- name: Restart Squid service ({{ proxy }})
  systemd:
    name: "{{ proxy }}"
    state: restarted
  when: squid_enabled and squid_install

# - name: Restart Wondershaper service (wondershaper)
#   systemd:
#     name: wondershaper
#     state: restarted
#   when: wondershaper_enabled

- name: Restart Avahi service (avahi-daemon)
  systemd:
    name: avahi-daemon
    state: restarted

#netplan de-configures pre-created bridged interfaces 
#- name: Reload netplan when Wifi is not gateway on Ubuntu 18+
#  shell: netplan apply
#  when: not no_net_restart and is_ubuntu and netplan.stdout.find("yaml") != -1

- name: Waiting {{ hostapd_wait }} seconds for network to stabilize (dnsmasq will fail if br0 isn't in a 'up' state!)
  shell: sleep {{ hostapd_wait }}

- name: Run /usr/bin/iiab-gen-iptables
  command: /usr/bin/iiab-gen-iptables

- name: Checking if WiFi slave is active
  shell: bridge -d link | grep {{ iiab_wireless_lan_iface }} | wc -l
#  when: hostapd_enabled and discovered_wireless_iface != iiab_wan_iface and iiab_lan_iface == "br0"
  when: hostapd_enabled and iiab_lan_iface == "br0"
  register: wifi_slave

- name: Restart hostapd if WiFi slave is inactive
  systemd:
    name: hostapd
    state: restarted
  when: hostapd_enabled and not no_net_restart and wifi_slave.stdout is defined and wifi_slave.stdout == 0

#both interfaces.d and systemd-networkd should have br0 available and Appliance lacks br0
#keep an eye on legacy wifi installs where br0 is present but not 'online' with an ip address
#due to hostapd didn't go to a carrier state. All others should get dnsmasq restarted
#- name: User choice of dnsmasq or dhcpd - restarting {{ dhcp_service2 }}
- name: Restarting dnsmasq
  systemd:
    name: dnsmasq
    state: restarted
  when: dnsmasq_enabled and ((not no_net_restart or (is_ubuntu and wifi_up_down)) or (iiab_stage|int == 9))
#  when: (not no_net_restart or (is_ubuntu and wifi_up_down)) or (iiab_stage|int == 9)
  #when: (not no_net_restart or (is_ubuntu_20 and wifi_up_down)) or (iiab_stage|int == 9)
  #when: (not no_net_restart or (is_ubuntu_20 and wifi_up_down))
  #when: (iiab_network_mode != "Appliance")    # Sufficient b/c br0 exists thanks to /etc/network/interfaces.d/iiab
  #when: iiab_network_mode != "Appliance" and iiab_wan_iface != discovered_wireless_iface