- name: "Install ImageMagick package: imagemagick"
  package:
    name:
      - imagemagick
      - python3-venv
    state: present

- 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 }})"
  file:
    state: directory
    path: "{{ item }}"
    owner: "{{ calibreweb_user }}"    # root
    group: "{{ apache_user }}"        # www-data on debuntu
    #mode: '0755'
  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 }}
  git:
    repo: https://github.com/janeczku/calibre-web.git
    dest: "{{ calibreweb_venv_path }}"    # /usr/local/calibre-web
    force: yes
    depth: 1
    version: "{{ calibreweb_version }}"    # e.g. master, 0.6.5
  when: internet_available

## 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 (using pip) 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 }}
  when: internet_available

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

- name: Install /etc/{{ apache_conf_dir }}/calibre-web.conf from template, for http://box{{ calibreweb_url1 }}, http://box{{ calibreweb_url2 }}, http://box{{ calibreweb_url3 }} via Apache    # http://box/books, http://box/libros, http://box/livres
  template:
    src: calibre-web.conf.j2
    dest: "/etc/{{ apache_conf_dir }}/calibre-web.conf"    # apache2/sites-available on debuntu
  when: apache_installed is defined

- 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
    #mode: '0644'
    backup: yes
  with_items:
    - roles/calibre-web/files/metadata.db
    - roles/calibre-web/files/metadata_db_prefs_backup.json
  when: not metadatadb.stat.exists
  #when: calibreweb_provision

- name: Provision/Copy default admin settings to {{ calibreweb_config }}/app.db IF metadata.db 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
    #mode: '0644'
    backup: yes
  when: not metadatadb.stat.exists
  #when: calibreweb_provision


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