- name: Create Linux user {{ kolibri_user }} and add it to groups {{ apache_user }}, disk
  user:
    name: "{{ kolibri_user }}"
    groups:
      - "{{ apache_user }}"
      - disk
    state: present
    shell: /bin/false
    system: yes
    create_home: no

- name: Create {{ kolibri_home }} (for data) and {{ kolibri_venv_path }} (for program/config)
  file:
    path: "{{ item }}"
    owner: "{{ kolibri_user }}"
    group: "{{ apache_user }}"
    mode: 0755
    state: directory
  with_items:
    - "{{ kolibri_home }}"
    - "{{ kolibri_venv_path }}"

- name: Install latest kolibri using pip
  pip:
    name: kolibri
    virtualenv: "{{ kolibri_venv_path }}"
    virtualenv_site_packages: no
    state: latest
    extra_args: --no-cache-dir
  when: internet_available and not (kolibri_version is defined)

- name: Install kolibri {{ kolibri_version }} using pip
  pip:
    name: kolibri
    virtualenv: "{{ kolibri_venv_path }}"
    virtualenv_site_packages: no
    version: "{{ kolibri_version }}"
    extra_args: --no-cache-dir
  when: internet_available and kolibri_version is defined

- name: Run Kolibri migrations
  shell: export KOLIBRI_HOME="{{ kolibri_home }}" && "{{ kolibri_exec_path }}" manage migrate
  ignore_errors: yes
  when: kolibri_provision | bool

- name: Set Kolibri default language
  shell: export KOLIBRI_HOME="{{ kolibri_home }}" && "{{ kolibri_exec_path }}" language setdefault "{{ kolibri_language }}"
  ignore_errors: yes
  when: kolibri_provision | bool

- name: Create Kolibri default facility name, admin account and language
  shell: >
    export KOLIBRI_HOME="{{ kolibri_home }}" &&
    "{{ kolibri_exec_path }}" manage provisiondevice --facility "{{ kolibri_facility }}"
    --superusername "{{ kolibri_admin_user }}" --superuserpassword "{{ kolibri_admin_password }}"
    --preset "{{ kolibri_preset }}" --language_id "{{ kolibri_language }}" --verbosity 0 --noinput
  ignore_errors: yes
  when: kolibri_provision | bool

- name: chown -R {{ kolibri_user }}:{{ apache_user }} {{ kolibri_home }}
  file:
    path: "{{ kolibri_home }}"
    owner: "{{ kolibri_user }}"
    group: "{{ apache_user }}"
    recurse: yes

- name: Create kolibri systemd service unit file
  template:
    src: "{{ item.src }}"
    dest: "{{ item.dest }}"
    mode: "{{ item.mode }}"
    owner: root
    group: root
  with_items:
    - { src: 'kolibri.service.j2', dest: '/etc/systemd/system/kolibri.service', mode: '0644' }
    - { src: 'kolibri.conf.j2', dest: '/etc/apache2/sites-available/kolibri.conf', mode: '0644' }

- name: Enable & (Re)Start kolibri service
  systemd:
    name: kolibri
    enabled: yes
    state: restarted
    daemon_reload: yes
  when: kolibri_enabled | bool

# Default: http://box/kolibri
- name: Enable http://box{{ kolibri_url }} with Apache
  command: a2ensite kolibri.conf
  when: kolibri_enabled | bool

- name: Disable kolibri service
  systemd:
    name: kolibri
    enabled: no
    state: stopped
  when: not kolibri_enabled

- name: Disable http://box{{ kolibri_url }} with Apache
  command: a2dissite kolibri.conf
  when: not kolibri_enabled

# {{ apache_service }} is almost always apache2
- name: Restart Apache service ({{ apache_service }})
  systemd:
    name: "{{ apache_service }}"
    state: restarted

- name: Add 'kolibri' variable values to {{ iiab_ini_file }}
  ini_file:
    path: "{{ iiab_ini_file }}"
    section: kolibri
    option: "{{ item.option }}"
    value: "{{ item.value }}"
  with_items:
    - option: name
      value: kolibri
    - option: description
      value: '"Kolibri is an open-source educational platform specially designed to provide offline access to a wide range of quality, openly licensed educational contents in low-resource contexts like rural schools, refugee camps, orphanages, and also in non-formal school programs."'
    - option: kolibri_url
      value: "{{ kolibri_url }}"
    - option: kolibri_path
      value: "{{ kolibri_exec_path }}"
    - option: kolibri_port
      value: "{{ kolibri_http_port }}"
    - option: kolibri_enabled
      value: "{{ kolibri_enabled }}"
