- name: "Set 'apache_install: True' and 'apache_enabled: True'"
  set_fact:
    apache_install: True
    apache_enabled: True

- name: APACHE - run 'httpd' role
  include_role:
    name: httpd


# 2019-01-16: @jvonau's PR #1403 moved installation of Node.js (8.x for now) &
# npm to roles/nodejs/tasks/main.yml

- name: "Set 'nodejs_install: True' and 'nodejs_enabled: True'"
  set_fact:
    nodejs_install: True
    nodejs_enabled: True

# 2020-01-04 no longer triggered by roles/nodered/meta/main.yml
- name: NODEJS - run 'nodejs' role (attempt to install & enable Node.js)
  include_role:
    name: nodejs

- name: FAIL (STOP THE INSTALL) IF 'nodejs_installed is undefined'
  fail:
    msg: "Node-RED install cannot proceed, as Node.js is not installed."
  when: nodejs_installed is undefined

# 2020-10-29: not really be nec as Node-RED supports recent Node.js versions
#- name: FAIL (STOP THE INSTALL) IF 'nodejs_version != "12.x"'
#  fail:
#    msg: "Node-RED install cannot proceed, as it currently requires Node.js 12.x, whereas nodejs_version is set to {{ nodejs_version }}.  Please check the value of nodejs_version in /opt/iiab/iiab/vars/default_vars.yml, /etc/iiab/local_vars.yml, /opt/iiab/iiab/roles/nodejs, etc."
#  when: nodejs_version != "12.x"


# BRUTAL but ensures consistency across OS's / distros like Raspbian Desktop &
# Ubermix that often include an older version of Node-RED.  Brutal, as this
# removes customizations on graphical desktop OS's e.g. Raspbian Desktop's:
#   1. Node-RED's icon (Raspberry Menu in top-left -> Programming -> Node-RED)
#   2. scripts like {node-red-start, node-red-stop, node-red-log} in /usr/bin
#   3. other changes per /usr/bin/update-nodejs-and-nodered summarized at
#      https://nodered.org/docs/hardware/raspberrypi for example low-memory
#      flag --max_old_space_size=256 for unit file (we're using 128 on RPi)
# That we'll reconstitute below!
- name: ASK apt/yum/dnf TO REMOVE PRE-EXISTING 'nodered' (IF PREVIOUSLY INSTALLED BY OS PKG MANAGER)
  package:
    name: nodered
    state: absent
  when: nodered_install

# 2012-02-13: the 6 RPi stanzas below recreate Raspbian Desktop's Node-RED
# environment, inspired by:
# https://nodered.org/docs/hardware/raspberrypi
# https://github.com/node-red/raspbian-deb-package/blob/master/resources/update-nodejs-and-nodered
# https://github.com/iiab/iiab/pull/1497

- name: "Globally 'npm install' 3 Node-RED packages: node-red, node-red-admin, node-red-dashboard"
  command: npm install -g --unsafe-perm node-red node-red-admin node-red-dashboard
  #command: npm install -g --unsafe-perm node-red@latest node-red-admin@latest node-red-dashboard@latest
  # Above "@latest" is recommended by https://nodered.org/docs/hardware/raspberrypi (SHOULD WE CONSIDER?)
  when: nodered_install and internet_available

- name: "Globally 'npm install' 8 Node-RED learning examples for RPi: node-red-contrib-ibm-watson-iot, node-red-contrib-play-audio, node-red-node-ledborg, node-red-node-ping, node-red-node-pi-sense-hat, node-red-node-random, node-red-node-serialport, node-red-node-smooth"
  command: npm install -g --unsafe-perm node-red-contrib-ibm-watson-iot node-red-contrib-play-audio node-red-node-ledborg node-red-node-ping node-red-node-pi-sense-hat node-red-node-random node-red-node-serialport node-red-node-smooth
  when: nodered_install and internet_available and is_raspbian

## To protect pre-installed packages within /usr/lib/node_modules in graphical
## desktop OS's like Raspbian Desktop & Ubermix, we now only install those that
## are missing -- among the 3+8 below.  WARNING: THIS COULD POTENTIALLY LEAD TO
## INCOMPATIBILITIES, IF OS'S /usr/lib/node_modules/node-red GETS OUT OF DATE!
#
## /usr/lib/node_modules/node-red is PRE-INSTALLED by Raspbian Desktop, even if
## their package (42MB, 0.19.4) is a bit out of date compared to npm's (55MB,
## 0.19.5) as of 2019-02-12.  Among others in /usr/lib/node_modules, pre-placed
## by Raspbian Desktop's apt package 'nodered':
##   node-red-contrib-ibm-watson-iot, node-red-contrib-play-audio,
##   node-red-node-ledborg, node-red-node-ping, node-red-node-pi-sense-hat
##   node-red-node-random, node-red-node-serialport, node-red-node-smooth
#- name: Globally 'npm install' pkg 'node-red' if /usr/lib/node_modules/node-red missing (most OS's except for Raspbian Desktop)
#  #command: npm install -g --unsafe-perm node-red
#  command: npm install -g --unsafe-perm node-red@latest
#  args:
#    creates: /usr/lib/node_modules/node-red
#  when: nodered_install and internet_available
#
## NOT pre-installed by Raspbian Desktop as of 2019-02-12...so we install this
## on most all OS's:
#- name: Globally 'npm install' pkg 'node-red-admin' if /usr/lib/node_modules/node-red-admin missing (most OS's)
#  command: npm install -g --unsafe-perm node-red-admin
#  args:
#    creates: /usr/lib/node_modules/node-red-admin
#  when: nodered_install and internet_available
#
## NOT pre-installed by Raspbian Desktop as of 2019-02-12...so we install this
## on most all OS's:
#- name: Globally 'npm install' pkg 'node-red-dashboard' if /usr/lib/node_modules/node-red-dashboard missing (most OS's)
#  command: npm install -g --unsafe-perm node-red-dashboard
#  args:
#    creates: /usr/lib/node_modules/node-red-dashboard
#  when: nodered_install and internet_available

- include_tasks: group.yml
  when: nodered_install and not is_raspbian

- include_tasks: rpi_desk.yml
  when: nodered_install and internet_available and is_raspbian

- name: Install /etc/systemd/system/nodered.service systemd unit file from template
  template:
    backup: no
    src: nodered.service.j2
    dest: /etc/systemd/system/nodered.service
    # mode: '0666'

- name: Install /etc/{{ apache_conf_dir }}/nodered.conf from template
  template:
    backup: yes
    src: nodered.conf.j2
    dest: "/etc/{{ apache_conf_dir }}/nodered.conf"
    # mode: '0666'

# SEE ALSO THE apache2_module SECTION IN roles/httpd/tasks/main.yml
- name: Enable proxy_wstunnel apache2 module
  apache2_module:
    state: present
    name: proxy_wstunnel


# RECORD Node-RED AS INSTALLED

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

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