- name: Create 3 Calibre-Web folders to store data and configuration files
  file:
    path: "{{ item }}"
    owner: "{{ calibreweb_user }}"
    group: "{{ apache_user }}"
    mode: 0755
    state: directory
  with_items:
    - "{{ calibreweb_home }}"
    - "{{ calibreweb_venv_path }}"
    - "{{ calibreweb_config }}"

## TODO: Calibre-web future release might get into pypi https://github.com/janeczku/calibre-web/issues/456
- name: Download Calibre-Web github repository to {{ calibreweb_venv_path }}
  git:
    repo: https://github.com/janeczku/calibre-web.git
    dest: "{{ calibreweb_venv_path }}"
    force: yes
    #update: yes
    depth: 1
    version: master
  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 virtual environment
  pip:
    requirements: "{{ calibreweb_venv_path }}/requirements.txt"
    virtualenv: "{{ calibreweb_venv_path }}"
    virtualenv_site_packages: no
  when: internet_available

- name: Symlink {{ calibreweb_venv_path }}/vendor to {{ calibreweb_venv_path }}/lib/python2.7/site-packages to keep cps.py happy
  file:
    src: "{{ calibreweb_venv_path }}/lib/python2.7/site-packages"
    dest: "{{ calibreweb_venv_path }}/vendor"
    state: link

- name: Install unit file /etc/systemd/system/calibre-web.service & /etc/apache2/sites-available/calibre-web.conf for http://box{{ calibreweb_url }}, from templates
  template:
    src: "{{ item.src }}"
    dest: "{{ item.dest }}"
    owner: root
    group: root
    mode: 0644
  with_items:
    - { src: 'calibre-web.service.j2', dest: '/etc/systemd/system/calibre-web.service' }
    - { src: 'calibre-web.conf.j2', dest: '/etc/apache2/sites-available/calibre-web.conf' }

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

- name: Provision/Copy both default metadata files into /library/calibre-web IF metadata.db did not exist
  copy:
    src: "{{ item }}"
    dest: "{{ calibreweb_home }}"
    owner: "{{ calibreweb_user }}"
    group: "{{ apache_user }}"
    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    # {{ calibreweb_config }} is /library/calibre-web/config
  copy:
    src: roles/calibre-web/files/app.db
    dest: "{{ calibreweb_config }}"
    owner: "{{ calibreweb_user }}"
    group: "{{ apache_user }}"
    mode: 0644
    backup: yes
  when: not metadatadb.stat.exists
  #when: calibreweb_provision

- name: Enable & Restart 'calibre-web' systemd service
  systemd:
    name: calibre-web
    daemon_reload: yes
    enabled: yes
    state: restarted
  when: calibreweb_enabled

# Default: http://box/books
- name: Enable http://box{{ calibreweb_url }} with Apache
  command: a2ensite calibre-web.conf
  when: calibreweb_enabled

#- name: Restart Apache after enabling calibre-web httpd2 site
#  command: apachectl -k graceful
#  when: calibreweb_enabled

- name: Disable 'calibre-web' systemd service
  systemd:
    name: calibre-web
    daemon_reload: yes
    enabled: no
    state: stopped
  when: not calibreweb_enabled

- name: Disable http://box{{ calibreweb_url }} with Apache
  command: a2dissite calibre-web.conf
  when: not calibreweb_enabled

#- name: Restart Apache after disabling calibre-web httpd2 site
#  command: apachectl -k graceful
#  when: not calibreweb_enabled

- name: Restart Apache systemd service ({{ apache_service }})
  systemd:
    name: "{{ apache_service }}"    # httpd or apache2
    state: restarted

- name: Add 'calibre-web' variable values to {{ iiab_ini_file }}
  ini_file:
    path: "{{ iiab_ini_file }}"
    section: calibre-web
    option: "{{ item.option }}"
    value: "{{ item.value }}"
  with_items:
    - option: name
      value: calibre-web
    - option: description
      value: '"calibre-web is a web app providing a clean interface for browsing, reading and downloading e-books."'
    - option: calibreweb_url
      value: "{{ calibreweb_url }}"
    - option: calibreweb_path
      value: "{{ calibreweb_venv_path }}"
    - option: calibreweb_home
      value: "{{ calibreweb_home }}"
    - option: calibreweb_port
      value: "{{ calibreweb_port }}"
    - option: calibreweb_database
      value: "{{ calibreweb_database }}"
    - option: calibreweb_enabled
      value: "{{ calibreweb_enabled }}"
#   - option: calibreweb_provision
#     value: "{{ calibreweb_provision }}"
