---
# Install build-essential for konga
# Required during npm install phase (needs make, g++, etc...)
- name: Installing build-essential for konga
  apt:
    update_cache: yes
    state: present
    name:
      - build-essential

# Create /var/www folder
- name: Creating /var/www directory
  file:
    path: /var/www
    state: directory
    owner: '{{ superuser }}'
    group: '{{ superuser }}'
    mode: '0755'

# Create /var/www/konga folder
- name: Creating /var/www/konga directory
  file:
    path: /var/www/konga
    state: directory
    owner: '{{ superuser }}'
    group: '{{ superuser }}'
    mode: '0755'

# Git clone https://github.com/pantsel/konga.git
- name: Git clone https://github.com/pantsel/konga.git tag 0.14.7
  # NOTICE: running as superuser
  become_user: '{{ superuser }}'
  git:
    clone: yes
    force: yes
    repo: https://github.com/pantsel/konga.git
    dest: /var/www/konga
    version: 0.14.7
    depth: 1

# Ensure konga git contents owned by superuser
- name: Ensuring konga git contents owned by superuser
  file:
    path: /var/www/konga
    state: directory
    owner: '{{ superuser }}'
    group: '{{ superuser }}'
    recurse: yes

# Delete package-lock.json
- name: Deleting konga package-lock.json
  file:
    path: /var/www/konga/package-lock.json
    state: absent

# Install konga npm packages
- name: Installing konga NPM packages
  # NOTICE: running as superuser
  become_user: '{{ superuser }}'
  npm:
    path: /var/www/konga

# Install konga bower dependencies
- name: Installing konga bower dependencies
  # NOTICE: running as superuser
  become_user: '{{ superuser }}'
  shell: npm run bower-deps
  args:
    chdir: /var/www/konga

# Ensure konga git contents owned by superuser
- name: Ensuring konga git contents owned by superuser
  file:
    path: /var/www/konga
    state: directory
    owner: '{{ superuser }}'
    group: '{{ superuser }}'
    recurse: yes

# Copy konga systemd unit file
- name: Copying konga.service systemd unit file
  template:
    src: konga.service
    dest: "/etc/systemd/system/konga.service"
    owner: root
    group: root
    mode: '0644' # -rw-r--r--
  notify: reload konga
