---
  - import_role:
      name: _app_log_inventory
    vars:
      log_type: "upgrade"

  - name: "create temporary download directory"
    tempfile:
      state: directory
      suffix: "_nextcloud_new"
    register: _tmp_new_nextcloud
    changed_when: False

  - name: "check local release for nextcloud__instance"
    local_action: stat path=$PWD/{{ local_release }}
    register: local_release_file

  - name: "retrieving nextcloud version {{ app_version }} in {{ _tmp_new_nextcloud.path }}"
    unarchive:
      remote_src: True
      src: "https://download.nextcloud.com/server/releases/nextcloud-{{ app_version }}.tar.bz2"
      dest: "{{ _tmp_new_nextcloud.path }}"
      group: "www-data"
      owner: "www-data"
    when: not(local_release_file.stat.exists)

  - name: "retrieving nextcloud from ./{{ local_release }} in {{ _tmp_new_nextcloud.path }}"
    unarchive:
      src: "{{ local_release }}"
      dest: "{{ _tmp_new_nextcloud.path }}"
      group: "www-data"
      owner: "www-data"
    when: local_release_file.stat.exists

  - name: "disable site for {{ app_domain }}"
    file:
      state: absent
      path: "/etc/{{ rev_proxy }}/sites-enabled/{{ app_instance_id }}.conf"

  - name: "reload {{ rev_proxy }}"
    service: name={{ rev_proxy }} state=reloaded

  - import_role:
      name: _app_backup_instance

  - name: "backup app list"
    shell: "php{{ php_version }} {{ www_root }}/{{ app_instance_id}}/occ app:list > {{ backup_version_dir }}/{{ app_instance_id }}/{{ app_old_version }}/app.list"
    become_user: "www-data"
    vars:
      ansible_ssh_pipelining: true
    args:
      creates: "{{ backup_version_dir }}/{{ app_instance_id }}/{{ app_old_version }}/app.list"

  - name: "remove {{ www_root }}/{{ app_instance_id }}"
    file:
      path: "{{ www_root }}/{{ app_instance_id }}"
      state: absent

  - name: "Move {{ _tmp_new_nextcloud.path }}/nextcloud to {{ www_root }}/{{ app_instance_id }}"
    command: "/bin/mv {{ _tmp_new_nextcloud.path }}/nextcloud {{ www_root }}/{{ app_instance_id }}"

  - name: "remove {{ _tmp_new_nextcloud.path }}"
    file:
      path: "{{ _tmp_new_nextcloud.path }}"
      state: absent
    changed_when: False

  - name: "restore config.php"
    copy:
      src: "{{ backup_version_dir }}/{{ app_instance_id }}/{{ app_old_version }}/app/{{ app_instance_id}}/config/config.php"
      dest: "{{ www_root }}/{{ app_instance_id }}/config/config.php"
      remote_src: yes
      backup: yes

  # cron task or something ? may log while migrating
  - name: "remove default data directory"
    file:
      state: absent
      path: "{{ www_root }}/{{ app_instance_id }}/data"

  - name: "www-data owner of {{ www_root }}/{{ app_instance_id }}"
    file:
      state: directory
      path: "{{ www_root }}/{{ app_instance_id }}"
      recurse: yes
      owner: "www-data"
      group: "www-data"

  - name: "chmod 750 for directories"
    command: "find {{ www_root }}/{{ app_instance_id }}/ -type d -exec chmod 750 {} \\;"

  - name: "chmod 640 for files"
    command: "find {{ www_root }}/{{ app_instance_id }}/ -type f -exec chmod 640 {} \\;"

  - name: "enable site for {{ app_domain }}"
    file:
      state: link
      path: "/etc/{{ rev_proxy }}/sites-enabled/{{ app_instance_id }}.conf"
      src: "/etc/{{ rev_proxy }}/sites-available/{{ app_instance_id }}.conf"

  - name: "ensure presence of {{ base_prod_options }}/nextcloud/"
    file:
      path: "{{ base_prod_options }}/nextcloud/"
      state: directory

  - name: "template for app enable script"
    template:
      src: "nextcloud_app_enable.j2"
      dest: "{{ base_prod_options }}/nextcloud/nextcloud_app_enable.py"
      mode: 0700

  - name: "restore apps"
    command: "{{ base_prod_options }}/nextcloud/nextcloud_app_enable.py {{ backup_version_dir }}/{{ app_instance_id }}/{{ app_old_version }}/app.list {{ www_root }}/{{ app_instance_id }}"
    failed_when: False

  - name: "reload {{ rev_proxy }}"
    service: name={{ rev_proxy }} state=reloaded

  - name: "perform nextcloud upgrade"
    command: "php{{ php_version }} {{ www_root }}/{{ app_instance_id }}/occ upgrade"
    become_user: "www-data"
    vars:
      ansible_ssh_pipelining: true
