#!/bin/bash
# This script installs the full range of packages for anynode - The Software SBC
# This version of script does not use the Debian/Ubuntu/Raspbian repositories...
#
# You may provide one commandline argument which specifies whether the configuration
# web-frontend will listen on a specific interface ( eg. eth0 ), otherwise it is
# only available via localhost.

an_install() {
	# make sure we are root...
	if [ "$(id -u)" != "0" ]; then
	    echo "--------> This script must be run as root" 1>&2
	    return 1
	fi

	ANYNODE_LDR=/opt/tesystems/anynode/bin/anynode-ldr
	ANYNODEMON_LDR=/opt/tesystems/anynodemon/bin/anynodemon-ldr
	ANYNODEAD_LDR=/opt/tesystems/anynodead/bin/anynodead-ldr
	ANYNODEFE_LDR=/opt/tesystems/anynodefe/bin/anynodefe-ldr

	# see if anynodefe is already installed & has a configured port & which version
	if [ -x "$(command -v $ANYNODEFE_LDR)" ]; then
		ANFE_VER=$($ANYNODEFE_LDR --runtime-version-information | sed -rn "s/productVersion: ([0-9]+\.[0-9]+\.[0-9]+)/\1/p")
	        ISWEBCONFIGURED=$($ANYNODEFE_LDR -s anynodefe http enum | grep "0 ->")
	        echo $ISWEBCONFIGURED
	fi

	# see if anynode is already installed & which version
	if [ -x "$(command -v $ANYNODE_LDR)" ]; then
		AN_VER=$($ANYNODE_LDR --runtime-version-information | sed -rn "s/productVersion: ([0-9]+\.[0-9]+\.[0-9]+)/\1/p")
	fi

	# see if anynodemon is already installed & which version
	if [ -x "$(command -v $ANYNODEMON_LDR)" ]; then
		ANMON_VER=$($ANYNODEMON_LDR --runtime-version-information | sed -rn "s/productVersion: ([0-9]+\.[0-9]+\.[0-9]+)/\1/p")
	fi

	# see if anynodead is already installed & which version
	if [ -x "$(command -v $ANYNODEAD_LDR)" ]; then
		ANAD_VER=$($ANYNODEAD_LDR --runtime-version-information | sed -rn "s/productVersion: ([0-9]+\.[0-9]+\.[0-9]+)/\1/p")
	fi
	

	# if not we need to specify the port where the frontend will be listening for web-access
	if ! [ -n "$ISWEBCONFIGURED" ]; then

		if [ "$1" == "" ]; then
			echo "--------> You need to specify one of the following network-interfaces:"
			basename -a /sys/class/net/*
			return 1
		fi

		# check if specified interface exists and has an IPv4 or v6 address
		if [ "$1" != "" ]; then
			IPV4=$(ip addr list $1 2>/dev/null |grep "inet " |cut -d' ' -f6|cut -d/ -f1)
			IPV6=$(ip addr list $1 2>/dev/null |grep "inet6 " |cut -d' ' -f6|cut -d/ -f1)

			if [[ -z $IPV4 && -z $IPV6 ]]; then
				echo "--------> The specified interface does not exist or is not bound to any IP v4 or v6 address" 1>&2
				return 1
			fi
		fi
	fi

	TE_BASE_PATH="https://linux.te-systems.de/beta"

	# check OS & pre-requisites
	DEBIAN_VERNUM=$(cat /etc/debian_version)
	ISRASPBIAN=$(lsb_release -a 2>/dev/null | grep "Raspbian" | grep "jessie")
	ISRASPBIAN9=$(lsb_release -a 2>/dev/null | grep "Raspbian" | grep "stretch")
	ISRASPBIAN10=$(lsb_release -a 2>/dev/null | grep "Raspbian" | grep "buster")
	ISRASPBIAN11=$(lsb_release -a 2>/dev/null | grep "Raspbian" | grep "bullseye")
	ISOLDRPI=$(cat /proc/cpuinfo 2>/dev/null | grep "BCM2708")
	ISOLDINNO=$(cat /proc/cpuinfo 2>/dev/null | grep "IP6010")
	ISNEWINNO=$(cat /proc/cpuinfo 2>/dev/null | grep "IPx11")
	ISARMV6=$(cat /proc/cpuinfo 2>/dev/null | grep "(v6")
	ISARMV7=$(cat /proc/cpuinfo 2>/dev/null | grep "(v7")
	ISARMEL=$(dpkg --print-architecture | grep "armel")
	ISARMHF=$(dpkg --print-architecture | grep "armhf")
	ISAMD64=$(dpkg --print-architecture | grep "amd64")
	ISARM64=$(dpkg --print-architecture | grep "arm64")
	EMBEDDEDJRE=$(find /opt/tesystems/anynodefe/jre -name 'libjvm.so' 2>/dev/null)

	# check OS-version
	if [[ $DEBIAN_VERNUM == 12.* ]]; then
		if [ -n "$ISAMD64" ]; then
			TE_REL_URI="$TE_BASE_PATH/deb12/"
			TE_DIST="debian12"
			TE_ARCH="x64"
			echo "--------> installing on Debian 12 (Bookworm) amd64"
		else
			echo "--------> anynode not supported on Debian 12 (Bookworm) for this cpu platform"
			return 1
		fi
	elif [[ $DEBIAN_VERNUM == 11.* ]]; then
		if [ -n "$ISRASPBIAN11" ]; then
			if [ -n "$ISOLDRPI" ]; then
				echo "-------->anynode can only be installed on Raspberry Pi v2 and above" 1>&2
				return 1
			fi
			TE_REL_URI="$TE_BASE_PATH/raspbian11/"
			TE_ARCH="rpi2"
			TE_DIST="debian11"
			echo "--------> installing on Raspbian (Bullseye)"	
		elif [ -n "$ISAMD64" ]; then
			TE_REL_URI="$TE_BASE_PATH/deb11/"
			TE_ARCH="x64"
			TE_DIST="debian11"
			echo "--------> installing on Debian 11 (Bullseye) amd64"
		else
			echo "--------> anynode not supported on Debian 11 (Bullseye) for this cpu platform"
			return 1
		fi
	elif [[ $DEBIAN_VERNUM == bullseye* ]]; then
		if [ -n "$ISAMD64" ]; then
			TE_REL_URI="$TE_BASE_PATH/ub2004/"
			TE_ARCH="x64"
			TE_DIST="ubuntu2004"
			echo "--------> installing on Ubuntu 20.04 amd64"
		else
			echo "--------> anynode not supported on Ubuntu 20.04 for this cpu platform"
			return 1
		fi
	elif [[ $DEBIAN_VERNUM == bookworm* ]]; then
		if [ -n "$ISAMD64" ]; then
			TE_REL_URI="$TE_BASE_PATH/ub2204/"
			TE_ARCH="x64"
			TE_DIST="ubuntu2204"
			echo "--------> installing on Ubuntu 22.04 amd64"
		else
			echo "--------> anynode not supported on Ubuntu 22.04 for this cpu platform"
			return 1
		fi
	elif [[ $DEBIAN_VERNUM == trixie* ]]; then
		if [ -n "$ISAMD64" ]; then
			TE_REL_URI="$TE_BASE_PATH/ub2404/"
			TE_ARCH="x64"
			TE_DIST="ubuntu2404"
			echo "--------> installing on Ubuntu 24.04 amd64"
		else
			echo "--------> anynode not supported on Ubuntu 24.04 for this cpu platform"
			return 1
		fi
	else
		echo "--------> This script will only run on Debian 11 (Bullseye), Debian 12 (Bookworm), Raspbian for rPi v2/v3/v4, Ubuntu 20.04, Ubuntu 22.04 and Ubuntu 24.04" 1>&2
		return 1
	fi

	set -e

	echo "--------> make sure root certificates are installed on this system..."
	apt-get update
	apt-get --yes install ca-certificates

	# circumvent bug on debian12
	if [[ $TE_DIST == debian12 ]]; then
		echo "--------> circumvent a java install issue on debian 12..."
		apt-get --yes install ca-certificates-java
	fi

	# create temp-dir for downlading files
	TEMP_DIR=`mktemp -d`
	echo copying temporary files to: $TEMP_DIR
	trap "rm -rf \"$TEMP_DIR\"" EXIT
	pushd "$TEMP_DIR"

	# get directory listing for our distribution sub-directory
	wget $TE_REL_URI -O dirfile

	# parse dirfile for anynode specific url
	TE_REGEX="s/.*<a href=\"(anynode-.*-${TE_DIST}-${TE_ARCH}-release\.deb)\">.*/\1/p"
	ANYNODE_DEB=$(sed -rn "$TE_REGEX" dirfile)

	# check if anynode uri found in directory listing and if we need to install this version
	if [ -z "$ANYNODE_DEB" ]; then
		echo "--------> could not find anynode deb on server!" 1>&2
		return 1
	else
		if [ -n "$AN_VER" ]; then
			if [[ ${ANYNODE_DEB} != *${AN_VER}* ]];then
		        	AN_INST=1
			fi 
		else
			AN_INST=1
		fi
	fi

	# check if anynodead uri found in directory listing and if we need to install this version
	TE_REGEX="s/.*<a href=\"(anynodead-.*-${TE_DIST}.${TE_ARCH}-release\.deb)\">.*/\1/p"
	ANYNODEAD_DEB=$(sed -rn "$TE_REGEX" dirfile)
	if [ -z "$ANYNODEAD_DEB" ]; then
		echo "--------> could not find anynodead deb on server!" 1>&2
		return 1
	else
		if [ -n "$ANAD_VER" ]; then
			if [[ ${ANYNODEAD_DEB} != *${ANAD_VER}* ]];then
	 	       		ANAD_INST=1
			fi 
		else
			ANAD_INST=1
		fi
	fi

	# check if anynodemon uri found in directory listing and if we need to install this version
	TE_REGEX="s/.*<a href=\"(anynodemon-.*-${TE_DIST}-${TE_ARCH}-release\.deb)\">.*/\1/p"
	ANYNODEMON_DEB=$(sed -rn "$TE_REGEX" dirfile)
	if [ -z "$ANYNODEMON_DEB" ]; then
		echo "--------> could not find anynodemon deb on server!" 1>&2
		return 1
	else
		if [ -n "$ANMON_VER" ]; then
			if [[ ${ANYNODEMON_DEB} != *${ANMON_VER}* ]];then
		        	ANMON_INST=1
			fi 
		else
			ANMON_INST=1
		fi
	fi

	# check if anynodefe uri found in directory listing and if we need to install this version
	TE_REGEX="s/.*<a href=\"(anynodefe-.*-${TE_DIST}-${TE_ARCH}-release\.deb)\">.*/\1/p"
	ANYNODEFE_DEB=$(sed -rn "$TE_REGEX" dirfile)
	if [ -z "$ANYNODEFE_DEB" ]; then
		echo "--------> could not find anynodefe deb on server!" 1>&2
		return 1
	else
		if [ -n "$ANMON_VER" ]; then
			if [[ ${ANYNODEFE_DEB} != *${ANFE_VER}* ]];then
		        	ANFE_INST=1
			fi 
		else
			ANFE_INST=1
		fi
	
	fi

	# get deb-package from webserver if necessary
	if [ -n "$AN_INST" ]; then
		wget $TE_REL_URI$ANYNODE_DEB
	else
		echo "anynode $AN_VER already installed!"
	fi

	# get deb-package from webserver if necessary
	if [ -n "$ANAD_INST" ]; then
		wget $TE_REL_URI$ANYNODEAD_DEB
	else
		echo "anynodead $ANAD_VER already installed!"
	fi

	# get deb-package from webserver if necessary
	if [ -n "$ANMON_INST" ]; then
		wget $TE_REL_URI$ANYNODEMON_DEB
	else
		echo "anynodemon $ANMON_VER already installed!"
	fi

	# get deb-package from webserver if necessary
	if [ -n "$ANFE_INST" ]; then
		wget $TE_REL_URI$ANYNODEFE_DEB
	else
		echo "anynodefe $ANFE_VER already installed!"
	fi

	# install deb-package if necessary
	if [ -n "$AN_INST" ]; then
		apt-get install -y ./$ANYNODE_DEB
	fi

	# install deb-package if necessary
	if [ -n "$ANAD_INST" ]; then
		apt-get install -y ./$ANYNODEAD_DEB
	fi

	# install deb-package if necessary
	if [ -n "$ANMON_INST" ]; then
		apt-get install -y ./$ANYNODEMON_DEB
	fi

	# install deb-package if necessary
	if [ -n "$ANFE_INST" ]; then
		apt-get install -y ./$ANYNODEFE_DEB
	fi

	# If a parameter is given, we expect a network-interface name in it
	# and configure the web-configuration-frontend to also listen on this
	# interface, IP v4 takes precedence...
	if [ -n "$IPV4" ]; then
		echo "...setting up anynodefe connector..."
		systemctl stop anynodefe
		$ANYNODEFE_LDR -s anynodefe http reset || true
		$ANYNODEFE_LDR -s anynodefe http add --port=80 --interface=$1 --address-version=IN_ADDRESS_VERSION_4
		$ANYNODEFE_LDR -s anynodefe http add --port=443 --interface=$1 --address-version=IN_ADDRESS_VERSION_4 --secure
		systemctl start anynodefe
#		echo "--------> you may now connect to anynode's web-configuration unencrypted here: http://$IPV4 or via TLS here: https://$IPV4"
	elif [ -n "$IPV6" ]; then
		echo "...setting up anynodefe connector..."
		systemctl stop anynodefe
		$ANYNODEFE_LDR -s anynodefe http reset || true
		$ANYNODEFE_LDR -s anynodefe http add --port=80 --interface=$1 --address-version=IN_ADDRESS_VERSION_6
		$ANYNODEFE_LDR -s anynodefe http add --port=443 --interface=$1 --address-version=IN_ADDRESS_VERSION_6 --secure
		systemctl start anynodefe
#		echo "--------> you may now connect to anynode's web-configuration unencrypted here: http://[$IPV6] or via TLS here: https://[$IPV6]"
	elif [ -n "$ISWEBCONFIGURED" ]; then
		echo "--------> you may now connect to anynode's web-configuration here:"
		$ANYNODEFE_LDR -s anynodefe http enum
	else
		echo "--------> you may now connect to anynode's web-configuration here: http://localhost:8088"
	fi

	popd
}

an_install $1
