- name: "Install 7 packages for Lokole: python3, python3-pip, python3-venv, python3-dev, libffi-dev, libssl-dev, bcrypt"
  apt:
    name:
      - python3
      - python3-pip
      - python3-venv
      - python3-dev
      - libffi-dev
      - libssl-dev
      - bcrypt
    state: present
  tags:
    - install

- name: pip install opwen_email_client (Lokole) {{ lokole_version }} from PyPI to {{ lokole_venv }}
  pip:
    name: opwen_email_client
    version: "{{ lokole_version }}"
    virtualenv: "{{ lokole_venv }}"
    virtualenv_command: python3 -m venv "{{ lokole_venv }}"
  tags:
    - install
  when: internet_available | bool

- name: Compile translations
  shell: |
    python_version=$(python3 -c 'from sys import version_info; print("%s.%s" % (version_info.major, version_info.minor));';)
    {{ lokole_venv }}/bin/pybabel compile -d {{ item }}/translations
  with_items:
    - "{{ lokole_venv }}/lib/python${python_version}/site-packages/opwen_email_client/webapp"
  tags:
    - install

- name: Create dir {{ lokole_run_directory }}
  file: 
    path: "{{ lokole_run_directory }}"
    state: directory
  tags:
    - configure

- name: Install {{ lokole_run_directory }}/webapp_secrets.sh from template, to configure Lokole
  template:
    src: webapp_secrets.sh.j2
    dest: "{{ lokole_run_directory }}/webapp_secrets.sh"
  tags:
    - configure

- name: Install {{ lokole_run_directory }}/webapp.sh from template, to configure Gunicorn
  template:
    src: webapp.sh.j2
    dest: "{{ lokole_run_directory }}/webapp.sh"
    mode: a+x
  tags:
    - configure

- name: Create admin user
  shell: |
    . {{ lokole_run_directory }}/webapp_secrets.sh
    {{ lokole_venv }}/bin/manage.py createadmin --name='{{ lokole_admin_user }}' --password='{{ lokole_admin_password }}'
  tags:
    - configure

- name: Install unit file /etc/systemd/system/lokole.service from template
  template:
    src: lokole.service.j2
    dest: /etc/systemd/system/lokole.service
  tags:
    - systemd

- name: Enable & Restart 'lokole' systemd service, with daemon_reload, if lokole_enabled
  systemd:
    daemon_reload: yes
    name: lokole
    enabled: yes
    state: restarted
  when: lokole_enabled | bool

- name: Disable 'lokole' service, if not lokole_enabled
  systemd:
    name: lokole
    enabled: no
    state: stopped
  when: not lokole_enabled

- name: Install /etc/{{ apache_config_dir }}/lokole.conf from template, for http://box/lokole
  template:
    src: lokole.conf.j2
    dest: "/etc/{{ apache_config_dir }}/lokole.conf"

- name: Symlink /etc/apache2/sites-enabled/lokole.conf to /etc/{{ apache_config_dir }}/lokole.conf, if lokole_enabled (debuntu)
  file:
    src: "/etc/{{ apache_config_dir }}/lokole.conf"
    path: /etc/apache2/sites-enabled/lokole.conf
    state: link
  when: lokole_enabled and is_debuntu

- name: Remove /etc/apache2/sites-enabled/lokole.conf, if not lokole_enabled (debuntu)
  file:
    path: /etc/apache2/sites-enabled/lokole.conf
    state: absent
  when: not lokole_enabled and is_debuntu

- name: Remove /etc/{{ apache_config_dir }}/lokole.conf, if not lokole_enabled (OS's other than debuntu)
  file:
    path: "/etc/{{ apache_config_dir }}/lokole.conf"
    state: absent
  when: (not lokole_enabled) and (not is_debuntu)

- name: Restart Apache ({{ apache_service }}) to enable/disable http://box/lokole
  service:
    name: "{{ apache_service }}"
    state: restarted

- name: Add 'lokole' variable values to {{ iiab_ini_file }}
  ini_file:
    path: "{{ iiab_ini_file }}"
    section: lokole
    option: "{{ item.option }}"
    value: "{{ item.value }}"
  with_items:
    - option: name
      value: lokole
    - option: description
      value: '"Lokole is an email service that works offline, for rural communities."'
    - option: lokole_run_directory
      value: "{{ lokole_run_directory }}"
    - option: lokole_url
      value: "{{ lokole_url }}"
    - option: lokole_full_url
      value: "{{ lokole_full_url }}"
    - option: lokole_enabled
      value: "{{ lokole_enabled }}"
