---

- name: install etherpad deps
  become: yes
  apt:
    pkg:
      - git
      - libreoffice-common
      - tidy

- name: ensure etherpad user is present
  become: yes
  user:
    name: "etherpad"
    home: "/srv/etherpad"
    shell: "/bin/bash"
    state: present

- name: ensure etherpad is correct version
  git:
    repo: "https://github.com/ether/etherpad-lite"
    dest: "/srv/etherpad/etherpad"
    version: "1.8.14"
    force: true
  become: true
  become_user: "etherpad"
  register: repository

- name: ensure etherpad init.d is latest
  become: yes
  template:
    src: etherpad.initd.j2
    dest: /etc/init.d/etherpad
    owner: root
    group: root
    mode: 0500

- name: copy etherpad settings.json
  become: yes
  template:
    src: settings.json.j2
    dest: /srv/etherpad/etherpad/settings.json
    owner: etherpad
    group: etherpad
    mode: 0644

- name: ensure etherpad will start after system is booted
  become: yes
  service:
    name: etherpad
    enabled: yes

# - name: install etherpad deps
#   become: true
#   become_user: etherpad
#   shell: /srv/etherpad/etherpad/bin/installDeps.sh
#   args:
#     chdir: /srv/etherpad/etherpad/

- name: copy custom assets
  become: yes
  copy: 
    src: "{{playbook_dir}}/assets/etherpad/{{item.key}}"
    dest: "{{item.value}}{{item.key}}"
    owner: etherpad
    group: etherpad
  with_dict: "{{ override_files| default([]) }}"

- name: remove package-lock.json
  become: yes
  file:
    state: absent
    path: "/srv/etherpad/etherpad/package-lock.json"

- name: install etherpad plugins
  npm:
    name: "{{ item }}"
    path: "/srv/etherpad/etherpad"
  become: true
  become_user: etherpad
  with_items: "{{ plugins|d() }}"

- name: restart etherpad
  become: true
  service:
    name: etherpad
    state: restarted
