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

- name: Ensure data subvolumes are present
  community.general.btrfs_subvolume:
    name: '/digikam/{{ item }}'
  loop:
    - 'mariadb'

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

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

- name: Ensure compose files is present
  ansible.builtin.copy:
    src: '{{ item }}'
    dest: '/etc/digikam/{{ item }}'
    mode: '0644'
    owner: 'root'
    group: 'root'
  notify: 'restart digikam'
  loop:
    - 'compose.yml'
    - 'init.sql'

- name: Ensure env file is present
  ansible.builtin.template:
    src: 'mariadb.env.j2'
    dest: '/etc/digikam/mariadb.env'
    mode: '0644'
    owner: 'root'
    group: 'root'
  notify: 'restart digikam'

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

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

- name: Ensure digikam service is enabled
  ansible.builtin.service:
    name: 'digikam'
    enabled: true
