#!/bin/bash
#
# Copyright 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 " "
echo "This script will remove the pre-installed versions of node.js and Node-RED"
echo "and replace them with node.js 4.x LTS (argon) and the latest Node-RED from Npm."
echo "To do this it runs commands as root - please satisfy yourself that this will"
echo "not damage your Pi, or otherwise compromise your configuration."
echo " "
echo "Doing this may also be 'a bad thing' if you have installed lots of extra nodes."
echo "Especially if they have any native binary component. Some nodes in your ~/.node-red"
echo " directory will probably need to be re-installed afterwards, some may need you to"
echo "run npm update, and some may require you to run npm rebuild."
echo " "
echo "There may be a period of frustration ahead to get back to where you were..."
echo " "
read -p "Are you really, really sure you want to do this ? " yn
case $yn in
    [Yy]* )
        cd $HOME
        node-red-stop
        sudo apt-get remove -y nodered nodejs nodejs-legacy npm
        sudo dpkg -r nodejs
        sudo dpkg -r node
        sudo apt-get autoremove -y
        sudo rm -rf /usr/local/lib/node_modules/node-* /usr/local/bin/node-* /usr/local/bin/npm*
        #sudo rm -rf /usr/lib/node_modules/node-* /usr/bin/node-* /usr/bin/npm*
        # grab the correct LTS bundle for the processor
        echo " "
        if $(cat /proc/cpuinfo | grep model | grep -q ARMv6) ; then
            echo "Installing node for Armv6"
            f=$(wget https://nodejs.org/download/release/latest-argon/ -qO- | grep "armv6l.tar.gz" | cut -d '"' -f 2)
            wget https://nodejs.org/download/release/latest-argon/$f -O node.tgz
            # unpack it into the correct places
            sudo tar -zxf node.tgz --strip-components=1 -C /usr
            # remove the tgz file to save space
            rm node.tgz
        else
            echo "Installing node for Armv7"
            # use the official script to install for Arm7
            curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
            sudo apt-get install -y nodejs
        fi
        hash -r
        echo "Installed"
        echo "   Node" $(node -v)
        echo "   Npm   "$(npm -v)
        # and install Node-RED
        echo "Now installing Node-RED - please wait - can take 20 mins on a Pi 1"
        echo " "
        sudo npm cache clean
        sudo rm -rf /home/pi/.node-gyp /home/pi/.npm
        sudo npm install -g --unsafe-perm node-red node-red-node-random node-red-node-ping node-red-node-smooth node-red-node-ledborg node-red-contrib-ibm-watson-iot node-red-node-pi-sense-hat
        # try to rebuild any already installed nodes
        if [ -d "/home/pi/.node-red" ]
        then
            pushd /home/pi/.node-red
            npm rebuild
            popd
        fi
        # Now add the shortcut to the menu
        sudo wget https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/node-red-icon.svg -O /usr/share/icons/gnome/scalable/apps/node-red-icon.svg
        sudo wget https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/Node-RED.desktop -O /usr/share/applications/Node-RED.desktop
        lxpanelctl restart
        # and add the start and stop scripts to systemd
        sudo wget https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/nodered.service -O /lib/systemd/system/nodered.service
        sudo wget https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/node-red-start2 -O /usr/bin/node-red-start
        sudo wget https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/node-red-stop -O /usr/bin/node-red-stop
        sudo wget https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/node-red-log -O /usr/bin/node-red-log
        sudo wget https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/update-nodejs-and-nodered -O /usr/bin/update-nodejs-and-nodered
        sudo chmod +x /usr/bin/node-red-st*
        sudo chmod +x /usr/bin/node-red-log
        sudo chmod +x /usr/bin/update-nodejs-and-nodered
        sudo systemctl daemon-reload
        sudo setcap cap_net_raw+eip $(eval readlink -f `which node`)
        sudo setcap cap_net_raw=ep /bin/ping
        sudo setcap cap_net_raw=ep /bin/ping6
        echo " "
        echo "All done."
        echo -e "  You can now start Node-RED with the command  \033[0;36mnode-red-start\033[0m"
        echo -e "  or using the icon under   Menu / Programming / Node-RED"
        echo -e "  Then point your browser to \033[0;36mlocalhost:1880\033[0m or \033[0;36mhttp://{{your_pi_ip-address}:1880\033[0m"
        echo " "
    ;;
        * ) exit
    ;;
esac
