---

- name: Look for legacy apt keyring
  ansible.builtin.stat:
    path: /etc/apt/trusted.gpg
  register: _trusted_gpg_keyring
  tags:
    - apt

- name: Evolix embedded GPG key is absent
  ansible.builtin.apt_key:
    id: "B8612B5D"
    keyring: /etc/apt/trusted.gpg
    state: absent
  tags:
    - apt
  when: _trusted_gpg_keyring.stat.exists

- name: "Ensure {{ apt_keyring_dir }} directory exists"
  file:
    path: "{{ apt_keyring_dir }}"
    state: directory
    mode: "755"
    owner: root
    group: root

- name: Set Evolix GPG key format to GPG (Debian < 9)
  set_fact:
    apt_evolix_public_key: "pub_evolix.gpg"
  when:
    - ansible_distribution_major_version is version('9', '<')
  tags:
    - apt

- name: Set Evolix GPG key format to ASC (Debian >= 9)
  set_fact:
    apt_evolix_public_key: "pub_evolix.asc"
  when:
    - ansible_distribution_major_version is version('9', '>=')
  tags:
    - apt

- name: Add Evolix GPG key
  ansible.builtin.copy:
    src: "{{ apt_evolix_public_key }}"
    dest: "{{ apt_keyring_dir }}/{{ apt_evolix_public_key }}"
    force: true
    mode: "0644"
    owner: root
    group: root
  tags:
    - apt

- name: Evolix public list is installed
  ansible.builtin.template:
    src: evolix_public.list.j2
    dest: /etc/apt/sources.list.d/evolix_public.list
    force: true
    mode: "0640"
  register: apt_evolix_public
  tags:
    - apt

- name: Apt update
  ansible.builtin.apt:
    update_cache: yes
  tags:
    - apt
  when: apt_evolix_public is changed
