# 2019-01-16: @jvonau's PR #1403 moved installation of Node.js [8.x at the time]
# and 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 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"


- name: Record (initial) disk space used
  shell: df -B1 --output=used / | tail -1
  register: df1


# 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

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

# https://nodered.org/docs/user-guide/node-red-admin built in since Node-RED
# 1.1.0 (2020-06-30).  Run it using: node-red admin
# If you install it separately, run: node-red-admin
#
#- name: "Globally 'npm install' 3 Node-RED packages: node-red, node-red-admin, node-red-dashboard"
- name: "Globally 'npm install' 2 Node-RED packages: node-red, 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-dashboard@latest
  # Above "@latest" recommended by https://nodered.org/docs/hardware/raspberrypi back in 2019

# 2022-06-08: New list copied from $EXTRANODES in https://github.com/node-red/linux-installers/blob/master/deb/update-nodejs-and-nodered
- name: "Globally 'npm install' 6 Node-RED learning examples IF Raspberry Pi hardware detected: node-red-node-pi-gpio, node-red-node-random, node-red-node-ping, node-red-contrib-play-audio, node-red-node-smooth, node-red-node-serialport"
  #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
  command: npm install -g --unsafe-perm node-red-node-pi-gpio@latest node-red-node-random@latest node-red-node-ping@latest node-red-contrib-play-audio@latest node-red-node-smooth@latest node-red-node-serialport@latest
  #command: npm i --unsafe-perm --save --no-progress --no-update-notifier --no-audit --no-fund node-red-node-pi-gpio@latest node-red-node-random@latest node-red-node-ping@latest node-red-contrib-play-audio@latest node-red-node-smooth@latest node-red-node-serialport@latest 2>&1
  when: rpi_model != "none"

## 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: settings.yml
  #when: not is_raspbian

- include_tasks: os-integration.yml
#- include_tasks: rpi_desk.yml
#  when: is_raspbian

# 2022-06-08 OFFICIALLY RECOMMENDED /lib/systemd/system/nodered.service IS:
# https://github.com/node-red/linux-installers/blob/master/resources/nodered.service
# EXPLAINED AT https://nodered.org/docs/faq/customising-systemd-on-pi
#
# AS RECOMMENDED BY OFFICIAL INSTALLER SCRIPT:
# https://github.com/node-red/linux-installers/blob/master/deb/update-nodejs-and-nodered
# OFFICIAL INSTRUCTIONS: https://nodered.org/docs/getting-started/local
#                        https://nodered.org/docs/getting-started/raspberrypi
#
# Should /lib be considered instead of /etc ?
# - name: Install /etc/systemd/system/nodered.service systemd unit file from template
#   template:
#     src: nodered.service.j2
#     dest: /etc/systemd/system/nodered.service

- name: Start by downloading https://github.com/node-red/linux-installers/blob/master/resources/nodered.service to /etc/systemd/system/nodered.service
  get_url:
    url: https://raw.githubusercontent.com/node-red/linux-installers/master/resources/nodered.service
    dest: /etc/systemd/system/nodered.service
    timeout: "{{ download_timeout }}"

- name: Replace every '[=/]pi' with '[=/]{{ nodered_linux_user }}' (nodered_linux_user) in /etc/systemd/system/nodered.service
  replace:
    path: /etc/systemd/system/nodered.service
    regexp: '([=/])pi'                       # e.g. nodered_linux_user: nodered
    replace: '\1{{ nodered_linux_user }}'    # \1 is a back-reference to above '=' or '/' -- like sed, but Ansible uses https://docs.python.org/3/library/re.html

# 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: Record (final) disk space used
  shell: df -B1 --output=used / | tail -1
  register: df2

- name: Add 'nodered_disk_usage = {{ df2.stdout|int - df1.stdout|int }}' to {{ iiab_ini_file }}
  ini_file:
    path: "{{ iiab_ini_file }}"    # /etc/iiab/iiab.ini
    section: nodered
    option: nodered_disk_usage
    value: "{{ df2.stdout|int - df1.stdout|int }}"

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