- name: Disable hostapd when not using ap0 and wifi gateway present, or no WiFi hardware present or support not detected
  set_fact:
    hostapd_enabled: False
  when: (not wifi_up_down and discovered_wireless_iface == iiab_wan_iface) or discovered_wireless_iface == "none" or not can_be_ap

- name: Disable the Access Point 'hostapd' service if hostapd_enabled False
  systemd:
    name: hostapd
    enabled: no
  when: not hostapd_enabled

- name: Setting WiFi channel to {{ current_client_channel.stdout }}
  set_fact:
    host_channel: "{{ current_client_channel.stdout }}"
  when: current_client_channel.stdout is defined and current_client_channel.stdout != "" and current_client_channel.stdout|int <= 13

- name: Generate new random mac address for ap0
  shell: tr -dc A-F0-9 < /dev/urandom | head -c 10 | sed -r 's/(..)/\1:/g;s/:$//;s/^/02:/'
  register: ap0_mac
  when: can_be_ap

- name: Setting ap0 mac address for use in hostapd service file
  set_fact:
    ap0_mac_addr: "{{ ap0_mac.stdout }}"
  when: can_be_ap

- name: "Use custom 'hostapd' systemd service unit file using ap0 -- install from template: /etc/systemd/system/hostapd.service, /etc/systemd/system/iiab-clone-wifi.service, /etc/systemd/system/iiab-wifi-test.service, /usr/sbin/iiab-test-wifi"
  template:
    owner: root
    group: root
    src: "{{ item.src }}"
    dest: "{{ item.dest }}"
    mode: "{{ item.mode  }}"
  with_items:
    - { src: 'hostapd/hostapd.service.j2', dest: '/etc/systemd/system/hostapd.service', mode: '0644' }
    - { src: 'hostapd/iiab-clone-wifi.service.j2', dest: '/etc/systemd/system/iiab-clone-wifi.service', mode: '0644' }
    - { src: 'hostapd/iiab-wifi-test.service.j2', dest: '/etc/systemd/system/iiab-wifi-test.service', mode: '0644'}
    - { src: 'hostapd/iiab-test-wifi.j2', dest: '/usr/sbin/iiab-test-wifi', mode: '0755' }
  when: can_be_ap

- name: Use custom 'hostapd' systemd service unit file for {{ discovered_wireless_iface }} when not wifi_up_down
  template:
    src: hostapd/hostapd.legacy.j2
    dest: /etc/systemd/system/hostapd.service
    owner: root
    group: root
    mode: 0644
  when: not wifi_up_down and can_be_ap

# 2022-07-11: Install of iiab-hotspot-on|off moved to network/tasks/main.yml
# as required for Admin Console

- name: Enable the Access Point 'hostapd' service
  systemd:
    name: hostapd
    enabled: yes
  when: hostapd_enabled and not wifi_up_down

- name: Disable ap0 related services
  systemd:
    name: "{{ item }}"
    enabled: no
    daemon_reload: yes
  with_items:
    - iiab-clone-wifi.service
    - iiab-wifi-test.service
  when: not wifi_up_down

- name: Enable the Access Point 'hostapd' and ap0 related services
  systemd:
    name: "{{ item }}"
    enabled: yes
    daemon_reload: yes
  with_items:
    - hostapd.service
    - iiab-clone-wifi.service
    - iiab-wifi-test.service
  when: hostapd_enabled and wifi_up_down

- name: Record HOSTAPD_ENABLED to {{ iiab_env_file }}
  lineinfile:
    path: "{{ iiab_env_file }}"
    regexp: '^HOSTAPD_ENABLED=*'
    line: 'HOSTAPD_ENABLED={{ hostapd_enabled }}'
    state: present

- name: Create /etc/hostapd/hostapd.conf and backup .iiab from template if needed
  template:
    owner: root
    group: root
    mode: 0644
    src: "{{ item.src }}"
    dest: "{{ item.dest }}"
  with_items:
    - { src: 'hostapd/hostapd.conf.j2', dest: '/etc/hostapd/hostapd.conf' }
    - { src: 'hostapd/hostapd.conf.j2', dest: '/etc/hostapd/hostapd.conf.iiab' }
  when: can_be_ap

- name: Record host_country_code_applied and host_channel in network of {{ iiab_ini_file }}
  ini_file:
    dest: "{{ iiab_ini_file }}"    # /etc/iiab/iiab.ini
    section: network
    option: "{{ item.option }}"
    value: "{{ item.value | string }}"
  with_items:
    - option: hostapd_enabled
      value: "{{ hostapd_enabled }}"
    - option: host_ssid
      value: "{{ host_ssid }}"
    - option: host_wifi_mode
      value: "{{ host_wifi_mode }}"
    - option: wifi_up_down
      value: "{{ wifi_up_down }}"
    - option: host_country_code_applied
      value: "{{ host_country_code }}"
    - option: host_channel
      value: "{{ host_channel }}"