---

- name: Install system dependencies
  package: name={{ penpot_packages }}
  tags: penpot

- name: Install nodejs tools
  npm: name={{ item }} global=True
  loop:
    - yarn
    - sfnt2woff
  tags: penpot

- when: system_proxy is defined and system_proxy != ''
  block:

    - name: Create maven configuration dir
      file: path={{ penpot_root_dir }}/.m2 state=directory owner={{ penpot_user }} group={{ penpot_user }}

    - name: Deploy maven configuration
      template: src=../maven/templates/maven.xml.j2 dest={{ penpot_root_dir }}/.m2/settings.xml owner={{ penpot_user }} group={{ penpot_user }}

  tags: penpot

- when: system_proxy is not defined or system_proxy == ''
  name: Remove maven proxy config
  file: path={{ penpot_root_dir }}/.m2/settings.xml state=absent
  tags: penpot

- when: penpot_install_mode != 'none'
  block:

    - name: Download penpot
      get_url:
        url: "{{ penpot_archive_url }}"
        dest: "{{ penpot_root_dir }}/tmp/"
        checksum: sha256:{{ penpot_archive_sha256 }}

    - name: Extract penpot archive
      unarchive:
        src: "{{ penpot_root_dir }}/tmp/penpot-{{ penpot_version }}.tar.gz"
        dest: "{{ penpot_root_dir }}/tmp/"
        remote_src: True

    - name: Build penpot backend
      command: ./scripts/build
      args:
        chdir: "{{ penpot_root_dir }}/tmp/penpot-{{ penpot_version }}/backend"

    - name: Build penpot frontend
      command: ./scripts/build
      args:
        chdir: "{{ penpot_root_dir }}/tmp/penpot-{{ penpot_version }}/frontend"
      environment:
        CURRENT_HASH: "{{ penpot_version }}"

    - name: Build penpot exporter
      command: ./scripts/build
      args:
        chdir: "{{ penpot_root_dir }}/tmp/penpot-{{ penpot_version }}/exporter"

    - name: Install penpot exporter dependencies
      command: yarn install
      args:
        chdir: "{{ penpot_root_dir }}/tmp/penpot-{{ penpot_version }}/exporter/target"

  become_user: "{{ penpot_user }}"
  tags: penpot

- when: penpot_install_mode != 'none'
  block:

    - name: Install penpot backend and frontend
      synchronize:
        src: "{{ penpot_root_dir }}/tmp/penpot-{{ penpot_version }}/{{ item }}/target/dist/"
        dest: "{{ penpot_root_dir }}/{{ item }}/"
        delete: True
        compress: False
      delegate_to: "{{ inventory_hostname }}"
      loop:
        - backend
        - frontend

    - name: Install penpot exporter
      synchronize:
        src: "{{ penpot_root_dir }}/tmp/penpot-{{ penpot_version }}/exporter/target/"
        dest: "{{ penpot_root_dir }}/exporter/"
        delete: True
        compress: False
      delegate_to: "{{ inventory_hostname }}"

    - name: Set permissions
      shell: |
        setfacl -R -k -b {{ penpot_root_dir }}/
        setfacl -m u:nginx:x {{ penpot_root_dir }}/
        setfacl -m u:nginx:x {{ penpot_root_dir }}/data
        setfacl -R -m u:nginx:rX {{ penpot_root_dir }}/data/assets

  tags: penpot

- block:
    - name: Create the PostgreSQL role
      postgresql_user:
        db: postgres
        name: "{{ penpot_db_user }}"
        password: "{{ penpot_db_pass }}"
        login_host: "{{ penpot_db_server }}"
        login_user: sqladmin
        login_password: "{{ pg_admin_pass }}"

    - name: Create the PostgreSQL database
      postgresql_db:
        name: "{{ penpot_db_name }}"
        encoding: UTF-8
        template: template0
        owner: "{{ penpot_db_user }}"
        login_host: "{{ penpot_db_server }}"
        login_user: sqladmin
        login_password: "{{ pg_admin_pass }}"

  tags: penpot

- name: Deploy systemd units
  template: src={{ item }}.j2 dest=/etc/systemd/system/{{ item }}
  loop:
    - penpot-server.service
    - penpot-exporter.service
  register: penpot_units
  notify: restart penpot
  tags: penpot

- name: Reload systemd
  systemd: daemon_reload=True
  when: penpot_units.results | selectattr('changed','equalto',True) | list | length > 0
  tags: penpot

- name: Install backup hooks
  template: src={{ item }}-backup.j2 dest=/etc/backup/{{ item }}.d/penpot mode=700
  loop:
    - pre
    - post
  tags: penpot

