- name: "Install packages: imagemagick, python3-venv"
  package:
    name:
      - imagemagick
      - python3-venv
    state: present

# https://github.com/iiab/iiab/pull/3496#issuecomment-1475094542
- name: "Install packages: python3-dev, gcc to compile 'netifaces'"
  package:
    name:
      - python3-dev # header files
      - gcc         # compiler
    state: present
  when: python_version is version('3.10', '>=')

- name: Allow ImageMagick to read PDFs, per /etc/ImageMagick-6/policy.xml, to create book cover thumbnails
  lineinfile:
    path: /etc/ImageMagick-6/policy.xml
    regexp: '<policy domain="coder" rights="none" pattern="PDF" />'
    backrefs: yes
    line: '  <policy domain="coder" rights="read" pattern="PDF" />'
    state: present

- name: "Create 3 Calibre-Web folders to store data and config files: {{ calibreweb_home }}, {{ calibreweb_venv_path }}, {{ calibreweb_config }} (all set to {{ calibreweb_user }}:{{ apache_user }}) (default to 0755)"
  file:
    state: directory
    path: "{{ item }}"
    owner: "{{ calibreweb_user }}"    # root
    group: "{{ apache_user }}"        # www-data on debuntu
  with_items:
    - "{{ calibreweb_home }}"         # /library/calibre-web
    - "{{ calibreweb_config }}"       # /library/calibre-web/config
    - "{{ calibreweb_venv_path }}"    # /usr/local/calibre-web-py3

## TODO: Calibre-web future release might get into pypi https://github.com/janeczku/calibre-web/issues/456
- name: Clone i.e. download Calibre-Web ({{ calibreweb_version }}) from https://github.com/janeczku/calibre-web.git to {{ calibreweb_venv_path }} (~94 MB initially, ~115+ MB later)
  git:
    repo: https://github.com/janeczku/calibre-web.git
    dest: "{{ calibreweb_venv_path }}"
    force: yes
    depth: 1
    version: "{{ calibreweb_version }}"    # e.g. master, 0.6.17

## Ansible Pip Bug: Cannot use 'chdir' with 'env' https://github.com/ansible/ansible/issues/37912 (Patch landed)
#- name: Download calibre-web dependencies into vendor subdirectory.
#  pip:
#    requirements: "{{ calibreweb_path }}/requirements.txt"
#    chdir: "{{ calibreweb_path }}"
#    extra_args: '--target vendor'
#  ignore_errors: True
##
# Implementing this with Ansible command module for now.
- name: Download Calibre-Web dependencies from 'requirements.txt' into python3 virtual environment {{ calibreweb_venv_path }}
  pip:
    requirements: "{{ calibreweb_venv_path }}/requirements.txt"
    virtualenv: "{{ calibreweb_venv_path }}"    # /usr/local/calibre-web-py3
    virtualenv_site_packages: no
    virtualenv_command: python3 -m venv {{ calibreweb_venv_path }}
# VIRTUALENV EXAMPLE COMMANDS:
# cd /usr/local/calibre-web-py3
# source bin/activate
# python3 -m pip list    ('pip list' probably sufficient, likewise below)
# python3 -m pip freeze > /tmp/requirements.txt
# python3 -m pip install -r requirements.txt
# deactivate
# https://pip.pypa.io/en/latest/user_guide/#requirements-files

- name: Install /etc/systemd/system/calibre-web.service from template
  template:
    src: calibre-web.service.j2
    dest: /etc/systemd/system/calibre-web.service

- name: Does /library/calibre-web/metadata.db exist?
  stat:
    path: /library/calibre-web/metadata.db
  register: metadatadb

- name: Provision/Copy both default metadata files (metadata.db, metadata_db_prefs_backup.json) into {{ calibreweb_home }} IF metadata.db did not exist
  copy:
    src: "{{ item }}"
    dest: "{{ calibreweb_home }}"     # /library/calibre-web
    owner: "{{ calibreweb_user }}"    # root
    group: "{{ apache_user }}"        # www-data on debuntu
    backup: yes
  with_items:
    - roles/calibre-web/files/metadata.db
    - roles/calibre-web/files/metadata_db_prefs_backup.json
  when: not metadatadb.stat.exists

- name: Does /library/calibre-web/config/app.db exist?
  stat:
    path: /library/calibre-web/config/app.db
  register: appdb

- name: Provision/Copy default admin settings to {{ calibreweb_config }}/app.db IF it did not exist
  copy:
    src: roles/calibre-web/files/app.db
    dest: "{{ calibreweb_config }}"    # /library/calibre-web/config
    owner: "{{ calibreweb_user }}"     # root
    group: "{{ apache_user }}"         # www-data on debuntu
    backup: yes
  when: not appdb.stat.exists

# https://github.com/iiab/iiab/pull/3496#issuecomment-1475094542
#- name: "Uninstall packages: python3-dev, gcc used to compile 'netifaces'"
#  package:
#    name:
#      - python3-dev # header files
#      - gcc         # compiler
#    state: absent
#  when: python_version is version('3.10', '>=')


# RECORD Calibre-Web AS INSTALLED

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

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