# Lokole PDF (User's Guide) gets copied for offline use (http://box/info) here:
# https://github.com/iiab/iiab/blob/master/roles/www_base/templates/iiab-refresh-wiki-docs.sh#L51-L52


- name: Install 13 packages for Lokole
  apt:
    name:
      #- python3    # 2022-12-21: IIAB pre-req, see scripts/local_facts.fact
      #- python3-pip
      - python3-venv
      - python3-dev
      - python3-bcrypt    # 2019-10-14: Should work across modern Linux OS's
      #- bcrypt does not exist on Ubuntu 19.10
      - libffi-dev
      - libjpeg-dev
      - libssl-dev
      - libopenjp2-7    # 2020-02-01: To solve bug #2221
      - supervisor
      - usb-modeswitch
      - usb-modeswitch-data
      - mobile-broadband-provider-info
      - ppp
      - wvdial
    state: present


# For development purposes -- To install Lokole from a given commit, add the
# following line to roles/lokole/defaults/main.yml:
# lokole_commit: <git_commit_id>
- name: "OPTIONAL: pip install opwen_email_client (Lokole, git commit {{ lokole_commit }}) from GitHub to {{ lokole_venv }}, if lokole_commit is defined"
  pip:
    name: "git+{{ lokole_repo }}@{{ lokole_commit }}#egg=opwen_email_client"
    virtualenv: "{{ lokole_venv }}"
    virtualenv_command: python3 -m venv "{{ lokole_venv }}"
    extra_args: --no-cache-dir --force-reinstall    # To avoid caching issues e.g. soon after new releases hit https://pypi.org/project/opwen-email-client/
  when: lokole_commit is defined

# For development purposes -- To install a given pip version of Lokole, add
# the following line to roles/lokole/defaults/main.yml:
# lokole_version: <git_version_number>
- name: "OPTIONAL: pip install opwen_email_client (Lokole, version {{ lokole_version }}) from PyPI to {{ lokole_venv }}, if lokole_version is defined"
  pip:
    name: opwen_email_client
    version: "{{ lokole_version }}"
    virtualenv: "{{ lokole_venv }}"
    virtualenv_command: python3 -m venv "{{ lokole_venv }}"
    extra_args: --no-cache-dir    # To avoid caching issues e.g. soon after new releases hit https://pypi.org/project/opwen-email-client/
  when: lokole_version is defined

- name: "DEFAULT: pip install opwen_email_client (Lokole, latest available version) from PyPI to {{ lokole_venv }}, if above vars both UNdefined"
  pip:
    name: opwen_email_client
    virtualenv: "{{ lokole_venv }}"
    virtualenv_command: python3 -m venv "{{ lokole_venv }}"
    extra_args: --no-cache-dir    # To avoid caching issues e.g. soon after new releases hit https://pypi.org/project/opwen-email-client/
  when: lokole_commit is undefined and lokole_version is undefined

# - 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"

# 2022-12-21: python_version determined by scripts/local_facts.fact -- to match templates/lokole-nginx.conf.j2
- name: Compile translations for Python {{ python_version }}
  shell: |
    {{ lokole_venv }}/bin/pybabel compile -d {{ item }}/translations
  with_items:
    - "{{ lokole_venv }}/lib/python{{ python_version }}/site-packages/opwen_email_client/webapp"

- name: Create system {{ lokole_user }} user
  ansible.builtin.user:
    state: present
    name: "{{ lokole_user }}"
    #group: "{{ lokole_user }}"
    groups: dialout, dip
    system: yes
    #uid: "{{ lokole_uid }}"
    home: "{{ lokole_home_dir }}"

- name: mkdir {{ lokole_run_dir }}
  file:
    state: directory
    path: "{{ lokole_run_dir }}/lokole_restarter"
    group: "{{ lokole_user }}"
    owner: "{{ lokole_user }}"
    mode: g+rw

- name: mkdir {{ lokole_log_dir }}
  file:
    state: directory
    path: "{{ lokole_log_dir }}"
    group: "{{ lokole_user }}"
    owner: "{{ lokole_user }}"
    mode: g+rw

- name: Generate key and salt
  set_fact:
    lokole_key: "{{ lookup('password', '/dev/null chars=ascii_letters,digits,_ length=32') }}"
    lokole_salt: "{{ lookup('password', '/dev/null chars=ascii_letters,digits,_ length=16') }}"

- name: Install {{ lokole_settings }}
  template:
    src: settings.env.j2
    dest: "{{ lokole_settings }}"
    group: "{{ lokole_user }}"
    owner: "{{ lokole_user }}"
    mode: g+rw

#- name: Fixup supervisorctl
#  file:
#    path: /usr/bin/supervisorctl
#    owner: root
#    group: "{{ lokole_user }}"
#    mode: u=rw+s,g=rx,o=rx

- name: Install {{ lokole_confd }} templates to configure Lokole
  template:
    src: "{{ item.src }}"
    dest: "{{ lokole_confd }}"
    group: "{{ lokole_user }}"
    owner: "{{ lokole_user }}"
    mode: 0644
  with_items:
    - { src: 'lokole_gunicorn.conf' }
    - { src: 'lokole_celery_beat.conf' }
    - { src: 'lokole_celery_worker.conf' }
    - { src: 'lokole_restarter.conf' }

- name: Create Lokole admin user with password, for http://box{{ lokole_url }}    # http://box/lokole
  include_tasks: setup.yml


# RECORD Lokole AS INSTALLED

- name: "Set 'lokole_installed: True'"
  set_fact:
    lokole_installed: True

- name: "Add 'lokole_installed: True' to {{ iiab_state_file }}"
  lineinfile:
    path: "{{ iiab_state_file }}"    # /etc/iiab/iiab_state.yml
    regexp: '^lokole_installed'
    line: 'lokole_installed: True'
