---
#########
# 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
      #  when: nc_multiple == ""

- name: Install... Check if a mysql/mariadb database is available
  shell: mysql --host={{ db_host }} --user={{ nc_db_user }} --password={{ nc_db_password }} --port={{ db_port }} -e "show status like 'Connections%';"
  no_log: true

- 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-port={{ db_port }} --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
  #no_log: true

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