---

- name: Update .htaccess
  block:
    - name: Make .htaccess writable
      file:
        path: "{{ nextcloud_installation_dir }}/.htaccess"
        mode: g+w
      listen: nextcloud update htaccess
    - name: Update .htaccess file
      command: php occ maintenance:update:htaccess --no-interaction
      args:
        chdir: "{{ nextcloud_installation_dir }}"
      listen: nextcloud update htaccess
      become: true
      become_user: "{{ nextcloud_file_owner }}"
    - name: Make .htaccess unwritable again
      file:
        path: "{{ nextcloud_installation_dir }}/.htaccess"
        mode: g-w
      listen: nextcloud update htaccess

- name: Set file permissions on Nextcloud files
  block:
    - name: Set Nextcloud file permissions
      file:
        path: "{{ nextcloud_installation_dir }}"
        mode: u=rwX,g=rX,o-rwx
        owner: "{{ nextcloud_file_owner }}"
        group: "{{ nextcloud_file_owner }}"
        recurse: true
      listen: nextcloud set file permissions
    - name: Set permissions on installation directory
      file:
        path: "{{ nextcloud_installation_dir }}"
        owner: root
        group: "{{ nextcloud_file_owner }}"
        mode: 0o750
        state: directory
      listen: nextcloud set file permissions
    - name: Set permissions on htaccess file
      file:
        path: "{{ nextcloud_installation_dir }}/.htaccess"
        owner: root
        group: "{{ nextcloud_file_owner }}"
        mode: 0o644
      listen: nextcloud set file permissions