---
- name: Ensure download directory is present
  ansible.builtin.file:
    path: "/home/debian/restic-rest-{{ restic_rest_version }}"
    state: directory
    mode: '0755'
  
- name: Ensure binary is downloaded
  ansible.builtin.unarchive:
    src: "https://github.com/restic/rest-server/releases/download/v{{ restic_rest_version }}/rest-server_{{ restic_rest_version }}_linux_arm64.tar.gz"
    remote_src: true
    dest: "/home/debian/restic-rest-{{ restic_rest_version }}"
    creates: "/home/debian/restic-rest-{{ restic_rest_version }}/rest-server_{{ restic_rest_version }}_linux_arm64/rest-server"
    include:
      - "rest-server_{{ restic_rest_version }}_linux_arm64/rest-server"
  register: res

- name: Ensure binary is copied to correct location
  ansible.builtin.copy:
    src: "/home/debian/restic-rest-{{ restic_rest_version }}/rest-server_{{ restic_rest_version }}_linux_arm64/rest-server"
    remote_src: true
    dest: '/usr/local/bin/restic-rest-server'
    owner: 'root'
    group: 'root'
    mode: '0755'
  when: 'res.changed'

- name: Ensure system group exists
  ansible.builtin.group:
    name: 'restic'
    gid: 202
    system: true
    state: present

- name: Ensure system user exists
  ansible.builtin.user:
    name: 'restic'
    group: 'restic'
    uid: 202
    system: true
    create_home: false

- name: Ensure data subvolume is present
  community.general.btrfs_subvolume:
    name: '/restic-rest'

- name: Ensure data subvolume permissions are correct
  ansible.builtin.file:
    path: '/mnt/data1/restic-rest'
    state: directory
    mode: '0755'
    owner: 'restic'
    group: 'restic'

- name: Ensure service file is present
  ansible.builtin.copy:
    src: 'restic-rest-server.service'
    dest: '/lib/systemd/system/restic-rest-server.service'
    owner: 'root'
    group: 'root'
    mode: '0644'
  register: res

- name: systemd-reload
  ansible.builtin.systemd_service:
    daemon_reload: true
  when: 'res.changed'

- name: Ensure service is enabled
  ansible.builtin.service:
    name: 'restic-rest-server'
    state: started
    enabled: true
