#!/bin/sh

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

dirname=`dirname $0`

# Stop Services
systemctl stop berto
systemctl stop zigbee2mqtt
systemctl stop zwavejs2mqtt
hb-service stop

# Remove Node Packages
npm uninstall -g simple-node-logger homebridge homebridge-mqttthing homebridge-config-ui-x homebridge-mqttthing minimist mqtt ip node-getmac hap-nodejs

# Remove Packages
apt-get -y purge nodejs gcc g++ make git python mosquitto mosquitto-clients npm monit nginx screen perl libnet-ssleay-perl openssl

# Check For Other Packages To Remove
apt-get -y autoremove

# Remove Flic Daemon
rm -f /usr/local/bin/flicd

# Remove Berto Hub Server
rm -rf /usr/lib/node_modules/berto

# Remove Berto Services
rm -f /etc/systemd/system/berto*.service /etc/systemd/system/zigbee2mqtt.service /etc/systemd/system/zwavejs2mqtt.service 

# Remove Berto Logrotate Configuration
rm -f /etc/logrotate.d/berto

# Remove Log File
rm -f /var/log/berto.log

# Remove Links For Services
rm -f /usr/local/bin/berto-*

# Setup Services
systemctl daemon-reload

# Remove Berto User Sudo Access Without Password
rm -f /etc/sudoers.d/010_berto-nopasswd

# Remove Configurations
rm -rf /root/persist /var/lib/homebridge /opt/zigbee2mqtt /opt/zwavejs2mqtt

# Remove Berto User
userdel berto
rm -rf /home/berto

echo "#################################"
echo "Hub Uinstall Complete - Rebooting"

reboot