---

- name: Setup... Set APCU config for Nextcloud
  copy:
    dest: "{{ http_webroot }}/nextcloud/config/apcu.config.php"
    src: files/apcu.config.php
    owner: "{{ nextcloud_websrv_user }}"
    group: "{{ nextcloud_websrv_group }}"
    mode: 0640

- name: Setup... Set background image
  copy:
    dest: "{{ http_webroot }}/nextcloud/apps/theming/img/background/pexels-jaymantri-5439.jpg"
    src: files/pexels-jaymantri-5439.jpg
    owner: "{{ nextcloud_websrv_user }}"
    group: "{{ nextcloud_websrv_group }}"
    mode: 0640
  when: NEXTCLOUD_VERSION|string is version('25.0.1', '>=')

- name: Setup... Set custom_apps config for Nextcloud
  copy:
    dest: "{{ http_webroot }}/nextcloud/config/apps.config.php"
    src: files/apps.config.php
    owner: "{{ nextcloud_websrv_user }}"
    group: "{{ nextcloud_websrv_group }}"
    mode: 0640

- name: Setup... Check disabled apps list
  shell: "{{ php_bin }} occ app:list --no-warnings | grep -A30 'Disabled' | grep -v 'Disabled' | cut -d'-' -f2 | cut -d':' -f1 | grep -v 'encryption'"
  args:
    chdir: "{{ http_webroot }}/nextcloud"
  become_user: "{{ nextcloud_websrv_user }}"
  become: true
  register: nc_apps_list
  failed_when: nc_apps_list.rc >= 2

- name: Setup... Enable all disabled apps
  become_user: "{{ nextcloud_websrv_user }}"
  become: true
  shell: "{{ php_bin }} occ app:enable {{ item }}"
  args:
    chdir: "{{ http_webroot }}/nextcloud"
  with_items: "{{ nc_apps_list.stdout_lines }}"
  when: nc_apps_list.rc == 0
  ignore_errors: yes

- name: Setup... Applying default settings
  become_user: "{{ nextcloud_websrv_user }}"
  become: true
  shell: "{{ php_bin }} occ {{ item }}"
  args:
    chdir: "{{ http_webroot }}/nextcloud"
  loop:
    - "config:system:set loglevel --value='{{ nc_loglevel }}'"
    - "config:system:set log_type --value=file"
    - "config:system:set logfile --value='{{ nc_data_dir }}/nextcloud.log'"
    - "config:system:set log_rotate_size --value='{{ nc_log_rotate_size }}'"
    - "config:app:set admin_audit logfile --value='{{ nc_data_dir }}/audit.log'"
    - "config:system:set log.condition apps 0 --value='admin_audit'"
  loop_control:
    pause: 2

- name: Setup... Applying preview settings
  become_user: "{{ nextcloud_websrv_user }}"
  become: true
  shell: "{{ php_bin }} occ {{ item }}"
  args:
    chdir: "{{ http_webroot }}/nextcloud"
  loop:
    - "config:system:set preview_max_x --value='2048'"
    - "config:system:set preview_max_y --value='2048'"
    - "config:app:set preview jpeg_quality --value='60'"
    - "config:system:set jpeg_quality --value='60'"
    - "config:system:delete enabledPreviewProviders"
    - "config:system:set enabledPreviewProviders 1 --value='OC\\Preview\\Image'"
    - "config:system:set enabledPreviewProviders 2 --value='OC\\Preview\\MarkDown'"
    - "config:system:set enabledPreviewProviders 3 --value='OC\\Preview\\MP3'"
    - "config:system:set enabledPreviewProviders 4 --value='OC\\Preview\\TXT'"
    - "config:system:set enabledPreviewProviders 5 --value='OC\\Preview\\OpenDocument'"
    - "config:system:set enabledPreviewProviders 6 --value='OC\\Preview\\Movie'"
    - "config:system:set enable_previews --value=true --type=boolean"
  loop_control:
    pause: 2

- name: Setup... Applying other settings
  become_user: "{{ nextcloud_websrv_user }}"
  become: true
  shell: "{{ php_bin }} occ {{ item }}"
  args:
    chdir: "{{ http_webroot }}/nextcloud"
  loop:
    - "config:system:set upgrade.disable-web --type=bool --value=true"
    - "config:system:set trashbin_retention_obligation --value='auto, 30'"
    - "config:system:set versions_retention_obligation --value='auto, 30'"
    - "config:system:set activity_expire_days --value='30'"
    - "config:system:set simpleSignUpLink.shown --type=bool --value=false"
    #- "config:system:set share_folder --value='/Shared'"
    - "theming:config url 'https://nextcloud.cism.ucl.ac.be'"
  loop_control:
    pause: 2

- name: Setup... Applying recent settings
  become_user: "{{ nextcloud_websrv_user }}"
  become: true
  shell: "{{ php_bin }} occ {{ item }}"
  args:
    chdir: "{{ http_webroot }}/nextcloud"
  loop:
    - "theming:config background {{ http_webroot }}/nextcloud/apps/theming/img/background/pexels-jaymantri-5439.jpg"
  loop_control:
    pause: 2
  when: NEXTCLOUD_VERSION|string is version('25.0.1', '>=')

- name: Setup... Set Nextcloud system settings in config.php
  become_user: "{{ nextcloud_websrv_user }}"
  become: true
  shell: "{{ php_bin }} occ config:system:set {{ item.name }} --value={{ item.value }}"
  args:
    chdir: "{{ http_webroot }}/nextcloud"
  with_items:
    - "{{ nextcloud_config_settings }}"

- name: Setup... Set Redis setup
  template:
    dest: "{{ http_webroot }}/nextcloud/config/redis.config.php" 
    src: redis.config.php.j2
    owner: "{{ nextcloud_websrv_user }}"
    group: "{{ nextcloud_websrv_group }}"
    mode: 0640

- name: Setup... Disabling filelocking because of Ceph
  become_user: "{{ nextcloud_websrv_user }}"
  become: true
  shell: "{{ php_bin }} occ config:system:set filelocking.enabled --value=false"
  args:
    chdir: "{{ http_webroot }}/nextcloud"
  when: "'ceph_servers' in groups.keys() or objectstore_s3_install"

- name: Setup... Install Nextcloud Apps
  become_user: "{{ nextcloud_websrv_user }}"
  become: true
  shell: "{{ php_bin }} occ app:install {{ item }}"
  args:
    chdir: "{{ http_webroot }}/nextcloud"
  with_items: "{{ nextcloud_apps }}"
  register: nc_apps_installed
  failed_when: nc_apps_installed.rc >= 2

- name: Setup... Set Cron method to Crontab
  become_user: "{{ nextcloud_websrv_user }}"
  become: true
  shell: "{{ php_bin }} occ background:cron"
  args:
    chdir: "{{ http_webroot }}/nextcloud"
  when: (nc_background_cron | bool)

      ###- name: Setup... "[NC] Set Custom Mimetype"
      ###  ansible.builtin.copy:
      ###    dest: "{{ nextcloud_webroot }}/config/mimetypemapping.json"
      ###    src: files/nextcloud_custom_mimetypemapping.json
      ###    mode: 0640
      ###

- name: Setup... Collabora settings ownership
  file:
    path: "{{ item }}"
    recurse: true
    owner: cool
    group: cool
  with_items:
    - /opt/cool/systemplate/etc/hosts
    - /opt/cool/systemplate/etc/resolv.conf
    - /etc/coolwsd
  when: nc_collabora

  #- name: Main... Setting stronger directories ownership
  #  file:
  #    path: "{{ item }}"
  #    state: directory
  #    owner: "{{ nextcloud_websrv_user }}"
  #    group: "{{ nextcloud_websrv_group }}"
  #    recurse: yes
  #    mode: 0750
  #  with_items:
  #    - "{{ nc_data_dir }}"
  #    - "{{ http_webroot }}"

      #- name: Setup... Ensure Nextcloud directories are 0750
      #  shell: find {{ http_webroot }}/nextcloud -type d -exec chmod -c 0750 {} \;
      #  register: nc_installation_chmod_result
      #  changed_when: "nc_installation_chmod_result.stdout != \"\""
      #
      #- name: Setup... Ensure Nextcloud files are 0640
      #  shell: find {{ http_webroot }}/nextcloud -type f -exec chmod -c 0640 {} \;
      #  register: nc_installation_chmod_result
      #  changed_when: "nc_installation_chmod_result.stdout != \"\""

      ###- name: Setup... "[NC] Setting stronger directory ownership"
      ###  ansible.builtin.file:
      ###    path: "{{ nextcloud_webroot }}/{{ item }}/"
      ###    recurse: true
      ###    owner: "{{ nextcloud_websrv_user }}"
      ###    group: "{{ nextcloud_websrv_group }}"
      ###    state: directory
      ###  with_items:
      ###    - apps
      ###    - custom_apps
      ###    - config
      ###    - themes
      ###    - updater
