---
- name: Create Nextcloud data directory
  ansible.builtin.file:
    path: "{{ nextcloud_data_dir }}"
    state: directory
    owner: root
    group: root
    mode: "0755"
  tags: nextcloud

- name: Create Nextcloud Redis directory (redis UID 999)
  ansible.builtin.file:
    path: "{{ nextcloud_data_dir }}/redis"
    state: directory
    owner: "999"
    group: "999"
    mode: "0700"
  tags: nextcloud

# www-data inside the container is UID 33. The html directory must be owned by
# this user so Apache can traverse it and process .htaccess files.
- name: Create Nextcloud html directory (www-data UID 33)
  ansible.builtin.file:
    path: "{{ nextcloud_data_dir }}/html"
    state: directory
    owner: "33"
    group: "33"
    mode: "0755"
  tags: nextcloud

- name: Create Nextcloud database directory (postgres UID 999)
  ansible.builtin.file:
    path: "{{ nextcloud_data_dir }}/db"
    state: directory
    owner: "999"
    group: "999"
    mode: "0700"
  tags: nextcloud

- name: Deploy Docker Compose file
  ansible.builtin.template:
    src: docker-compose.yml.j2
    dest: "{{ nextcloud_data_dir }}/docker-compose.yml"
    owner: root
    group: root
    mode: "0644"
  notify: Restart nextcloud
  tags: nextcloud

- name: Start Nextcloud
  community.docker.docker_compose_v2:
    project_src: "{{ nextcloud_data_dir }}"
    state: present
    pull: missing
  tags: nextcloud
