# SPDX-License-Identifier: BSD-3-Clause
---
- name: Install packages required to set up mock wifi network
  package:
    name:
      - hostapd
      - NetworkManager
      - wpa_supplicant
    state: present
    use: "{{ (__network_is_ostree | d(false)) |
      ternary('ansible.posix.rhel_rpm_ostree', omit) }}"
  register: __install_status
  until: __install_status is success
  retries: 6
  delay: 10

- name: Ensure NetworkManager is running
  service:
    name: NetworkManager
    state: started

- name: Copy server certificates
  copy:
    src: "{{ item }}"
    dest: "/etc/pki/tls/{{ item }}"
    mode: "0644"
  with_items:
    - server.key
    - dh.pem
    - server.pem
    - cacert.pem

- name: Create hostapd config
  copy:
    content: |
      interface=wlan1
      driver=nl80211
      ctrl_interface=/var/run/hostapd
      ctrl_interface_group=0
      ssid=mock_wifi
      country_code=EN
      hw_mode=g
      channel=7
      auth_algs=3
      wpa=3
      ieee8021x=1
      eapol_version=1
      wpa_key_mgmt=WPA-EAP WPA-PSK
      wpa_passphrase=p@55w0rD
      eap_reauth_period=3600
      eap_server=1
      use_pae_group_addr=1
      eap_user_file=/etc/hostapd/hostapd.eap_user
      ca_cert=/etc/pki/tls/cacert.pem
      dh_file=/etc/pki/tls/dh.pem
      server_cert=/etc/pki/tls/server.pem
      private_key=/etc/pki/tls/server.key
      private_key_passwd=test
      logger_syslog=-1
      logger_syslog_level=0
    dest: /etc/hostapd/wireless.conf
    mode: "0644"

- name: Create eap_user_file config
  copy:
    content: |
      * TLS
    dest: /etc/hostapd/hostapd.eap_user
    mode: "0644"

- name: Load mac80211_hwsim kernel module to mock a wifi network
  shell: modprobe mac80211_hwsim && sleep 5
  changed_when: false

- name: Restart NetworkManager and wpa_supplicant
  service:
    name: "{{ item }}"
    state: restarted
  with_items:
    - NetworkManager
    - wpa_supplicant

- name: Configure wlan0 and wlan1 (mock wifi interfaces)
  shell: |
    ip link set up wlan0
    ip link set up wlan1
    nmcli device set wlan1 managed off
    ip add add 203.0.113.1/24 dev wlan1
    sleep 5
  changed_when: false

- name: Start hostapd
  shell: hostapd -B /etc/hostapd/wireless.conf && sleep 5
  changed_when: false
