#!/usr/bin/env bash
# install yunkong2 on Raspbian 8/Debian Jessie with SystemD
# Copyright (c) 2017, keynight yunkong2.net/forum

# install tools for nodels install
sudo apt-get install -y apt-transport-https curl build-essential

# install NodeJS for non Raspbian 8/Debian Jessie
#curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -

# install NodeJS 8.x
#curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -


# add repos for node_8.x for Raspbian 8/Debian Jessie
echo "###############  add repos for node_8.x for Raspbian 8/Debian Jessie ###############"
curl --silent https://deb.nodesource.com/gpgkey/nodesource.gpg.key | sudo apt-key add -

sudo cat <<- EOF > /etc/apt/sources.list.d/nodesource.list
deb https://deb.nodesource.com/node_8.x jessie main
deb-src https://deb.nodesource.com/node_8.x jessie main

EOF



# add start script /etc/init.d/yunkong2
echo "############### add start script /etc/init.d/yunkong2  ###############"

sudo cat <<- EOF > /etc/init.d/yunkong2
#!/bin/bash

### BEGIN INIT INFO
# Provides:          yunkong2
# Required-Start:    \$network \$local_fs \$remote_fs
# Required-Stop::    \$network \$local_fs \$remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts yunkong2
# Description:       starts yunkong2
### END INIT INFO

source /etc/profile
source /etc/skel/.bashrc
source /etc/skel/.profile

(( EUID )) && echo .You need to have root privileges.. && exit 1
PIDF=/opt/yunkong2/node_modules/yunkong2.js-controller/lib/yunkong2.pid
#NODECMD=/usr/local/bin/node
NODECMD=/usr/bin/node
yunkong2CMD=/opt/yunkong2/node_modules/yunkong2.js-controller/yunkong2.js
RETVAL=0
yunkong2USER=root
#yunkong2USER=roker

# Starting yunkong2
export yunkong2_HOME=/opt/yunkong2
echo -n "Starting yunkong2"
sudo -u \${yunkong2USER} \$NODECMD \$yunkong2CMD start

EOF




# add service script /etc/systemd/system/yunkong2.service
echo "############### add service script /etc/systemd/system/yunkong2.service  ###############"

sudo cat <<- EOF > /etc/systemd/system/yunkong2.service
#
# Start yunkong2 Daemon
#
# /etc/systemd/system/yunkong2.service
# Invoking scripts to start/shutdown yunkong2

[Unit]
Description=yunkong2 server task
Requires=network.target

[Service]

#User=roker
User=root

Type=forking
RemainAfterExit=yes
Restart=no

ExecStart=/etc/init.d/yunkong2

[Install]
WantedBy=multi-user.target

EOF




# add user for yunkong2
#echo "###############  Add user roker with sudo permisions for yunkong2 ###############"
#sudo useradd roker -G sudo -d /opt/yunkong2
#sudo sh -c "echo 'roker ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers"


# fix permisions
sudo chmod 755  /etc/init.d/yunkong2
sudo chmod 755  /etc/systemd/system/yunkong2.service


# install nodejs and another tools
echo "###############  install NodeJs and another tools ###############"
# install nodejs
sudo apt-get update -y
sudo apt-get --purge remove node -y
sudo apt-get --purge remove nodejs -y
sudo apt-get autoremove
sudo apt-get install -y build-essential nodejs
# install another tools
#sudo apt-get install -y redis-server redis-tools mosquitto mosquitto-clients python-rpi.gpio



# install yunkong2
echo "###############  install yunkong2 ###############"
sudo mkdir /opt/yunkong2
#sudo chown -R roker.roker /opt/yunkong2
sudo chmod 777 /opt/yunkong2
cd /opt/yunkong2
sudo npm install yunkong2 --unsafe-perm
#sudo chown -R roker.roker /opt/yunkong2
sudo ln -s /opt/yunkong2/yunkong2 /usr/sbin/yunkong2

echo "###############  start yunkong2 ###############"
sudo systemctl daemon-reload
sudo systemctl enable yunkong2.service
sudo systemctl start yunkong2.service


echo "###############  status yunkong2 ###############"
sudo systemctl status yunkong2.service

echo "###############  You can login in yunkong2 ###############"
IP=$(ip addr show eth0 | grep "inet\b" | awk '{print $2}' | cut -d/ -f1)
echo "###############  http://$IP:8081/  ###############"
