#!/bin/sh

# build
#	BH tools script
#
#	Copyright (c) 2015 BiZright Technology Inc. 
#################################################################################

set -e

DIR=`dirname ${0}`

cd $DIR
DIR=`pwd`

if [ -f /lib/systemd/system/shutdown-sensor.service -a -x "`which systemctl`" ]; then
	sudo systemctl stop shutdown-sensor.service || :
fi

if [ -f /etc/init/shutdown-sensor.conf -a -x "`which initctl`" ]; then
	sudo initctl stop shutdown-sensor || :
fi

if [ "$1" = "uninstall" ]; then
	sudo systemctl disable shutdown-sensor.service || :
	sudo systemctl disable set-rtc.service || :
	sudo rm /usr/local/sbin/shutdownSensor
#	sudo rm /usr/local/sbin/shutdownSensor.conf || :
	sudo rm /usr/local/sbin/__fastdown-function
	sudo rm /usr/local/sbin/fastshutdown
	sudo rm /usr/local/sbin/fastreboot
	sudo rm /usr/local/sbin/set-rtc
	sudo rm /lib/systemd/system/shutdown-sensor.service || :
	sudo rm /lib/systemd/system/set-rtc.service || :
	sudo rm /etc/init/shutdown-sensor.conf || :
	sudo rm /etc/init/set-rtc.conf || :
	sudo rm /etc/network/if-up.d/ntpdate
	sudo rm /etc/dhcpcd.enter-hook
	sudo rm /etc/cron.d/ntpdate
	sudo rm /etc/cron.d/set-fake-hwclock
	sudo killall -9 ntpdate || :
	sudo update-rc.d fake-hwclock enable || :
	sudo update-rc.d ntp enable || :
	echo "Uninstall of BH tools completed."
	echo "Please reboot."
	exit
fi

sudo apt-get update
sudo apt -f --fix-broken install
sudo apt-get -y install git

if [ ! -d $DIR/wiringPi ]; then
	# git clone --depth 1 git://git.drogon.net/wiringPi
	# if [ $? != 0 ]; then
	# 	wget -O 'wiringPi.tar.gz' 'https://git.drogon.net/?p=wiringPi;a=snapshot;h=master;sf=tgz'
	# 	tar axf wiringPi.tar.gz
	# 	mv wiringPi-master-* wiringPi
	# fi
	git clone --depth 1 https://github.com/WiringPi/WiringPi wiringPi
fi

cd $DIR/wiringPi
# git pull origin
./build
cd $DIR/shutdownSensor
make

sudo install -o root -g root -m 755 ./shutdownSensor /usr/local/sbin/shutdownSensor
#sudo install -o root -g root -m 644 ./shutdownSensor.conf /usr/local/sbin/shutdownSensor.conf
sudo install -o root -g root -m 755 $DIR/usr/local/sbin/__fastdown-function /usr/local/sbin/__fastdown-function
sudo install -o root -g root -m 755 $DIR/usr/local/sbin/fastshutdown /usr/local/sbin/fastshutdown
sudo install -o root -g root -m 755 $DIR/usr/local/sbin/fastreboot /usr/local/sbin/fastreboot
sudo install -o root -g root -m 755 $DIR/usr/local/sbin/set-rtc /usr/local/sbin/set-rtc

if [ -x "`which systemctl`" ]; then
	sudo install -o root -g root -m 644 $DIR/lib/systemd/system/shutdown-sensor.service /lib/systemd/system/shutdown-sensor.service
else
	sudo install -o root -g root -m 644 $DIR/etc/init/shutdown-sensor.conf /etc/init/shutdown-sensor.conf
fi

if [ ! -e "/usr/local/sbin/system-stop-use-usb" ]; then
	sudo install -o root -g root -m 755 $DIR/usr/local/sbin/system-stop-use-usb /usr/local/sbin/system-stop-use-usb
fi

if [ ! -e "/usr/local/sbin/system-stop" ]; then
	sudo install -o root -g root -m 755 $DIR/usr/local/sbin/system-stop /usr/local/sbin/system-stop
fi

if [ -f /lib/systemd/system/shutdown-sensor.service -a -x "`which systemctl`" ]; then
	sudo systemctl start shutdown-sensor.service || :
	sudo systemctl enable shutdown-sensor.service || :
fi

if [ -f /etc/init/shutdown-sensor.conf -a -x "`which initctl`" ]; then
	sudo initctl start shutdown-sensor || :
fi

if [ -f "/boot/firmware/config.txt" ]; then
    sudo sed -i -e "s/#dtparam=i2c_arm=on/dtparam=i2c_arm=on/g" /boot/firmware/config.txt
else
    sudo sed -i -e "s/#dtparam=i2c_arm=on/dtparam=i2c_arm=on/g" /boot/config.txt
fi


if test `grep i2c-dev /etc/modules | wc -c` = 0 ; then
	sudo sh -c 'echo "i2c-dev" >> /etc/modules'
fi

sudo apt-get -y install fake-hwclock
sudo update-rc.d fake-hwclock disable || :
sudo update-rc.d ntp disable || :

if [ -x "`which systemctl`" ]; then
	sudo install -o root -g root -m 644 $DIR/lib/systemd/system/set-rtc.service /lib/systemd/system/set-rtc.service
	sudo systemctl enable set-rtc.service || :
else
	sudo install -o root -g root -m 644 $DIR/etc/init/set-rtc.conf /etc/init/set-rtc.conf
fi

sudo apt-get -y install ntpdate

sudo install -o root -g root -m 755 $DIR/etc/network/if-up.d/ntpdate /etc/network/if-up.d/ntpdate
sudo install -o root -g root -m 755 $DIR/etc/dhcpcd.enter-hook /etc/dhcpcd.enter-hook

sudo install -o root -g root -m 644 $DIR/etc/cron.d/ntpdate /etc/cron.d/ntpdate

sudo install -o root -g root -m 644 $DIR/etc/cron.d/set-fake-hwclock /etc/cron.d/set-fake-hwclock

if [ -f "/etc/default/ntpsec-ntpdate" ]; then
    sudo sed -i -e "s/NTPDATE_USE_NTP_CONF=yes/NTPDATE_USE_NTP_CONF=no/g" /etc/default/ntpsec-ntpdate
    sudo sed -i -e "s/NTPSERVERS=.*/NTPSERVERS=\"ntp.nict.jp ntp.jst.mfeed.ad.jp ntp.ring.gr.jp\"/g" /etc/default/ntpsec-ntpdate
else
    sudo sed -i -e "s/NTPDATE_USE_NTP_CONF=yes/NTPDATE_USE_NTP_CONF=no/g" /etc/default/ntpdate
    sudo sed -i -e "s/NTPSERVERS=.*/NTPSERVERS=\"ntp.nict.jp ntp.jst.mfeed.ad.jp ntp.ring.gr.jp\"/g" /etc/default/ntpdate
fi


sudo modprobe -q bcm2708_wdog &&:
if [ "$?" -eq 0 ] ; then
        WDT_MODULE='bcm2708_wdog'
else
        WDT_MODULE='bcm2835_wdt'
fi
sudo modprobe $WDT_MODULE &&:
if [ "$?" -ne 0 ] ; then
        echo "Error: WDT module is unknown."
        exit
fi

echo "Use WDT module: $WDT_MODULE";

sudo apt-get install watchdog
if [ -x "`which systemctl`" ]; then
	if test `grep "WantedBy=multi-user.target" /lib/systemd/system/watchdog.service | wc -c` = 0 ; then
		sudo sh -c 'echo "WantedBy=multi-user.target" >> /lib/systemd/system/watchdog.service'
	fi
fi
sudo update-rc.d watchdog enable

sudo sed -i -e "s/watchdog_module=\"none\"/watchdog_module=\"$WDT_MODULE\"/g" /etc/default/watchdog

sudo sed -i -e "s/#max-load-1	/max-load-1	/g" /etc/watchdog.conf
sudo sed -i -e "s/#watchdog-device	/watchdog-device	/g" /etc/watchdog.conf
if test `grep "watchdog-timeout = 10" /etc/watchdog.conf | wc -c` = 0 ; then
        sudo sh -c 'echo "watchdog-timeout = 10" >> /etc/watchdog.conf'
fi

if test `grep $WDT_MODULE /etc/modules | wc -c` = 0 ; then
        sudo sh -c "echo $WDT_MODULE >> /etc/modules"
fi



if [ ! -x "`which systemctl`" ]; then
	echo 'Yes, do as I say!' | sudo apt-get --force-yes install upstart
fi

echo "Installation of BH tools completed."
echo "Please reboot."

