#!/bin/bash
#
# Copyright 2016,2020 JS Foundation and other contributors, https://js.foundation/
# Copyright 2015,2016 IBM Corp.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
echo -e '\033]2;'Node-RED console'\007'
if [[ $(which node-red-pi) ]]; then
    if [[ ! $(which npm) ]]; then
        echo -e "\033[0m                                                  \033[0m"
        echo -e "\033[0m    npm is not installed, it is recommended       \033[0m"
        echo -e "\033[0m    to install the latest by running:             \033[0m"
        echo -e "\033[0m        update-nodejs-and-nodered                 \033[0m"
        echo -e "\033[0m                                                  \033[0m"
        echo -e " "
    fi
    HOSTIP=`hostname -I | cut -d ' ' -f 1`
    if [ "$HOSTIP" = "" ]; then
        HOSTIP="127.0.0.1"
    fi
    echo -e "\033[1mStart \033[38;5;88mNode-RED\033[0m"
    echo " "
    echo "Once Node-RED has started, point a browser at http://$HOSTIP:1880"
    echo "On Pi Node-RED works better with the Firefox or Chrome browser"
    echo " "
    if groups | grep -q -w sudo; then
        echo -e "Use   \033[0;36mnode-red-stop\033[0m                          to stop Node-RED"
        echo -e "Use   \033[0;36mnode-red-start\033[0m                         to start Node-RED again"
        echo -e "Use   \033[0;36mnode-red-log\033[0m                           to view the recent log output"
    fi
    echo -e "Use   \033[0;36msudo systemctl enable nodered.service\033[0m  to autostart Node-RED at every boot"
    echo -e "Use   \033[0;36msudo systemctl disable nodered.service\033[0m to disable autostart on boot"
    echo " "
    echo "To find more nodes and example flows - go to http://flows.nodered.org"
    if [ "$nv" = "v0" ]; then
        echo "You may also need to install and upgrade npm"
        echo -e "      \033[0;36msudo apt-get install npm\033[0m"
        echo -e "      \033[0;36msudo npm i -g npm@2.x\033[0m"
    fi
    if groups $USER | grep -q -w sudo; then
        # Current user is member of sudo group, start node-red through systemd
        sudo systemctl start nodered
        echo " "
        echo "Starting as a systemd service."
        sudo journalctl -f -n 0 -u nodered -o cat
    elif [ $EUID -eq 0 ]; then
        # Current user is root, start node-red through systemd
        systemctl start nodered
        echo " "
        echo "Starting as root systemd service."
        journalctl -f -n 0 -u nodered -o cat
    else
        # Start node-red as unprivileged user
        echo " "
        echo "Starting as a normal user."
        node-red-pi
    fi
else
    echo "Node-RED is not yet fully installed. Please re-run the install script again manually."
    echo " "
    echo "   bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered)"
    echo " "
    echo "then retry  node-red-start"
    echo " "
fi