#!/bin/sh

echo "Starting Hub Install - Please Wait"
echo "##################################"

dirname=`dirname $0`

# Architecture
arch=`uname -m`

# Install Node 12 Respository
curl -sL https://deb.nodesource.com/setup_12.x | sudo bash -

# Update System
apt-get -y update
apt-get -y upgrade
apt-get -y dist-upgrade
apt-get -y autoremove

# Install Packages
apt-get -y install ca-certificates
apt-get -y install nodejs gcc g++ make git python mosquitto mosquitto-clients

# Halt If NPM Did Not Install
if [ ! -x "/usr/bin/npm" ]; then
	echo "Failure Installing Packages, Please Reboot And Try Again"
	exit 1
fi

# System
RESET=$1

# Update NPM
npm install -g npm

# Install Node Packages
npm install -g mqtt simple-node-logger minimist express body-parser

# Install Homebridge
npm install -g --unsafe-perm homebridge homebridge-config-ui-x homebridge-mqttthing

# Add Berto User
useradd -m -G sudo,users,dialout berto

# Add Berto User To Sudo Access Without Password
echo "berto ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/010_berto-nopasswd

# Download Flic Daemon
case $arch in
	aarch64)
		wget -O /usr/local/bin/flicd https://raw.githubusercontent.com/50ButtonsEach/fliclib-linux-hci/master/bin/aarch64/flicd
		;;
	arm*)
		wget -O /usr/local/bin/flicd https://raw.githubusercontent.com/50ButtonsEach/fliclib-linux-hci/master/bin/armv6l/flicd
		;;
	x86_64)
		wget -O /usr/local/bin/flicd https://raw.githubusercontent.com/50ButtonsEach/fliclib-linux-hci/master/bin/x86_64/flicd
		;;
	*)
		wget -O /usr/local/bin/flicd https://raw.githubusercontent.com/50ButtonsEach/fliclib-linux-hci/master/bin/i386/flicd
		;;
esac

if [ -e "/usr/local/bin/flicd" ]; then
	chmod 755 /usr/local/bin/flicd
	setcap cap_net_admin=ep /usr/local/bin/flicd
fi

# Install Berto Hub Server
rm -rf /usr/lib/node_modules/berto
cp -av $dirname /usr/lib/node_modules/berto
chown -R root:root /usr/lib/node_modules/berto
chmod 755 /usr/lib/node_modules/berto/*.js

# Install zigbee2mqtt
git clone https://github.com/Koenkk/zigbee2mqtt.git /opt/zigbee2mqtt
chown -R berto:berto /opt/zigbee2mqtt
cd /opt/zigbee2mqtt
su -c "npm ci" berto
cp /usr/lib/node_modules/berto/zigbee2mqtt.yaml /opt/zigbee2mqtt/data/configuration.yaml
chown berto:berto /opt/zigbee2mqtt/data/configuration.yaml

# Install zwavejs2mqtt
git clone https://github.com/zwave-js/zwavejs2mqtt /opt/zwavejs2mqtt
chown -R berto:berto /opt/zwavejs2mqtt
cd /opt/zwavejs2mqtt
su -c "npm install; npm audit fix; npm run build" berto
cp /usr/lib/node_modules/berto/zwavejs2mqtt.json /opt/zwavejs2mqtt/store/settings.json
chown berto:berto /opt/zwavejs2mqtt/store/settings.json

# Install Service
cp /usr/lib/node_modules/berto/*.service /etc/systemd/system
chmod 644 /etc/systemd/system/*.service

# Install Berto Logrotate Configuration
cp /usr/lib/node_modules/berto/berto.logrotate /etc/logrotate.d/berto

# Create Log File
rm -f /var/log/berto.log
touch /var/log/berto.log
chown berto:berto /var/log/berto.log
chmod 666 /var/log/berto.log

# Create Links For Services
ln -s /usr/lib/node_modules/berto/server.js /usr/local/bin/berto-server
ln -s /usr/lib/node_modules/berto/homebridge.js /usr/local/bin/berto-homebridge
ln -s /usr/lib/node_modules/berto/flic.js /usr/local/bin/berto-flic

# Remove Existing Homebridge Configuration
if [ "$RESET" = "YES" ]; then
	echo "Reset Configuration"
	rm -rf /var/lib/homebridge/*
fi

# Install Homebridge As A Service
hb-service --user berto install

# Setup Services
systemctl daemon-reload
systemctl enable berto berto-server berto-homebridge berto-flic zigbee2mqtt ssh homebridge
systemctl disable bluetooth zwavejs2mqtt

echo "################################"
echo "Hub Install Complete - Rebooting"

reboot