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

- name: Ensure data subvolumes are present
  community.general.btrfs_subvolume:
    name: '/atuin-server/{{ item }}'
  loop:
    - 'postgres'

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

- name: Ensure compose file is present
  ansible.builtin.copy:
    src: 'compose.yml'
    dest: '/etc/atuin/compose.yml'
    mode: '0644'
    owner: 'root'
    group: 'root'
  notify: 'restart atuin-server'

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

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

- name: Ensure service file is present
  ansible.builtin.copy:
    src: 'atuin-server.service'
    dest: '/lib/systemd/system/atuin-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 atuin-server service is enabled
  ansible.builtin.service:
    name: 'atuin-server'
    enabled: true
