- name: Install restic
  community.general.xbps:
    name:
      - restic
    state: "{{ desired_package_state | default('present') }}"
  tags: packages

- name: Ensure restic configuration directory exists
  ansible.builtin.file:
    dest: "/etc/restic/root"
    state: directory

- name: Create restic root backup configuration
  ansible.builtin.template:
    src: restic.conf.j2
    dest: "/etc/restic/root/restic.conf"
    mode: 0600
    force: true # ensure contents are always exact

- name: Ensure files and exclude files exist # Only change if necessary
  ansible.builtin.file:
    dest: "/etc/restic/root/{{ item }}"
    state: touch
    modification_time: preserve
    access_time: preserve
  loop:
    - include
    - exclude

- name: Create include files # TODO: Rename file to include?
  ansible.builtin.copy:
    content: "/"
    dest: "/etc/restic/root/include"
    force: true # ensure contents are always exact

- name: Install snapstic script
  ansible.builtin.copy:
    src: snapstic
    dest: "/usr/bin/snapstic"
    mode: 0744

- name: Ensure restic configuration directory exists
  ansible.builtin.file:
    dest: "/etc/cron.weekly"
    state: directory

- name: Add snapstic to weekly cronjobs
  ansible.builtin.copy:
    content: "#!/bin/sh\n\nsnapstic backup"
    dest: "/etc/cron.weekly/snapstic"
    mode: 0755
    force: true # ensure contents are always exact