---

- name: Install dependencies
  package: name={{ kimai_packages }}
  tags: kimai

- when: kimai_install_mode != 'none'
  block:
    - name: Download kimai
      get_url:
        url: "{{ kimai_archive_url }}"
        dest: "{{ kimai_root_dir }}/tmp"
        checksum: sha256:{{ kimai_archive_sha256 }}
      register: kimai_dl_archive
      retries: 3
      delay: 5
      until: kimai_dl_archive is not failed

    - name: Extract the archive
      unarchive:
        src: "{{ kimai_root_dir }}/tmp/kimai2-{{ kimai_version }}.tar.gz"
        dest: "{{ kimai_root_dir }}/tmp"
        remote_src: True

    - name: Move kimai to its final dir
      synchronize:
        src: "{{ kimai_root_dir }}/tmp/kimai2-{{ kimai_version }}/"
        dest: "{{ kimai_root_dir }}/app/"
        delete: True
        compress: False
        rsync_opts:
          - '--exclude=/var'
      delegate_to: "{{ inventory_hostname }}"

    - name: Populate data directories
      synchronize:
        src: "{{ kimai_root_dir }}/tmp/kimai2-{{ kimai_version }}/var/"
        dest: "{{ kimai_root_dir }}/data/"
        compress: False
      delegate_to: "{{ inventory_hostname }}"

    - name: Link data directories
      file: src={{ kimai_root_dir }}/data dest={{ kimai_root_dir }}/app/var state=link

    - name: Install PHP libs with composer
      composer:
        command: install
        working_dir: "{{ kimai_root_dir }}/app"
        executable: /bin/php{{ kimai_php_version }}

    - name: Install LDAP auth support
      composer:
        command: require
        arguments: laminas/laminas-ldap
        working_dir: "{{ kimai_root_dir }}/app"
        executable: /bin/php{{ kimai_php_version }}

  tags: kimai

- import_tasks: ../includes/webapps_create_mysql_db.yml
  vars:
    - db_name: "{{ kimai_db_name }}"
    - db_user: "{{ kimai_db_user }}"
    - db_server: "{{ kimai_db_server }}"
    - db_port: "{{ kimai_db_port }}"
    - db_pass: "{{ kimai_db_pass }}"
  tags: kimai

- name: Set correct SELinux context
  sefcontext:
    target: "{{ kimai_root_dir }}(/.*)?"
    setype: httpd_sys_content_t
    state: present
  when: ansible_selinux.status == 'enabled'
  tags: kimai

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

