---

- name: Git is installed (Debian)
  apt:
    name: git
    state: present
  tags:
    - etc-git

- include: repository.yml
  vars:
    repository_path: "/etc"
    gitignore_items:
      - "aliases.db"
      - "*.swp"
      - "postfix/sa-blacklist.access"
      - "postfix/*.db"
      - "postfix/spamd.cidr"
      - "evobackup/.keep-*"
      - "letsencrypt/.certbot.lock"

- name: verify /usr/share/scripts presence
  stat:
    path: /usr/share/scripts
  register: _usr_share_scripts

- include: repository.yml
  vars:
    repository_path: "/usr/share/scripts"
    gitignore_items: []
  when:
    - _usr_share_scripts.stat.isdir
    - ansible_distribution_major_version is version('10', '>=')

- name: Check if cron is installed
  shell: "set -o pipefail && dpkg -l cron 2> /dev/null | grep -q -E '^(i|h)i'"
  args:
    executable: /bin/bash
  failed_when: False
  changed_when: False
  check_mode: no
  register: is_cron_installed

- name: Optimize script is installed in monthly crontab
  copy:
    src: optimize-etc-git
    dest: /etc/cron.monthly/optimize-etc-git
    mode: "0750"
    force: no
  when: is_cron_installed.rc == 0
  tags:
    - etc-git

- name: Cron job for /etc/.git status is installed
  template:
    src: etc-git-status.j2
    dest: /etc/cron.d/etc-git-status
    mode: "0644"
  when: is_cron_installed.rc == 0 and etc_git_monitor_status
  tags:
    - etc-git

- name: Cron job for /etc/.git status is removed
  file:
    dest: /etc/cron.d/etc-git-status
    state: absent
  when: is_cron_installed.rc == 0 and not etc_git_monitor_status
  tags:
    - etc-git
