---
- name: Ensure data directory is present
  ansible.builtin.file:
    path: '/mnt/data1/matrix'
    state: directory
    mode: '0755'
    owner: 'root'
    group: 'root'

- name: Ensure data subvolumes are present
  community.general.btrfs_subvolume:
    name: '/matrix/{{ item }}'
  with_items:
    - 'conduit'

- name: Ensure subvolume permissions are correct
  ansible.builtin.file:
    path: "/mnt/data1/matrix/{{ item.dir }}"
    state: directory
    mode: '0755'
    owner: "{{ item.owner }}"
    group: "{{ item.group }}"
  loop:
    - dir: 'conduit'
      owner: 1000
      group: 1000

- name: Ensure configuration directory is present
  ansible.builtin.file:
    path: '/etc/matrix'
    state: directory
    mode: '0755'

- name: Ensure Conduit config file is present
  ansible.builtin.copy:
    src: 'conduit.toml'
    dest: '/etc/matrix/conduit.toml'
    mode: '0644'
    owner: 'root'
    group: 'root'
  notify: conduit-restart

- name: Ensure compose file is present
  ansible.builtin.copy:
    src: 'compose.yml'
    dest: '/etc/matrix/compose.yml'
    mode: '0644'
    owner: 'root'
    group: 'root'
  register: res

- name: Ensure stack is deployed
  ansible.builtin.shell:
    chdir: '/etc/matrix'
    cmd: 'docker compose up -d --remove-orphans'
  when: 'res.changed'

- name: Ensure backup script is present
  ansible.builtin.copy:
    src: 'matrix.backup.sh'
    dest: '/etc/backups/matrix.backup.sh'
    owner: 'root'
    group: 'root'
    mode: '0644'
