---

- name: Install/Update system tools
  ansible.builtin.apt:
    name:
      - locales
      - sudo
      - ntpdate
      - lsb-release
      - dnsutils
      - pv
      - apg
      - conntrack
      - logrotate
      - bash-completion
      - ssl-cert
      - ca-certificates
      - rename
      - dmidecode
  when: evolinux_packages_system | bool

- name: Install/Update diagnostic tools
  ansible.builtin.apt:
    name:
      - strace
      - htop
      - iftop
      - iptraf
      - ncdu
      - iotop
      - tcpdump
      - mtr-tiny
      - curl
      - wget
      - telnet
      - traceroute
      - man
      - molly-guard
  when: evolinux_packages_diagnostic | bool

- name: Install/Update hardware tools
  ansible.builtin.apt:
    name:
      - hdparm
      - smartmontools
      - lm-sensors
  when: ansible_virtualization_role == "host"

- name: Install/Update common tools
  ansible.builtin.apt:
    name:
      - vim
      - screen
      - tmux
      - mutt
      - tree
      - git
      - rsync
      - bc
      - pinentry-curses
      - ncurses-term
  when: evolinux_packages_common | bool

- name: Be sure that openntpd package is absent/purged
  ansible.builtin.apt:
    name: openntpd
    state: absent
    purge: True
  when: evolinux_packages_purge_openntpd | bool

- name: the chrony package is absent
  ansible.builtin.apt:
    name: chrony
    purge: True
    state: absent
  when: evolinux_packages_purge_chrony | bool

- name: Be sure locate/mlocate is absent/purged
  ansible.builtin.apt:
    name:
      - locate
      - mlocate
    state: absent
    purge: yes
  when: evolinux_packages_purge_locate | bool

- name: Install/Update serveur-base meta-package
  ansible.builtin.apt:
    name: serveur-base
    allow_unauthenticated: yes
  when: evolinux_packages_serveur_base | bool

- name: Install/Update packages for Stretch and later
  ansible.builtin.apt:
    name: net-tools
  when:
    - evolinux_packages_stretch | bool
    - ansible_distribution_major_version is version('9', '>=')

- name: Install/Update packages for Buster and later
  ansible.builtin.apt:
    name:
      - spectre-meltdown-checker
      - binutils
  when:
    - evolinux_packages_buster | bool
    - ansible_distribution_major_version is version('10', '>=')

- name: Customize logcheck recipient
  ansible.builtin.lineinfile:
    dest: /etc/logcheck/logcheck.conf
    regexp: '^SENDMAILTO=".*"$'
    line: 'SENDMAILTO="{{ logcheck_alert_email or general_alert_email | mandatory }}"'
  when: evolinux_packages_logcheck_recipient | bool

- name: Deleting rpcbind and nfs-common
  ansible.builtin.apt:
    name:
      - rpcbind
      - nfs-common
    state: absent
  when: evolinux_packages_delete_nfs | bool


# TODO: use ini_file when Ansible > 2.1 (no_extra_spaces: yes)

- name: Configure Listchanges on Jessie
  ansible.builtin.lineinfile:
    dest: /etc/apt/listchanges.conf
    regexp: '^{{ item.option }}\s*='
    line: "{{ item.option }}={{ item.value }}"
  loop:
    - { option: "confirm", value: "1" }
    - { option: "which",   value: "both" }
  when:
    - evolinux_packages_listchanges | bool
    - ansible_distribution == "Debian"
    - ansible_distribution_release == "jessie"

- name: apt-listchanges is absent on Stretch and later
  ansible.builtin.apt:
    name: apt-listchanges
    state: absent
  when:
    - ansible_distribution == "Debian"
    - ansible_distribution_major_version is version('9', '>=')
    - evolinux_packages_delete_aptlistchanges

- ansible.builtin.meta: flush_handlers
