#!/bin/bash
if [ "$(id -u)" != "0" ]; then
	echo "This script must be run as root" 1>&2
	exit 1
fi

SCRIPTDIR="$( cd "$(dirname $0)" && pwd )"
export NCURSES_NO_UTF8_ACS=1

builddate="current"
if [[ -n $1 ]]; then
	builddate=$1
fi

readonlyRoot=$(grep "/dev/root" /proc/mounts | grep -c "\sro[\s,]")
[[ $readonlyRoot -ne 0 ]] && mount -o remount,rw /

free_log_space=$(df -P -B 1 /var/log | tail -1 | awk '{print $4}')
if [[ $free_log_space -lt 10000000 ]]; then
  /usr/sbin/logrotate -s /data/logrotate-status -f /etc/logrotate.conf
  # Restart rsyslog because of bug in logrotate config of older images
  systemctl restart rsyslog
  /usr/sbin/logrotate -s /data/logrotate-status -f /etc/logrotate.conf
  free_log_space=$(df -P -B 1 /var/log | tail -1 | awk '{print $4}')
  if [[ $free_log_space -lt 10000000 ]]; then
    sync
    sleep 5
    [[ $readonlyRoot -ne 0 ]] && mount -o remount,ro /
    exit 1
  fi
fi

rm -Rf /tmp/homegear-nightly-installer-cache
rm -Rf /data/homegear-nightly-installer-cache
rm -Rf /var/lib/homegear/homegear-nightly-installer-cache
SPACE_AVAILABLE=$(df -P /tmp | tail -1 | awk '{print $4}')
if [[ $SPACE_AVAILABLE -lt 200000 ]]; then
	if [[ -d /data ]]; then
		SPACE_AVAILABLE=$(df -P /data | tail -1 | awk '{print $4}')
		if [[ $SPACE_AVAILABLE -lt 200000 ]]; then
			echo "Not enough disk space available in /tmp or /data"
			exit 1
		else
			TEMPDIR="/data/homegear-nightly-installer-cache"
		fi
	else
		SPACE_AVAILABLE=$(df -P /var/lib/homegear | tail -1 | awk '{print $4}')
		if [[ $SPACE_AVAILABLE -lt 200000 ]]; then
			echo "Not enough disk space available in /tmp or /var/lib/homegear"
			exit 1
		else
			TEMPDIR="/var/lib/homegear/homegear-nightly-installer-cache"
		fi
	fi
else
	TEMPDIR="/tmp/homegear-nightly-installer-cache"
fi

backtitle="Homegear Nightly Installer"
installedPackages=$(dpkg-query -W -f '${db:Status-Abbrev}|${binary:Package}\n' '*' 2>/dev/null | grep '^ii' | awk -F '|' '{print $2}' | cut -d ':' -f 1)
neededDependencies="lsb-release dialog apt-utils curl gnupg apt-transport-https"
dependencies=()

for package in $neededDependencies; do
	grep -q -x -e "$package" <<< "$installedPackages"
	[[ $? -ne 0 ]] && dependencies+=("$package")
done

if [[ ${#dependencies[@]} -gt 0 ]]; then
	echo "Installing dependencies..."
	apt-get update
	[[ $? -ne 0 ]] && exit 1
	apt-get -y --no-install-recommends -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" install "${dependencies[@]}"
	[[ $? -ne 0 ]] && exit 1
fi

distribution=$(lsb_release -i -s | tr '[:upper:]' '[:lower:]')
codename=$(lsb_release -c -s)
arch=$(dpkg --print-architecture)

supportedArchitectures=$'amd64\narmhf\narm64'
if ! grep -q -x -e "$arch" <<< "$supportedArchitectures"; then
	echo "Architecture $arch is not supported."
	exit 1
fi

supportedDistributions=$'debian\nraspbian\nubuntu'
supportedCodenames=$'wheezy\njessie\nstretch\nbuster\nbullseye\ntrusty\nxenial\nbionic\nfocal'
if ! grep -q -x -e "$distribution" <<< "$supportedDistributions" || ! grep -q -x -e "$codename" <<< "$supportedCodenames"; then
	TTY_X=$(($(stty size | awk '{print $2}')-6))
	TTY_Y=$(($(stty size | awk '{print $1}')-6))
	distributionOptions=("1" "Debian" ON)
	distributionOptions+=("2" "Ubuntu" OFF)
	distributionOptions+=("3" "Raspberry Pi OS" OFF)
	system=$(dialog --stdout --backtitle "$backtitle" --radiolist "Choose Distribution" $TTY_Y $TTY_X $(($TTY_Y - 8)) "${distributionOptions[@]}")
	[ $? -ne 0 ] && exit 1
	if [ $system -eq 1 ]; then
		distributionOptions=("1" "Debian 11 (Bullseye)" ON)
		distributionOptions=("2" "Debian 10 (Buster)" ON)
		system=$(dialog --stdout --backtitle "$backtitle" --radiolist "Choose Distribution" $TTY_Y $TTY_X $(($TTY_Y - 8)) "${distributionOptions[@]}")
		[ $? -ne 0 ] && exit 1
		if [ $system -eq 1 ]; then
			distribution="debian"
			codename="bullseye"
			system="debian_bullseye"
		elif [ $system -eq 2 ]; then
			distribution="debian"
			codename="buster"
			system="debian_buster"
		fi
	elif [ $system -eq 2 ]; then
		distributionOptions=("1" "Ubuntu 20.04 (Focal Fossa)" ON)
		distributionOptions+=("2" "Ubuntu 18.04 (Bionic Beaver)" OFF)
		system=$(dialog --stdout --backtitle "$backtitle" --radiolist "Choose Distribution" $TTY_Y $TTY_X $(($TTY_Y - 8)) "${distributionOptions[@]}")
		[ $? -ne 0 ] && exit 1
		if [ $system -eq 1 ]; then
			distribution="ubuntu"
			codename="focal"
			system="ubuntu_focal"
		elif [ $system -eq 2 ]; then
			distribution="ubuntu"
			codename="bionic"
			system="ubuntu_bionic"
		fi
	elif [ $system -eq 3 ]; then
		distributionOptions=("1" "Raspberry Pi OS 11 (Bullseye)" ON)
		distributionOptions+=("2" "Raspberry Pi OS 10 (Buster)" OFF)
		system=$(dialog --stdout --backtitle "$backtitle" --radiolist "Choose Distribution" $TTY_Y $TTY_X $(($TTY_Y - 8)) "${distributionOptions[@]}")
		[ $? -ne 0 ] && exit 1
		if [ $system -eq 1 ]; then
			distribution="raspbian"
			codename="bullseye"
			system="raspbian_pi_os_bullseye"
		elif [ $system -eq 2 ]; then
			distribution="raspbian"
			codename="buster"
			system="raspbian_buster"
		fi
	fi
else
	system=${distribution}_${codename}
fi

if [[ $distribution == "raspbian" ]]; then
	distribution="raspberry_pi_os"
fi

buildVersion=$(curl -s "https://downloads.homegear.eu/getNightlyBuildInfo.php?f=libhomegear-base_${builddate}_${system}_${arch}.deb&t=version")
buildTime=$(curl -s "https://downloads.homegear.eu/getNightlyBuildInfo.php?f=libhomegear-base_${builddate}_${system}_${arch}.deb&t=date")
backtitle="$backtitle - Version: ${buildVersion} (build time: ${buildTime})"

function moduleInstalled()
{
	installed=$(dpkg-query -W -f='${Status}\n' $1 2>/dev/null | grep -c installed);
	if [ $installed -eq 1 ]; then
		echo -n ON
	else
		echo -n OFF
	fi
}

function moduleInstalled2()
{
	installed=$(dpkg-query -W -f='${Status}\n' $1 2>/dev/null | grep -c installed);
	if [ $installed -eq 1 ]; then
		return 0
	else
		return 1
	fi
}

TTY_X=$(($(stty size | awk '{print $2}')-6))
TTY_Y=$(($(stty size | awk '{print $1}')-6))
moduleOptions=("01" "HomeMatic BidCoS" $(moduleInstalled homegear-homematicbidcos))
moduleOptions+=("02" "HomeMatic Wired" $(moduleInstalled homegear-homematicwired))
moduleOptions+=("03" "Insteon" $(moduleInstalled homegear-insteon))
moduleOptions+=("04" "MAX!" $(moduleInstalled homegear-max))
moduleOptions+=("05" "Philips Hue" $(moduleInstalled homegear-philipshue))
moduleOptions+=("06" "Sonos" $(moduleInstalled homegear-sonos))
moduleOptions+=("09" "Kodi" $(moduleInstalled homegear-kodi))
moduleOptions+=("10" "IP Cam" $(moduleInstalled homegear-ipcam))
moduleOptions+=("11" "Beckhoff BK90x0" $(moduleInstalled homegear-beckhoff))
moduleOptions+=("14" "KNX" $(moduleInstalled homegear-knx))
moduleOptions+=("15" "EnOcean" $(moduleInstalled homegear-enocean))
moduleOptions+=("16" "Intertechno" $(moduleInstalled homegear-intertechno))
moduleOptions+=("17" "Z-Wave" $(moduleInstalled homegear-zwave))
moduleOptions+=("19" "Renkforce (Conrad RS2W)" $(moduleInstalled homegear-rs2w))
moduleOptions+=("20" "Conrad RSL" $(moduleInstalled homegear-rsl))
moduleOptions+=("22" "Nanoleaf" $(moduleInstalled homegear-nanoleaf))
moduleOptions+=("23" "wM-Bus" $(moduleInstalled homegear-mbus))
moduleOptions+=("24" "HomeMatic CCU" $(moduleInstalled homegear-ccu))
moduleOptions+=("26" "Zigbee" $(moduleInstalled homegear-zigbee))
moduleOptions+=("27" "Velux KLF200" $(moduleInstalled homegear-velux-klf200))
moduleOptions+=("28" "ABI MC 1500" $(moduleInstalled homegear-abi))
moduleOptions+=("95" "UI" $(moduleInstalled homegear-ui))
moduleOptions+=("96" "WebSSH Client" $(moduleInstalled homegear-webssh))
moduleOptions+=("97" "Management Client" $(moduleInstalled homegear-management))
moduleOptions+=("98" "Admin UI" $(moduleInstalled homegear-adminui))
moduleOptions+=("99" "InfluxDB Client" $(moduleInstalled homegear-influxdb))
extraModules=()
while true; do
	modules=$(dialog --stdout --backtitle "$backtitle" --extra-button --extra-label "Extra modules" --checklist "Choose Homegear modules" $TTY_Y $TTY_X $(($TTY_Y - 8)) "${moduleOptions[@]}")
	exitcode=$?
	[[ $exitcode -eq 1 ]] && exit 1
	[[ $exitcode -eq 255 ]] && exit 1
	moduleInstalled2 homegear-easycam && extraModules+=(homegear-easycam)
	moduleInstalled2 homegear-easyled && extraModules+=(homegear-easyled)
	moduleInstalled2 homegear-easyled2 && extraModules+=(homegear-easyled2)
	moduleInstalled2 binrpc && extraModules+=(binrpc)
	moduleInstalled2 homegear-dc-connector && extraModules+=(homegear-dc-connector)
	moduleInstalled2 homegear-cloudconnect && extraModules+=(homegear-cloudconnect)
	moduleInstalled2 ibs-ssh && extraModules+=(ibs-ssh)
	if [[ $exitcode -eq 3 ]]; then
		TTY_X=$(($(stty size | awk '{print $2}')-6))
		TTY_Y=$(($(stty size | awk '{print $1}')-6))
		extraModuleOptions=("07" "EASYCam" $(moduleInstalled homegear-easycam))
		extraModuleOptions+=("08" "EASYLed" $(moduleInstalled homegear-easyled))
		extraModuleOptions+=("18" "EASYLed 2" $(moduleInstalled homegear-easyled2))
		extraModuleOptions+=("93" "Binary RPC test tool" $(moduleInstalled binrpc))
		extraModuleOptions+=("94" "Homegear Daisy Chain Connector" $(moduleInstalled homegear-dc-connector))
		extraModuleOptions+=("91" "Homegear Cloudconnect" $(moduleInstalled homegear-cloudconnect))
		extraModuleOptions+=("92" "ib symphony SSH" $(moduleInstalled ibs-ssh))
		extraModules2=$(dialog --stdout --backtitle "$backtitle" --checklist "Choose Homegear modules" $TTY_Y $TTY_X $(($TTY_Y - 8)) "${extraModuleOptions[@]}")
		exitcode2=$?
		[[ $exitcode2 -eq 1 ]] && continue
		[[ $exitcode2 -eq 255 ]] && continue
	fi
	break
done
if [[ -n $extraModules2 ]]; then
	modules+=" $extraModules2"
else
	modules+=" $extraModules"
fi

function downloadModule {
	if [[ "$modules" == *"${1}"* ]]; then
		TTY_X=$(($(stty size | awk '{print $2}')-6))
		TTY_Y=$(($(stty size | awk '{print $1}')-6))
		wget https://downloads.homegear.eu/nightlies/${2} 2>&1 | dialog --backtitle "$backtitle" --progressbox "Downloading ${2}..." $TTY_Y $TTY_X
		[[ ${PIPESTATUS[0]} -ne 0 ]] && exit 1
	fi
}

function installModule {
	if [[ "$modules" == *"${1}"* ]]; then
		TTY_X=$(($(stty size | awk '{print $2}')-6))
		TTY_Y=$(($(stty size | awk '{print $1}')-6))
		dpkg --force-confdef --force-confold -i ${2} 2>&1 | dialog --backtitle "$backtitle" --progressbox "Installing ${2}..." $TTY_Y $TTY_X
		if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
			apt-get -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" -y -f install | dialog --backtitle "$backtitle" --progressbox "Installing dependencies..." $TTY_Y $TTY_X
			[[ ${PIPESTATUS[0]} -ne 0 ]] && exit 1
			dpkg --force-confdef --force-confold -i ${2} 2>&1 | dialog --backtitle "$backtitle" --progressbox "Installing ${2}..." $TTY_Y $TTY_X
			[[ ${PIPESTATUS[0]} -ne 0 ]] && exit 1
		fi
	fi
}

function installModuleOverwriteConfig {
	if [[ "$modules" == *"${1}"* ]]; then
		TTY_X=$(($(stty size | awk '{print $2}')-6))
		TTY_Y=$(($(stty size | awk '{print $1}')-6))
		dpkg --force-confdef --force-confnew -i ${2} 2>&1 | dialog --backtitle "$backtitle" --progressbox "Installing ${2}..." $TTY_Y $TTY_X
		if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
			apt-get -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" -y -f install | dialog --backtitle "$backtitle" --progressbox "Installing dependencies..." $TTY_Y $TTY_X
			[[ ${PIPESTATUS[0]} -ne 0 ]] && exit 1
			dpkg --force-confdef --force-confnew -i ${2} 2>&1 | dialog --backtitle "$backtitle" --progressbox "Installing ${2}..." $TTY_Y $TTY_X
			[[ ${PIPESTATUS[0]} -ne 0 ]] && exit 1
		fi
	fi
}

if [[ -n $2 ]]; then
	echo "Sleeping $2 seconds"
	sleep $2
	[[ $readonlyRoot -ne 0 ]] && mount -o remount,rw /
fi

curl -fsSL https://apt.homegear.eu/Release.key | gpg --dearmor -o /usr/share/keyrings/homegear-archive-keyring.gpg

echo "deb [signed-by=/usr/share/keyrings/homegear-archive-keyring.gpg] https://apt.homegear.eu/${distribution}/${codename}/homegear/stable/ ${codename} main" > /etc/apt/sources.list.d/homegear.list
#echo "deb https://aptnightly.homegear.eu/$(tr '[:lower:]' '[:upper:]' <<< ${distribution:0:1})${distribution:1}/ ${codename}/" >> /etc/apt/sources.list.d/homegear.list

TTY_X=$(($(stty size | awk '{print $2}')-6))
TTY_Y=$(($(stty size | awk '{print $1}')-6))
apt-get update 2>&1 | dialog --backtitle "$backtitle" --progressbox "Installing packages..." $TTY_Y $TTY_X
[[ $? -ne 0 ]] && exit 1
TTY_X=$(($(stty size | awk '{print $2}')-6))
TTY_Y=$(($(stty size | awk '{print $1}')-6))
apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" install nodejs-homegear 2>&1 | dialog --backtitle "$backtitle" --progressbox "Installing packages..." $TTY_Y $TTY_X

mkdir $TEMPDIR
cd $TEMPDIR

TTY_X=$(($(stty size | awk '{print $2}')-6))
TTY_Y=$(($(stty size | awk '{print $1}')-6))
wget https://downloads.homegear.eu/nightlies/libhomegear-base_${builddate}_${system}_${arch}.deb 2>&1 | dialog --backtitle "$backtitle" --progressbox "Downloading base library..." $TTY_Y $TTY_X
[[ ${PIPESTATUS[0]} -ne 0 ]] && exit 1

TTY_X=$(($(stty size | awk '{print $2}')-6))
TTY_Y=$(($(stty size | awk '{print $1}')-6))
wget https://downloads.homegear.eu/nightlies/libhomegear-node_${builddate}_${system}_${arch}.deb 2>&1 | dialog --backtitle "$backtitle" --progressbox "Downloading node library..." $TTY_Y $TTY_X
[[ ${PIPESTATUS[0]} -ne 0 ]] && exit 1

TTY_X=$(($(stty size | awk '{print $2}')-6))
TTY_Y=$(($(stty size | awk '{print $1}')-6))
wget https://downloads.homegear.eu/nightlies/libhomegear-ipc_${builddate}_${system}_${arch}.deb 2>&1 | dialog --backtitle "$backtitle" --progressbox "Downloading IPC library..." $TTY_Y $TTY_X
[[ ${PIPESTATUS[0]} -ne 0 ]] && exit 1

TTY_X=$(($(stty size | awk '{print $2}')-6))
TTY_Y=$(($(stty size | awk '{print $1}')-6))
wget https://downloads.homegear.eu/nightlies/homegear_${builddate}_${system}_${arch}.deb 2>&1 | dialog --backtitle "$backtitle" --progressbox "Downloading Homegear..." $TTY_Y $TTY_X
[[ ${PIPESTATUS[0]} -ne 0 ]] && exit 1

TTY_X=$(($(stty size | awk '{print $2}')-6))
TTY_Y=$(($(stty size | awk '{print $1}')-6))
wget https://downloads.homegear.eu/nightlies/python3-homegear_${builddate}_${system}_${arch}.deb 2>&1 | dialog --backtitle "$backtitle" --progressbox "Downloading Python3 Homegear module..." $TTY_Y $TTY_X
[[ ${PIPESTATUS[0]} -ne 0 ]] && exit 1

TTY_X=$(($(stty size | awk '{print $2}')-6))
TTY_Y=$(($(stty size | awk '{print $1}')-6))
wget https://downloads.homegear.eu/nightlies/homegear-nodes-core_${builddate}_${system}_${arch}.deb 2>&1 | dialog --backtitle "$backtitle" --progressbox "Downloading core nodes..." $TTY_Y $TTY_X
[[ ${PIPESTATUS[0]} -ne 0 ]] && exit 1

TTY_X=$(($(stty size | awk '{print $2}')-6))
TTY_Y=$(($(stty size | awk '{print $1}')-6))
wget https://downloads.homegear.eu/nightlies/homegear-nodes-ui_${builddate}_${system}_${arch}.deb 2>&1 | dialog --backtitle "$backtitle" --progressbox "Downloading UI nodes..." $TTY_Y $TTY_X
#[[ ${PIPESTATUS[0]} -ne 0 ]] && exit 1

TTY_X=$(($(stty size | awk '{print $2}')-6))
TTY_Y=$(($(stty size | awk '{print $1}')-6))
wget https://downloads.homegear.eu/nightlies/homegear-nodes-extra_${builddate}_${system}_${arch}.deb 2>&1 | dialog --backtitle "$backtitle" --progressbox "Downloading extra nodes..." $TTY_Y $TTY_X
[[ ${PIPESTATUS[0]} -ne 0 ]] && exit 1

TTY_X=$(($(stty size | awk '{print $2}')-6))
TTY_Y=$(($(stty size | awk '{print $1}')-6))
wget https://downloads.homegear.eu/nightlies/homegear-licensing_${builddate}_${system}_${arch}.deb 2>&1 | dialog --backtitle "$backtitle" --progressbox "Downloading Homegear licensing module..." $TTY_Y $TTY_X
[[ ${PIPESTATUS[0]} -ne 0 ]] && exit 1

if [[ -n $extraModules ]]; then
	TTY_X=$(($(stty size | awk '{print $2}')-6))
	TTY_Y=$(($(stty size | awk '{print $1}')-6))
	wget https://downloads.homegear.eu/nightlies/homegear-easy-licensing_${builddate}_${system}_${arch}.deb 2>&1 | dialog --backtitle "$backtitle" --progressbox "Downloading Homegear EASY licensing module..." $TTY_Y $TTY_X
	[[ ${PIPESTATUS[0]} -ne 0 ]] && exit 1
fi

downloadModule 01 homegear-homematicbidcos_${builddate}_${system}_${arch}.deb
downloadModule 02 homegear-homematicwired_${builddate}_${system}_${arch}.deb
downloadModule 03 homegear-insteon_${builddate}_${system}_${arch}.deb
downloadModule 04 homegear-max_${builddate}_${system}_${arch}.deb
downloadModule 05 homegear-philipshue_${builddate}_${system}_${arch}.deb
downloadModule 06 homegear-sonos_${builddate}_${system}_${arch}.deb
downloadModule 09 homegear-kodi_${builddate}_${system}_${arch}.deb
downloadModule 10 homegear-ipcam_${builddate}_${system}_${arch}.deb
downloadModule 11 homegear-beckhoff_${builddate}_${system}_${arch}.deb
downloadModule 14 homegear-knx_${builddate}_${system}_${arch}.deb
downloadModule 15 homegear-enocean_${builddate}_${system}_${arch}.deb
downloadModule 16 homegear-intertechno_${builddate}_${system}_${arch}.deb
downloadModule 17 homegear-zwave_${builddate}_${system}_${arch}.deb
downloadModule 19 homegear-rs2w_${builddate}_${system}_${arch}.deb
downloadModule 20 homegear-rsl_${builddate}_${system}_${arch}.deb
downloadModule 22 homegear-nanoleaf_${builddate}_${system}_${arch}.deb
downloadModule 23 homegear-mbus_${builddate}_${system}_${arch}.deb
downloadModule 24 homegear-ccu_${builddate}_${system}_${arch}.deb
downloadModule 26 homegear-zigbee_${builddate}_${system}_${arch}.deb
downloadModule 27 homegear-velux-klf200_${builddate}_${system}_${arch}.deb
downloadModule 28 homegear-abi_${builddate}_${system}_${arch}.deb

downloadModule 07 homegear-easycam_${builddate}_${system}_${arch}.deb
downloadModule 08 homegear-easyled_${builddate}_${system}_${arch}.deb
downloadModule 18 homegear-easyled2_${builddate}_${system}_${arch}.deb
downloadModule 93 binrpc_${builddate}_${system}_${arch}.deb
downloadModule 94 homegear-dc-connector_${builddate}_${system}_${arch}.deb
downloadModule 91 homegear-cloudconnect_${builddate}_${system}_${arch}.deb
downloadModule 92 ibs-ssh_${builddate}_${system}_${arch}.deb

downloadModule 95 homegear-ui_${builddate}_${system}_${arch}.deb
downloadModule 96 homegear-webssh_${builddate}_${system}_${arch}.deb
downloadModule 97 homegear-management_${builddate}_${system}_${arch}.deb
downloadModule 98 homegear-adminui_${builddate}_${system}_${arch}.deb
downloadModule 99 homegear-influxdb_${builddate}_${system}_${arch}.deb

TTY_X=$(($(stty size | awk '{print $2}')-6))
TTY_Y=$(($(stty size | awk '{print $1}')-6))
dpkg --force-confdef --force-confold -i libhomegear-base_${builddate}_${system}_${arch}.deb 2>&1 | dialog --backtitle "$backtitle" --progressbox "Installing base library..." $TTY_Y $TTY_X
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
	apt-get -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" -y -f install | dialog --backtitle "$backtitle" --progressbox "Installing dependencies..." $TTY_Y $TTY_X
	[[ ${PIPESTATUS[0]} -ne 0 ]] && exit 1
	dpkg --force-confdef --force-confold -i libhomegear-base_${builddate}_${system}_${arch}.deb 2>&1 | dialog --backtitle "$backtitle" --progressbox "Installing base library..." $TTY_Y $TTY_X
	[[ ${PIPESTATUS[0]} -ne 0 ]] && exit 1
fi

TTY_X=$(($(stty size | awk '{print $2}')-6))
TTY_Y=$(($(stty size | awk '{print $1}')-6))
dpkg --force-confdef --force-confold -i libhomegear-node_${builddate}_${system}_${arch}.deb 2>&1 | dialog --backtitle "$backtitle" --progressbox "Installing node library..." $TTY_Y $TTY_X
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
	apt-get -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" -y -f install | dialog --backtitle "$backtitle" --progressbox "Installing dependencies..." $TTY_Y $TTY_X
	[[ ${PIPESTATUS[0]} -ne 0 ]] && exit 1
	dpkg --force-confdef --force-confold -i libhomegear-node_${builddate}_${system}_${arch}.deb 2>&1 | dialog --backtitle "$backtitle" --progressbox "Installing node library..." $TTY_Y $TTY_X
	[[ ${PIPESTATUS[0]} -ne 0 ]] && exit 1
fi

TTY_X=$(($(stty size | awk '{print $2}')-6))
TTY_Y=$(($(stty size | awk '{print $1}')-6))
dpkg --force-confdef --force-confold -i libhomegear-ipc_${builddate}_${system}_${arch}.deb 2>&1 | dialog --backtitle "$backtitle" --progressbox "Installing IPC library..." $TTY_Y $TTY_X
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
	apt-get -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" -y -f install | dialog --backtitle "$backtitle" --progressbox "Installing dependencies..." $TTY_Y $TTY_X
	[[ ${PIPESTATUS[0]} -ne 0 ]] && exit 1
	dpkg --force-confdef --force-confold -i libhomegear-ipc_${builddate}_${system}_${arch}.deb 2>&1 | dialog --backtitle "$backtitle" --progressbox "Installing IPC library..." $TTY_Y $TTY_X
	[[ ${PIPESTATUS[0]} -ne 0 ]] && exit 1
fi

TTY_X=$(($(stty size | awk '{print $2}')-6))
TTY_Y=$(($(stty size | awk '{print $1}')-6))
dpkg --force-confdef --force-confold -i homegear_${builddate}_${system}_${arch}.deb 2>&1 | dialog --backtitle "$backtitle" --progressbox "Installing Homegear..." $TTY_Y $TTY_X
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
	apt-get -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" -y -f install | dialog --backtitle "$backtitle" --progressbox "Installing dependencies..." $TTY_Y $TTY_X
	[[ ${PIPESTATUS[0]} -ne 0 ]] && exit 1
	dpkg --force-confdef --force-confold -i homegear_${builddate}_${system}_${arch}.deb 2>&1 | dialog --backtitle "$backtitle" --progressbox "Installing Homegear..." $TTY_Y $TTY_X
	[[ ${PIPESTATUS[0]} -ne 0 ]] && exit 1
fi

TTY_X=$(($(stty size | awk '{print $2}')-6))
TTY_Y=$(($(stty size | awk '{print $1}')-6))
dpkg --force-confdef --force-confold -i python3-homegear_${builddate}_${system}_${arch}.deb 2>&1 | dialog --backtitle "$backtitle" --progressbox "Installing Python3 Homegear module..." $TTY_Y $TTY_X
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
	apt-get -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" -y -f install | dialog --backtitle "$backtitle" --progressbox "Installing dependencies..." $TTY_Y $TTY_X
	[[ ${PIPESTATUS[0]} -ne 0 ]] && exit 1
	dpkg --force-confdef --force-confold -i python3-homegear_${builddate}_${system}_${arch}.deb 2>&1 | dialog --backtitle "$backtitle" --progressbox "Installing Python3 Homegear module..." $TTY_Y $TTY_X
	[[ ${PIPESTATUS[0]} -ne 0 ]] && exit 1
fi

TTY_X=$(($(stty size | awk '{print $2}')-6))
TTY_Y=$(($(stty size | awk '{print $1}')-6))
dpkg --force-confdef --force-confold -i homegear-nodes-core_${builddate}_${system}_${arch}.deb 2>&1 | dialog --backtitle "$backtitle" --progressbox "Installing core nodes..." $TTY_Y $TTY_X
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
	apt-get -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" -y -f install | dialog --backtitle "$backtitle" --progressbox "Installing dependencies..." $TTY_Y $TTY_X
	[[ ${PIPESTATUS[0]} -ne 0 ]] && exit 1
	dpkg --force-confdef --force-confold -i homegear-nodes-core_${builddate}_${system}_${arch}.deb 2>&1 | dialog --backtitle "$backtitle" --progressbox "Installing core nodes..." $TTY_Y $TTY_X
	[[ ${PIPESTATUS[0]} -ne 0 ]] && exit 1
fi

TTY_X=$(($(stty size | awk '{print $2}')-6))
TTY_Y=$(($(stty size | awk '{print $1}')-6))
dpkg --force-confdef --force-confold -i homegear-nodes-ui_${builddate}_${system}_${arch}.deb 2>&1 | dialog --backtitle "$backtitle" --progressbox "Installing UI nodes..." $TTY_Y $TTY_X
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
	apt-get -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" -y -f install | dialog --backtitle "$backtitle" --progressbox "Installing dependencies..." $TTY_Y $TTY_X
	#[[ ${PIPESTATUS[0]} -ne 0 ]] && exit 1
	dpkg --force-confdef --force-confold -i homegear-nodes-ui_${builddate}_${system}_${arch}.deb 2>&1 | dialog --backtitle "$backtitle" --progressbox "Installing UI nodes..." $TTY_Y $TTY_X
	#[[ ${PIPESTATUS[0]} -ne 0 ]] && exit 1
fi

TTY_X=$(($(stty size | awk '{print $2}')-6))
TTY_Y=$(($(stty size | awk '{print $1}')-6))
dpkg --force-confdef --force-confold -i homegear-nodes-extra_${builddate}_${system}_${arch}.deb 2>&1 | dialog --backtitle "$backtitle" --progressbox "Installing extra nodes..." $TTY_Y $TTY_X
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
	apt-get -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" -y -f install | dialog --backtitle "$backtitle" --progressbox "Installing dependencies..." $TTY_Y $TTY_X
	[[ ${PIPESTATUS[0]} -ne 0 ]] && exit 1
	dpkg --force-confdef --force-confold -i homegear-nodes-extra_${builddate}_${system}_${arch}.deb 2>&1 | dialog --backtitle "$backtitle" --progressbox "Installing extra nodes..." $TTY_Y $TTY_X
	[[ ${PIPESTATUS[0]} -ne 0 ]] && exit 1
fi

TTY_X=$(($(stty size | awk '{print $2}')-6))
TTY_Y=$(($(stty size | awk '{print $1}')-6))
dpkg --force-confdef --force-confold -i homegear-licensing_${builddate}_${system}_${arch}.deb 2>&1 | dialog --backtitle "$backtitle" --progressbox "Installing Homegear licensing module..." $TTY_Y $TTY_X
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
	apt-get -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" -y -f install | dialog --backtitle "$backtitle" --progressbox "Installing dependencies..." $TTY_Y $TTY_X
	[[ ${PIPESTATUS[0]} -ne 0 ]] && exit 1
	dpkg --force-confdef --force-confold -i homegear-licensing_${builddate}_${system}_${arch}.deb 2>&1 | dialog --backtitle "$backtitle" --progressbox "Installing Homegear licensing module..." $TTY_Y $TTY_X
	[[ ${PIPESTATUS[0]} -ne 0 ]] && exit 1
fi

if [[ -n $extraModules ]]; then
	TTY_X=$(($(stty size | awk '{print $2}')-6))
	TTY_Y=$(($(stty size | awk '{print $1}')-6))
	dpkg --force-confdef --force-confold -i homegear-easy-licensing_${builddate}_${system}_${arch}.deb 2>&1 | dialog --backtitle "$backtitle" --progressbox "Installing Homegear EASY licensing module..." $TTY_Y $TTY_X
	if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
		apt-get -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" -y -f install | dialog --backtitle "$backtitle" --progressbox "Installing dependencies..." $TTY_Y $TTY_X
		[[ ${PIPESTATUS[0]} -ne 0 ]] && exit 1
		dpkg --force-confdef --force-confold -i homegear-easy-licensing_${builddate}_${system}_${arch}.deb 2>&1 | dialog --backtitle "$backtitle" --progressbox "Installing Homegear EASY licensing module..." $TTY_Y $TTY_X
		[[ ${PIPESTATUS[0]} -ne 0 ]] && exit 1
	fi
fi

installModule 01 homegear-homematicbidcos_${builddate}_${system}_${arch}.deb
installModule 02 homegear-homematicwired_${builddate}_${system}_${arch}.deb
installModule 03 homegear-insteon_${builddate}_${system}_${arch}.deb
installModule 04 homegear-max_${builddate}_${system}_${arch}.deb
installModule 05 homegear-philipshue_${builddate}_${system}_${arch}.deb
installModule 06 homegear-sonos_${builddate}_${system}_${arch}.deb
installModule 09 homegear-kodi_${builddate}_${system}_${arch}.deb
installModule 10 homegear-ipcam_${builddate}_${system}_${arch}.deb
installModule 11 homegear-beckhoff_${builddate}_${system}_${arch}.deb
installModule 14 homegear-knx_${builddate}_${system}_${arch}.deb
installModule 15 homegear-enocean_${builddate}_${system}_${arch}.deb
installModule 16 homegear-intertechno_${builddate}_${system}_${arch}.deb
installModule 17 homegear-zwave_${builddate}_${system}_${arch}.deb
installModule 19 homegear-rs2w_${builddate}_${system}_${arch}.deb
installModule 20 homegear-rsl_${builddate}_${system}_${arch}.deb
installModule 22 homegear-nanoleaf_${builddate}_${system}_${arch}.deb
installModule 23 homegear-mbus_${builddate}_${system}_${arch}.deb
installModule 24 homegear-ccu_${builddate}_${system}_${arch}.deb
installModule 26 homegear-zigbee_${builddate}_${system}_${arch}.deb
installModule 27 homegear-velux-klf200_${builddate}_${system}_${arch}.deb
installModule 28 homegear-abi_${builddate}_${system}_${arch}.deb

installModule 07 homegear-easycam_${builddate}_${system}_${arch}.deb
installModule 08 homegear-easyled_${builddate}_${system}_${arch}.deb
installModule 18 homegear-easyled2_${builddate}_${system}_${arch}.deb
installModule 93 binrpc_${builddate}_${system}_${arch}.deb
installModule 94 homegear-dc-connector_${builddate}_${system}_${arch}.deb
installModule 91 homegear-cloudconnect_${builddate}_${system}_${arch}.deb
installModule 92 ibs-ssh_${builddate}_${system}_${arch}.deb

installModuleOverwriteConfig 95 homegear-ui_${builddate}_${system}_${arch}.deb
installModule 96 homegear-webssh_${builddate}_${system}_${arch}.deb
installModule 97 homegear-management_${builddate}_${system}_${arch}.deb
installModule 98 homegear-adminui_${builddate}_${system}_${arch}.deb
installModule 99 homegear-influxdb_${builddate}_${system}_${arch}.deb

if test -f /etc/homegear/management.conf; then
	sed -i "s/repositoryType = stable/repositoryType = nightly/g" /etc/homegear/management.conf
fi

cd $SCRIPTDIR
rm -Rf $TEMPDIR
rm -f InstallNightly.sh

service homegear stop
if [[ "$modules" == *"96"* ]]; then
	systemctl enable homegear-webssh
fi
if [[ "$modules" == *"97"* ]]; then
	systemctl enable homegear-management
fi
if [[ "$modules" == *"99"* ]]; then
	systemctl enable homegear-influxdb
fi
if [[ "$modules" == *"94"* ]]; then
	systemctl enable homegear-dc-connector
fi
if [[ "$modules" == *"91"* ]]; then
	systemctl enable homegear-cloudconnect
fi
sync
sleep 5
[[ $readonlyRoot -ne 0 ]] && mount -o remount,ro /
service homegear start
if [[ "$modules" == *"96"* ]]; then
	service homegear-webssh start
fi
if [[ "$modules" == *"97"* ]]; then
	service homegear-management start
fi
if [[ "$modules" == *"99"* ]]; then
	service homegear-influxdb start
fi
if [[ "$modules" == *"94"* ]]; then
	service homegear-dc-connector start
fi
if [[ "$modules" == *"91"* ]]; then
	service homegear-cloudconnect start
fi
