---

- name: "Compatibility check"
  ansible.builtin.assert:
    that:
      - ansible_distribution == "Debian"
      - ansible_distribution_major_version is version('8', '>=')
    msg: Only compatible with Debian >= 8
  tags:
    - apt

- name: "apt-transport-https is installed for https repositories (before Buster)"
  ansible.builtin.apt:
    name:
      - apt-transport-https
  tags:
    - apt
  when: ansible_distribution_major_version is version('10', '<')

- name: "certificates are installed for https repositories"
  ansible.builtin.apt:
    name:
      - ca-certificates
  tags:
    - apt

- name: Custom configuration
  ansible.builtin.import_tasks: config.yml
  when: apt_config | bool
  tags:
    - apt

- name: Install basics repositories (Debian <12)
  ansible.builtin.import_tasks: basics.oneline.yml
  tags:
    - apt
  when:
    - apt_install_basics | bool
    - ansible_distribution_major_version is version('12', '<')

- name: Install basics repositories (Debian >=12)
  ansible.builtin.import_tasks: basics.deb822.yml
  tags:
    - apt
  when:
    - apt_install_basics | bool
    - ansible_distribution_major_version is version('12', '>=')

- name: Install backports repositories (Debian <12)
  ansible.builtin.import_tasks: backports.oneline.yml
  tags:
    - apt
  when:
    - apt_install_backports | bool
    - ansible_distribution_major_version is version('12', '<')

# With Debian 12+ and the deb822 format of source files
# backports are always installed but enabled according to `apt_install_backports`
- name: Install backports repositories (Debian >=12)
  ansible.builtin.import_tasks: backports.deb822.yml
  tags:
    - apt
  when:
    - ansible_distribution_major_version is version('12', '>=')


- name: Install Evolix Public repositories (Debian <12)
  ansible.builtin.import_tasks: evolix_public.oneline.yml
  tags:
    - apt
  when:
    - apt_install_evolix_public | bool
    - ansible_distribution_major_version is version('12', '<')

- name: Install Evolix Public repositories (Debian >=12)
  ansible.builtin.import_tasks: evolix_public.deb822.yml
  tags:
    - apt
  when:
    - apt_install_evolix_public | bool
    - ansible_distribution_major_version is version('12', '>=')

- name: Install Extended-LTS repositories (Debian < 10)
  ansible.builtin.import_tasks: extended-lts.oneline.yml
  tags:
    - apt
  when:
    - apt_install_extended_lts | bool
    - ansible_distribution_major_version is version('10', '<')

- name: Clean GANDI sources
  ansible.builtin.file:
    path: '{{ item }}'
    state: absent
  loop:
    - /etc/apt/sources.list.d/debian-security.list
    - /etc/apt/sources.list.d/debian-jessie.list
    - /etc/apt/sources.list.d/debian-stretch.list
    - /etc/apt/sources.list.d/debian-buster.list
    - /etc/apt/sources.list.d/debian-bullseye.list
    - /etc/apt/sources.list.d/debian-update.list
  tags:
    - apt
  when: apt_clean_gandi_sourceslist | bool


- name: "Disable NonFreeFirmware warning for VM on Debian 12+"
  ansible.builtin.lineinfile:
    path: /etc/apt/apt.conf.d/no-bookworm-firmware.conf
    create: yes
    line: "APT::Get::Update::SourceListWarnings::NonFreeFirmware \"false\";"
  tags:
    - apt
  when:
    - ansible_distribution_major_version is version('12', '>=')
    - ansible_virtualization_role == "guest"


- name: Install check for packages marked hold
  ansible.builtin.import_tasks: hold_packages.yml
  when: apt_install_hold_packages | bool
  tags:
    - apt

- name: Updating APT cache
  ansible.builtin.apt:
    update_cache: yes
  changed_when: False
  tags:
    - apt

- name: Upgrading system
  ansible.builtin.apt:
    upgrade: dist
  when: apt_upgrade | bool
  tags:
    - apt
