---
- name: Install deps
  package:
    state: latest
    name:
    - mariadb-server
    - redis
    - sendmail-bin
    - libmagickcore-6.q16-6-extra

- name: get and unzip nextcloud
  unarchive:
    src: "{{ nextcloud_url_download }}"
    dest: /var/www/
    owner: www-data
    group: www-data
    remote_src: yes
    creates: /var/www/nextcloud

- name: config nextcloud
  lineinfile:
    path: "/var/www/nextcloud/config/config.php"
    regex: ".*'{{ item.key }}'.*=>.*"
    insertbefore: "\\);"
    line: "  '{{ item.key }}' =>  {{ item.value }},"
  with_items:
  - key: "datadirectory"
    value: "'/data'"
  - key: "redis"
    value: "array('host' => '127.0.0.1', 'port' => 6379)"
  - key: "memcache.local"
    value: "'\\\\OC\\\\Memcache\\\\Redis'"
  - key: "memcache.distributed"
    value: "'\\\\OC\\\\Memcache\\\\Redis'"
  - key: "memcache.locking"
    value: "'\\\\OC\\\\Memcache\\\\Redis'"
  - key: "default_phone_region"
    value: "'de'"

- name: config acme
  notify: regenerate certificates
  lineinfile:
    path: "/etc/dehydrated/domains.txt"
    line: "{{ nextcloud_domain }}"
    create: yes

- name: config webserver
  notify: reload nginx
  template:
    src: "nginx"
    dest: "/etc/nginx/sites-available/nextcloud"
    mode: 660

- name: enable webserver
  notify: reload nginx
  file:
    src: "/etc/nginx/sites-available/nextcloud"
    dest: "/etc/nginx/sites-enabled/nextcloud"
    state: link

- name: Install nextcloud cronjob
  template:
    dest: "/etc/systemd/system/{{ item }}"
    src: "{{ item }}"
  with_items:
  - nextcloudcron.service
  - nextcloudcron.timer

- name: Start nextcloud cronjob
  systemd:
    name: nextcloudcron.timer
    state: started
    enabled: yes