---
#########
# Run command line installation.
# the web server must be running by now in order to launch the installation

- name: Install... Removing possibly old or incomplete config.php
  file:
    path: "{{ http_webroot }}/nextcloud/config/config.php"
    state: absent

- name: Install... Create custom_apps directory
  file:
    path: "{{ http_webroot }}/nextcloud/custom_apps"
    state: directory
    owner: "{{ nextcloud_websrv_user }}"
    group: "{{ nextcloud_websrv_group }}"
    mode: 0770

- name: Install... Create data directory
  file:
    path: "{{ nc_data_dir }}"
    state: directory
    owner: "{{ nextcloud_websrv_user }}"
    group: "{{ nextcloud_websrv_group }}"
    mode: 0770

- name: Install... Mount NFS directory (or not)
  mount:
    src: "{{ nfs_server }}:{{ nfs_path }}"
    path: "{{ nc_data_dir }}"
    state: mounted
    fstype: nfs 
    opts: nosharecache,context="system_u:object_r:httpd_sys_rw_content_t:s0"
  when: nfs_data_dir

- name: Install... First setup Nextcloud
  become_user: "{{ nextcloud_websrv_user }}"
  become: true
  shell: "{{ php_bin }} occ maintenance:install --database=mysql --database-host={{ db_host }} --database-name={{ nc_db_name }} --database-user={{ nc_db_user }} --database-pass={{ nc_db_password }} --admin-user={{ nc_admin_name }} --admin-pass={{ nc_admin_pwd }} --data-dir={{ nc_data_dir }}"
  args:
    chdir: "{{ http_webroot }}/nextcloud"
    creates: "{{ http_webroot }}/nextcloud/config/config.php"
  register: setup_nc
  when: "numbertables.stdout|int == 0"

- name: Install... Removing possibly sample config
  file:
    path: "{{ http_webroot }}/nextcloud/config/config.sample.php"
    state: absent
