# We need a recent version of node

- name: FAIL (STOP INSTALLING) IF nodejs_version is not set to 10.x
  fail:
    msg: "Internet Archive install cannot proceeed, as it currently requires Node.js 10.x, and your nodejs_version is set to {{ nodejs_version }}.  Please check the value of nodejs_version in /opt/iiab/iiab/vars/default_vars.yml and possibly also /etc/iiab/local_vars.yml"
  when: internetarchive_install and (nodejs_version != "10.x")

- name: Install packages needed by Distributed Web
  package:
    name:
      - libsecret-1-dev
      - cmake
    state: present

- name: Create directory {{ internetarchive_dir }}
  file:
    path: "{{ internetarchive_dir }}"
    state: directory
    owner: "root"

- name: Run yarn install to get needed modules (CAN TAKE ~5 MINUTES)
  command: sudo yarn add @internetarchive/dweb-archive @internetarchive/dweb-mirror
  args:
    chdir: "{{ internetarchive_dir }}"
  when: internet_available | bool

- name: Create directory /library/archiveorg
  file:
    path: "/library/archiveorg"
    state: directory
    owner: "root"


# CONFIG FILES

- name: "Install from templates: internetarchive.service (systemd), internetarchive.conf (Apache)"
  template:
    src: "{{ item.src }}"
    dest: "{{ item.dest }}"
    mode: 0644
    owner: root
    group: root
  with_items:
    - { src: 'internetarchive.service.j2', dest: '/etc/systemd/system/internetarchive.service' }
    - { src: 'internetarchive.conf', dest: '/etc/apache2/sites-available/internetarchive.conf' }

- name: Create symlink internetarchive.conf from sites-enabled to sites-available, for short URL http://box/archive (if debuntu and internetarchive_enabled)
  file:
    src: /etc/apache2/sites-available/internetarchive.conf
    path: /etc/apache2/sites-enabled/internetarchive.conf
    state: link
  when: is_debuntu and internetarchive_enabled

- name: Remove symlink /etc/apache2/sites-enabled/internetarchive.conf (if debuntu and not internetarchive_enabled)
  file:
    path: /etc/apache2/sites-enabled/internetarchive.conf
    state: absent
  when: is_debuntu and not internetarchive_enabled


# RESTART/STOP SYSTEMD SERVICE

# with "systemctl daemon-reload" in case mongodb.service changed, etc
- name: Enable & Restart 'internetarchive' systemd service (if internetarchive_enabled)
  systemd:
    name: internetarchive
    daemon_reload: yes
    enabled: yes
    state: restarted
  when: internetarchive_enabled | bool

- name: Disable & Stop 'internetarchive' systemd service (if not internetarchive_enabled)
  systemd:
    name: internetarchive
    daemon_reload: yes
    enabled: no
    state: stopped
  when: not internetarchive_enabled

- name: Restart Apache service ({{ apache_service }}) to enable/disable http://box/archive (not just http://box:{{ internetarchive_port }})
  systemd:
    name: "{{ apache_service }}"    # httpd or apache2
    state: restarted
  when: internetarchive_enabled | bool


- name: Add 'internetarchive' variable values to {{ iiab_ini_file }}
  ini_file:
    path: "{{ iiab_ini_file }}"
    section: internetarchive
    option: "{{ item.option }}"
    value: "{{ item.value }}"
  with_items:
    - option: name
      value: Internet Archive Distributed Web
    - option: description
      value: '"Dweb-mirror is intended to make the Internet Archive experience and UI available offline."'
    - option: internetarchive_enabled
      value: "{{ internetarchive_enabled }}"
