---

- name: check for config file
  stat:
    path: "{{nextcloud_directory}}/config/config.php"
  register: _nextcloud_config_file

- name: stop apache2
  service:
    name: apache2
    state: stopped
  when: not _nextcloud_config_file.stat.isreg is defined

- name: nextcloud initial config file
  template:
    src: config.php.j2
    dest: "{{nextcloud_directory}}/config/config.php"
    owner: www-data
    group: www-data
  when: not _nextcloud_config_file.stat.isreg is defined

- name: install nextcloud
  command:
    php occ maintenance:install
    --admin-user "{{ nextcloud_admin_user }}"
    --admin-pass "{{ nextcloud_admin_pass }}"
    --database "{{ nextcloud_database_type }}"
    --database-host "{{ nextcloud_database_host == inventory_hostname | ternary('localhost',nextcloud_database_host) }}"
    --database-name "{{ nextcloud_database_name }}"
    --database-user "{{ nextcloud_database_user }}"
    --database-pass "{{ nextcloud_database_pass }}"
    --data-dir "{{ nextcloud_data_directory }}"
  args:
    chdir: "{{nextcloud_directory}}"
  become: true
  become_user: www-data
  become_method: sudo
  when: not _nextcloud_config_file.stat.isreg is defined