#!/bin/bash
################################################################################
# Auto install/update deCONZ for Raspberry Pi (stable and beta channel).
#  - This script is usually copied to /tmp and executed from there.
#  - Dont call this script directly use deCONZ-update.sh from deCONZ package instead.
################################################################################
APP=deconz
VERSION_STABLE=2.04.35
VERSION_BETA=2.04.77

# auto configure system
OS_ID=$(cat /etc/os-release | sed -n 's/^ID=\(.*\)/\1/p')
OS_VERSION=$(cat /etc/os-release | sed -n 's/^VERSION="[0-9]\s(\(.*\))"/\1/p')
OS_VERSION_NR=$(cat /etc/os-release | grep VERSION_ID | cut -d'=' -f2 | xargs)
WGETVERSION=$(dpkg -s wget | grep Version | cut -d' ' -f2)
CURVERSION=$(dpkg -s deconz | grep Version | cut -d' ' -f2)
RPIREV=$(cat /proc/cpuinfo | grep Revision | cut -d':' -f2 | xargs)
USERAGENT="User-Agent: Wget/${WGETVERSION} (GNU/Linux) deCONZ/${CURVERSION} ${OS_ID}/${OS_VERSION_NR}"
OTAUDIR="/home/${USER}/otau"
WD_SCRIPT="test-deconz.sh"

dpkg -l deconz
if [ $? -eq 1 ]; then
	echo "deCONZ not installed"
	exit 1
fi

dpkg --compare-versions ${CURVERSION} gt ${VERSION_STABLE}
if [ $? -eq 0 ]; then
	# beta based on qt5
	QT_VERSION=5
	CHANNEL=beta
	VERSION=${VERSION_BETA}-qt5
	URL="http://www.dresden-elektronik.de/rpi/deconz/beta"
else
	# stable based on qt4, transition: tbd
	QT_VERSION=4
	CHANNEL=stable
	VERSION=${VERSION_STABLE}
	URL="http://www.dresden-elektronik.de/rpi/deconz"
fi

APPFILE="${APP}-${VERSION}.deb"

echo "download $APPFILE in $CHANNEL channel (qt${QT_VERSION})"

# check if deCONZ is running and quit if so
if [ "$(pidof deCONZ)" ]; then
	echo "deCONZ is running, please close before proceed with update"
	exit 1
fi

# stop watchdog if deCONZ test script is installed
if [ -e "/etc/watchdog.d/${WD_SCRIPT}" ]; then
	systemctl status watchdog.service > /dev/null
	if [ $? -eq 0 ]; then
		echo "stop watchdog while update deCONZ"
		sudo systemctl stop watchdog.service > /dev/null
	fi

	curl http://www.dresden-elektronik.de/rpi/deconz/etc/watchdog.d/${WD_SCRIPT} > /tmp/${WD_SCRIPT}

	if [ $? -eq 0 ]; then
		diff /tmp/${WD_SCRIPT} /etc/watchdog.d/${WD_SCRIPT} > /dev/null
		if [ $? -ne 0 ]; then
			echo "update watchdog deCONZ test script"
			sudo cp /tmp/${WD_SCRIPT} /etc/watchdog.d/${WD_SCRIPT}
		else
			echo "watchdog deCONZ test script is up to date"
		fi
		rm /tmp/${WD_SCRIPT}
	fi

	if [ -x "/etc/watchdog.d/${WD_SCRIPT}" ]; then
		echo "watchdog deCONZ test script looks ok"
	else
		echo "make watchdog deCONZ test script executable"
		sudo chmod +x /etc/watchdog.d/${WD_SCRIPT}
	fi
fi

if [ "$OS_ID" = "raspbian" ]; then
	USERAGENT="User-Agent: Wget/${WGETVERSION} (linux-gnueabihf) deCONZ/${CURVERSION} rpirev/${RPIREV} ${OS_ID}/${OS_VERSION_NR}"
fi

if [ "$OS_ID" = "raspbian" ] && [ "$OS_VERSION" = "jessie" ] && [ "$CHANNEL" = "stable" ]; then
	# on Raspbian Jessie RaspBee can co exist with bluetooth
	egrep -q -e "^enable_uart=1" /boot/config.txt
	if [ $? -eq 1 ]; then
		sudo sed -i s/^enable_uart=.*//g /boot/config.txt
		sudo sh -c 'echo "enable_uart=1" >> /boot/config.txt'
	fi

	egrep -q -e "console=serial0" /boot/cmdline.txt
	if [ $? -eq 0 ]; then
		sudo sed -i s/console=serial0,115200//g /boot/cmdline.txt
	fi

	egrep -q -e "^dtoverlay=pi3-miniuart-bt-overlay" /boot/config.txt
	if [ $? -eq 1 ]; then
		sudo sed -i s/^dtoverlay=pi3-disable-bt.*//g /boot/config.txt
		sudo sh -c 'echo "dtoverlay=pi3-miniuart-bt-overlay" >> /boot/config.txt'
	fi
fi

## sanity satisfy dependencies
if [ $QT_VERSION -eq 4 ]; then
	# check for qt4
	dpkg -l libqt4-core > /dev/null 2>&1

	if [ $? -ne 0 ]; then
		sudo apt-get install --yes libqt4-core
	fi
elif [ $QT_VERSION -eq 5 ]; then
	# check for qt5
	deps=("libqt5core5a" "libqt5network5" "libqt5widgets5" "libqt5gui5" "libqt5serialport5" "libqt5websockets5" "libqt5sql5" "wiringpi")
	for i in "${deps[@]}"; do
		dpkg -l $i 2> /dev/null | grep '^ii' > /dev/null

		if [ $? -ne 0 ]; then
			sudo apt-get -y install $i
		else
			echo "$i is installed"
		fi
	done
fi

# check for setcap / getcap
dpkg -l libcap2-bin > /dev/null 2>&1

if [ $? -ne 0 ]; then
	sudo apt-get install --yes libcap2-bin
fi

# check if otau dir exists
if [ ! -d "$OTAUDIR" ]; then
	echo creating otau directory
	mkdir -p ${OTAUDIR}
fi

# remove 'old' deCONZ package if exist
if [ "$CHANNEL" = "stable" ] && [ -e "${APPFILE}" ]; then
	rm ${APPFILE}
fi

# check for deCONZ
dpkg -l deconz 2> /dev/null | grep "${VERSION}" > /dev/null

if [ $? -ne 0 ]; then

	# download deCONZ package
	wget --user-agent="${USERAGENT}" ${URL}/${APPFILE}

	if [ $? -ne 0 ]; then
		exit 1
	fi

	# install deCONZ
	sudo dpkg --install ${APPFILE}

	if [ $? -ne 0 ]; then
		exit 1
	fi

	# cleanup
	if [ -e "${APPFILE}" ]; then
		rm ${APPFILE}
	fi

fi

# sync filesystem
sync

# reboot so that all is written properly to the sd-card
# and new deCONZ-autostart.sh will be started after bootup
if [ "$CHANNEL" = "stable" ] && [ -e "/home/pi/.config/autostart/deCONZ.desktop" ]; then
	sudo reboot
fi

exit 0
