---
- name: Install restic and snooze
  package:
    name:
      - restic
      - snooze
    state: present
  notify:
    - Restart restic

- name: Create restic service directories
  file:
    path: '/etc/s6-rc/service/{{ item }}'
    state: directory
    owner: root
    group: wheel
    mode: 0755
  with_items: '{{ restic_service_dirs }}'
  notify:
    - Reload s6-rc
    - Restart restic
    - Restart restic-log

- name: Generate restic service scripts
  template:
    dest: '/etc/s6-rc/service/{{ item }}'
    src: '{{ item }}.j2'
    mode: 0555
    owner: root
    group: wheel
  with_items: '{{ restic_service_scripts }}'
  notify:
    - Reload s6-rc
    - Restart restic
    - Restart restic-log

- name: Generate restic service configuration
  copy:
    dest: '/etc/s6-rc/service/{{ item.name }}'
    content: '{{ item.content }}'
    mode: 0444
    owner: root
    group: wheel
  loop_control:
    label: '{{ item.name }} = {{ item.content }}'
  with_items: '{{ restic_service_config }}'
  notify:
    - Reload s6-rc
    - Restart restic
    - Restart restic-log

- name: Flush handlers
  meta: flush_handlers

- name: Start restic renew service
  command: fdmove -c 2 1 s6-rc -l {{ s6_live_dir }} -u -v 2 change restic
  register: change
  changed_when: change.stdout | length > 0

- name: Enable restic
  lineinfile:
    path: /etc/s6-rc/service/enabled/contents
    regexp: "^restic$"
    line: "restic"
  notify:
    - Reload s6-rc

- name: Flush handlers (again)
  meta: flush_handlers