---
- name: wait for existence of config directory
  ansible.builtin.wait_for:
    path: /ansible_docker_compose/nextcloud_var_www_html/config
    state: present
  become: true

- name: extra Nextcloud configuration
  ansible.builtin.copy:
    content: "{{ nextcloud__extra_configuration }}"
    dest: /ansible_docker_compose/nextcloud_var_www_html/config/ansible_nextcloud_extra_config.config.php
    mode: "0644"
    owner: www-data
    group: www-data
  become: true

- name: fail, if nextcloud__use_custom_new_user_skeleton is set, but nextcloud__custom_new_user_skeleton_directory isn't
  ansible.builtin.fail:
    msg: If you set nextcloud__use_custom_new_user_skeleton, you also need to set nextcloud__custom_new_user_skeleton_directory.
  when: nextcloud__use_custom_new_user_skeleton and nextcloud__custom_new_user_skeleton_directory == ""

- name: ensure custom new user skeleton
  when: nextcloud__use_custom_new_user_skeleton
  block:
    - name: ensure `rsync` package is installed
      ansible.builtin.apt:
        name: rsync
        state: present
      become: true

    - name: ensure custom new user skeleton directory
      ansible.posix.synchronize:
        src: "{{ nextcloud__custom_new_user_skeleton_directory }}"
        dest: /ansible_docker_compose/custom_new_user_skeleton
        delete: true
        recursive: true
        use_ssh_args: true
      become: true

    - name: ensure custom new user skeleton config
      ansible.builtin.copy:
        content: |
          <?php
          $CONFIG = array (
              'skeletondirectory' => '/custom_new_user_skeleton'
          );
        dest: /ansible_docker_compose/nextcloud_var_www_html/config/ansible_nextcloud_custom_new_user_skeleton.config.php
        mode: "0644"
        owner: www-data
        group: www-data
      become: true

- name: ensure absence of custom new user skeleton config
  ansible.builtin.file:
    path: /ansible_docker_compose/nextcloud_var_www_html/config/ansible_nextcloud_custom_new_user_skeleton.config.php
    state: absent
  become: true
  when: not nextcloud__use_custom_new_user_skeleton