# 1. CREATE/VERIFY CRITICAL DIRECTORIES & FILES ARE IN PLACE

- name: Create directory {{ iiab_zim_path }} and subdirs {content, index} for Kiwix ZIM files
  file:
    path: "{{ item }}"
    owner: root
    group: root
    mode: 0755
    state: directory
  with_items:
  - "{{ iiab_zim_path }}"
  - "{{ iiab_zim_path }}/content"
  - "{{ iiab_zim_path }}/index"

- name: Check for {{ kiwix_library_xml }}    # /library/zims/library.xml
  stat:
    path: "{{ kiwix_library_xml }}"
  register: kiwix_xml

- name: Install a stub /library/zims/library.xml if one doesn't exist
  template:
    src: "{{ item }}"
    dest: "{{ kiwix_library_xml }}"
    mode: 0644
    owner: root
    group: root
    force: no
  with_items:
  - library.xml
  when: not kiwix_xml.stat.exists

- name: Check for /opt/iiab/kiwix/bin/kiwix-serve binary
  stat:
    path: "{{ kiwix_path }}/bin/kiwix-serve"
  register: kiwix_bin

- name: Set fact kiwix_force_install if kiwix-serve not found
  set_fact:
    kiwix_force_install: True
  when: not kiwix_bin.stat.exists

- name: Install {{ iiab_zim_path }}/content/test.zim if kiwix_force_install
  copy:
    src: test.zim
    dest: "{{ iiab_zim_path }}/content/test.zim"
    mode: 0644
    owner: root
    group: root
    force: no
  when: kiwix_force_install

- name: Create {{ kiwix_path }}/bin directory    # /opt/iiab/kiwix/bin
  file:
    path: "{{ kiwix_path }}/bin"
    owner: root
    group: root
    mode: 0755
    state: directory

# 2. INSTALL KIWIX-TOOLS EXECUTABLES IF kiwix_force_install

- name: Unarchive {{ kiwix_src_file }} to /tmp    # e.g. kiwix-tools_linux-armhf-0.6.1-1.tar.gz
  unarchive:
    src: "{{ downloads_dir }}/{{ kiwix_src_file }}"
    dest: /tmp
    owner: root
    group: root
  when: kiwix_force_install

- name: Move /tmp/{{ kiwix_src_dir }}/* to permanent location /opt/iiab/kiwix/bin (armhf & linux64 & i686)
  shell: "mv /tmp/{{ kiwix_src_dir }}/* {{ kiwix_path }}/bin/"
  when: kiwix_force_install

# 3. ENABLE MODS FOR APACHE PROXY IF DEBUNTU

- name: Enable the 4 mods which permit Apache to proxy (debuntu)
  apache2_module:
    name: "{{ item }}"
  with_items:
  - proxy
  - proxy_html
  - proxy_http
  - rewrite
  when: is_debuntu

# 4. CREATE/ENABLE/RESTART (OR DISABLE) KIWIX SERVICE & ITS CRON JOB

- name: 'Install from templates: kiwix-serve.service, iiab-make-kiwix-lib, iiab-make-kiwix-lib.py, kiwix.conf'
  template:
    backup: no
    src: "{{ item.src }}"
    dest: "{{ item.dest }}"
    owner: root
    group: root
    mode: "{{ item.mode }}"
  with_items:
  - { src: 'kiwix-serve.service.j2', dest: '/etc/systemd/system/kiwix-serve.service', mode: '0644'}
#   - { src: 'kiwix-serve-init.j2', dest: '/usr/libexec/kiwix-serve-init', mode: '0755'}
  - { src: 'iiab-make-kiwix-lib', dest: '/usr/bin/iiab-make-kiwix-lib', mode: '0755'}
  - { src: 'iiab-make-kiwix-lib.py', dest: '/usr/bin/iiab-make-kiwix-lib.py', mode: '0755'}
#  - { src: 'iiab-make-apache-config.py', dest: '/usr/bin/iiab-make-apache-config.py', mode: '0755'}
  - { src: 'kiwix.conf.j2', dest: '/etc/{{ apache_config_dir }}/kiwix.conf', mode: '0644'}

- name: Create softlink kiwix.conf from sites-enabled to sites-available - for Kiwix Proxy in Apache - is disabled by turning off service kiwix-serve (debuntu)
  file:
    src: /etc/apache2/sites-available/kiwix.conf
    path: /etc/apache2/sites-enabled/kiwix.conf
    state: link
  when: is_debuntu

- name: Enable & Restart 'kiwix-serve' service
  service:
    name: kiwix-serve
    enabled: yes
    state: restarted
  when: kiwix_enabled

- name: Disable 'kiwix-serve' service
  service:
    name: kiwix-serve
    enabled: no
    state: stopped
  when: not kiwix_enabled
# IN THEORY: BOTH CRON ENTRIES BELOW *SHOULD* BE DELETED "when: not kiwix_enabled"

# In the past kiwix-serve did not stay running, so we'd been doing this hourly.
# @mgautierfr & others suggest kiwix-serve might be auto-restarted w/o cron in
# future, whenever service fails, if this really catches all cases??
# https://github.com/iiab/iiab/issues/484#issuecomment-342151726
- name: Make a crontab entry to restart kiwix-serve at 4AM (debuntu)
  lineinfile:
         # mn hr dy mo day-of-week[Sunday=0] username command-to-be-executed
    line: "0  4  *  *  * root /bin/systemctl restart kiwix-serve.service"
    dest: /etc/crontab
  when: kiwix_enabled and is_debuntu

- name: Make a crontab entry to restart kiwix-serve at 4AM (redhat)
# *  *  *  *  * user-name  command to be executed
  lineinfile:
         # mn hr dy mo day-of-week[Sunday=0] username command-to-be-executed
    line: "0  4  *  *  * root /usr/bin/systemctl restart kiwix-serve.service"
    dest: /etc/crontab
  when: kiwix_enabled and is_redhat

- name: Restart Apache, so it picks up kiwix.conf
  service:
    name: "{{ apache_service }}"
    state: restarted

# 5. FINALIZE

- name: Add 'kiwix' variable values to {{ iiab_ini_file }}
  ini_file:
    path: "{{ iiab_ini_file }}"
    section: kiwix
    option: "{{ item.option }}"
    value: "{{ item.value }}"
  with_items:
  - option: name
    value: Kiwix
  - option: description
    value: '"Part of https://github.com/kiwix/kiwix-tools/ - kiwix-serve is the most used web server for ZIM files."'
  - option: kiwix_url
    value: "{{ kiwix_url }}"
  - option: kiwix_path
    value: "{{ kiwix_path }}"
  - option: kiwix_port
    value: "{{ kiwix_port }}"
  - option: iiab_zim_path
    value: "{{ iiab_zim_path }}"
  - option: kiwix_library_xml
    value: "{{ kiwix_library_xml }}"
  - option: enabled
    value: "{{ kiwix_enabled }}"
