- name: Download {{ kalite_requirements }} to {{ pip_packages_dir }}/kalite.txt
  get_url:
    url: "{{ kalite_requirements }}"
    dest: "{{ pip_packages_dir }}/kalite.txt"    # /opt/iiab/pip-packages/kalite.txt
    timeout: "{{ download_timeout }}"
  when: internet_available

# 2020-01-19: https://github.com/piwheels/packages/issues/74 says the following is no longer needed...
#- name: Run 'mv /etc/pip.conf /etc/pip.conf.see-iiab-issue-2139' as "TEMPORARY" workaround (2020-01-17) for piwheels.org's setuptools Python 2/3 brokenness on RPi (https://github.com/iiab/iiab/issues/2139)
#  command: mv /etc/pip.conf /etc/pip.conf.see-iiab-issue-2139
#  ignore_errors: yes
#  when: is_raspbian

- name: Install python2, if Raspbian/Debian > 10 or Ubuntu > 19
  package:
    name:
      - python2
      - python-setuptools # provides setuptools-44 last version compatible with python2
    state: present
  when: not (is_debian_9 or is_debian_10 or is_ubuntu_16 or is_ubuntu_17 or is_ubuntu_18 or is_ubuntu_19)
  # 2020-03-31: Testing for {is_raspbian_9, is_raspbian_10} is not currently nec, as testing for {is_debian_9, is_debian_10} covers that already.

- name: Use pip to pin setuptools to 44 in {{ kalite_venv }} if Raspbian/Debian > 10 or Ubuntu > 19
  pip:
    name: setuptools==44
    virtualenv: "{{ kalite_venv }}"    # /usr/local/kalite/venv
    virtualenv_site_packages: no
    virtualenv_command: /usr/bin/virtualenv
    virtualenv_python: python2.7
    extra_args: "--no-use-pep517 --no-cache-dir --no-python-version-warning"
  when: not (is_debian_9 or is_debian_10 or is_ubuntu_16 or is_ubuntu_17 or is_ubuntu_18 or is_ubuntu_19)
  # long form of (is_debian_11+ or is_ubuntu_20+)

- name: Use pip to install ka-lite-static to {{ kalite_venv }}
  pip:
    name: ka-lite-static
    version: "{{ kalite_version }}"
    virtualenv: "{{ kalite_venv }}"    # /usr/local/kalite/venv
    virtualenv_site_packages: no
    virtualenv_command: /usr/bin/virtualenv
    virtualenv_python: python2.7
    extra_args: "--no-cache-dir"
  when: internet_available

- name: "Install from templates: venv wrapper /usr/bin/kalite, unit file /etc/systemd/system/kalite-serve.service"
  template:
    src: "{{ item.src }}"
    dest: "{{ item.dest }}"
    mode: "{{ item.mode }}"
  with_items:
    - { src: 'kalite.sh.j2', dest: '/usr/bin/kalite', mode: '0755' }
    - { src: 'kalite-serve.service.j2', dest: '/etc/systemd/system/kalite-serve.service', mode: '0644' }

# Useless stanza, for 2 reasons: (1) http://box/kalite was never made to work
# (2) /etc/apache2/sites-available does not exist on many IIAB's w/o Apache
- name: "Install from template: /etc/{{ apache_conf_dir }}/kalite.conf (useless, as http://box/kalite was never made to work)"
  template:
    src: kalite.conf
    dest: "/etc/{{ apache_conf_dir }}"    # apache2/sites-available on debuntu
  when: apache_installed is defined

- name: Fix KA Lite bug in regex parsing ifconfig output (ifcfg/parser.py) for @m-anish's network names that contain dashes, if Raspbian/Debian < 11 or Ubuntu < 20
  replace:
    path: /usr/local/kalite/venv/local/lib/python2.7/site-packages/kalite/packages/dist/ifcfg/parser.py
    regexp: 'a-zA-Z0-9'
    replace: 'a-zA-Z0-9\-'
  when: is_debian_9 or is_debian_10 or is_ubuntu_16 or is_ubuntu_17 or is_ubuntu_18 or is_ubuntu_19
  # 2020-03-31: Testing for {is_raspbian_9, is_raspbian_10} is not currently nec, as testing for {is_debian_9, is_debian_10} covers that already.

- name: Fix KA Lite bug in regex parsing ifconfig output (ifcfg/parser.py) for @m-anish's network names that contain dashes, if Raspbian/Debian > 10 or Ubuntu > 19
  replace:
    path: /usr/local/kalite/venv/lib/python2.7/site-packages/kalite/packages/dist/ifcfg/parser.py
    regexp: 'a-zA-Z0-9'
    replace: 'a-zA-Z0-9\-'
  when: not (is_debian_9 or is_debian_10 or is_ubuntu_16 or is_ubuntu_17 or is_ubuntu_18 or is_ubuntu_19)
  # 2020-03-31: Testing for {is_raspbian_9, is_raspbian_10} is not currently nec, as testing for {is_debian_9, is_debian_10} covers that already.
  # JV: why not just is_ubuntu_20? AH: to make this work on Ubuntu 21+ and ideally Debian/RaspiOS 11+ too?

- name: Create dir {{ kalite_root }}
  file:
    state: directory
    path: "{{ kalite_root }}/httpsrv/static"    # /library/ka-lite

- name: Run '{{ kalite_program }} manage setup ...'
  command: "{{ kalite_program }} manage setup --username={{ kalite_admin_user }} --password={{ kalite_admin_password }} --noinput"    # Runs /usr/local/kalite/venv/bin/kalite
  environment:
    KALITE_HOME: "{{ kalite_root }}"    # /library/ka-lite
  async: 1800
  poll: 10


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

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