- name: Install packages
  package:
    name:
      - wireguard-tools
    state: present

- name: Configure wireguard
  copy:
    src: "{{ item }}"
    dest: /usr/local/etc/wireguard/
    mode: 0440
    owner: root
    group: wheel
  with_fileglob: "files/wireguard_configs/{{ wireguard_directory }}/*.conf"

- name: Enable gateway
  when: wireguard_gateway is defined and wireguard_gateway == true
  community.general.sysrc:
    name: "{{ item }}"
    value: "YES"
    path: /etc/rc.conf.d/routing
  loop:
    - gateway_enable
    - ipv6_gateway_enable

- name: Enable wireguard service
  when: enabled_wireguard is defined
  community.general.sysrc:
    name: wireguard_enable
    value: "YES"
    path: /etc/rc.conf.d/wireguard

- name: Disable wireguard service
  when: enabled_wireguard is not defined
  community.general.sysrc:
    name: wireguard_enable
    value: "NO"
    path: /etc/rc.conf.d/wireguard

- name: Set enabled wireguard list
  when: enabled_wireguard is defined
  community.general.sysrc:
    name: wireguard_interfaces
    value: "{{ enabled_wireguard|join(' ') }}"
    path: /etc/rc.conf.d/wireguard
