---
- name: install SpamAssasin
  apt:
    name:
      - spamassassin
      - evomaintenance
    state: present
  tags:
    - spamassassin

- name: configure SpamAssasin
  copy:
    src: spamassassin.cf
    dest: /etc/spamassassin/local_evolix.cf
    mode: "0644"
  notify: restart spamassassin
  tags:
    - spamassassin

- name: enable SpamAssasin
  replace:
    dest: /etc/default/spamassassin
    regexp: 'ENABLED=0'
    replace: 'ENABLED=1'
  notify: restart spamassassin
  tags:
    - spamassassin

- name: add amavis user to debian-spamd group
  user:
    name: amavis
    groups: debian-spamd
    append: yes
  tags:
    - spamassassin

- name: fix right on /var/lib/spamassassin
  file:
    dest: /var/lib/spamassassin
    state: directory
    mode: "0750"
  tags:
    - spamassassin

- include_role:
    name: evolix/remount-usr
  tags:
    - spamassassin

- name: copy sa-update.sh script
  copy:
    src: sa-update.sh
    dest: /usr/share/scripts/sa-update.sh
    mode: "0750"
  tags:
    - spamassassin

- name: Check if cron is installed
  shell: "dpkg -l cron 2> /dev/null | grep -q -E '^(i|h)i'"
  failed_when: False
  changed_when: False
  register: is_cron_installed
  
- name: enable sa-update.sh cron
  lineinfile:
    dest: /etc/cron.d/sa-update
    line: "42 6 5 1,4,7,10 * root /usr/share/scripts/sa-update.sh"
    create: yes
    state: present
    mode: "0640"
  when: is_cron_installed.rc == 0
  tags:
    - spamassassin

- name: update SpamAssasin's rules
  command: "/usr/share/scripts/sa-update.sh"
  changed_when: false
  tags:
    - spamassassin

- name: ensure SpamAssasin is started and enabled
  systemd:
    name: spamassassin
    state: started
    enabled: True
  tags:
    - spamassassin
