---

- import_tasks: ../includes/webapps_webconf.yml
  vars:
    - app_id: kimai_{{ kimai_id }}
    - php_version: "{{ kimai_php_version }}"
    - php_fpm_pool: "{{ kimai_php_fpm_pool | default('') }}"
  tags: kimai

- name: Deploy kimai configuration
  template: src={{ item.src }} dest={{ item.dest }} group={{ kimai_php_user }} mode=640
  loop:
    - src: env.j2
      dest: "{{ kimai_root_dir }}/app/.env"
    - src: local.yaml.j2
      dest: "{{ kimai_root_dir }}/app/config/packages/local.yaml"
  notify: clear kimai cache
  tags: kimai

- name: Deploy permission script
  template: src=perms.sh.j2 dest={{ kimai_root_dir }}/perms.sh mode=755
  register: kimai_perm_script
  tags: kimai

- name: Apply permissions
  command: "{{ kimai_root_dir }}/perms.sh"
  when: kimai_perm_script.changed or kimai_install_mode != 'none'
  tags: kimai

- when: kimai_install_mode == 'install'
  block:

    - name: Init the database
      command: /bin/php{{ kimai_php_version }} {{ kimai_root_dir }}/app/bin/console kimai:install -n

    - name: Create initial admin user
      shell: echo {{ kimai_admin_pass | quote }} | /bin/php{{ kimai_php_version }} {{ kimai_root_dir }}/app/bin/console kimai:user:create username {{ kimai_admin_user | quote }} ROLE_SUPER_ADMIN

  become_user: "{{ kimai_php_user }}"
  tags: kimai

- when: kimai_install_mode == 'upgrade'
  block:

    - name: Update the database
      command: /bin/php{{ kimai_php_version }} {{ kimai_root_dir }}/app/bin/console kimai:update -n

    - name: Clear kimai cache
      command: /bin/php{{ kimai_php_version }} {{ kimai_root_dir }}/app/bin/console kimai:reload --env=prod

  become_user: "{{ kimai_php_user }}"
  tags: kimai

- name: Setup logrotate
  template: src=logrotate.conf.j2 dest=/etc/logrotate.d/kimai_{{ kimai_id | string }}
  tags: kimai
