#!/bin/bash
# This file is part of the Network Performance Monitor which is released under the GNU General Public License v3.0
# See the file LICENSE for full license details.

if [[ ! -f /opt/netperf/scriptutils.sh ]]; then
	printf "Can't find a required file.\nPlease copy the project files to /opt/netperf before running this script.\ne.g.:\nsudo cp -R network_performance_monitor/netperf /opt/netperf\n\n"
	exit 1
fi

# check that the script is being run as root
if [[ $EUID -ne 0 ]]; then
	echo "This script must be run as root, e.g:"
	echo "sudo ./$(basename $0)"
	exit 1
fi

source /opt/netperf/scriptutils.sh

raspbian_os_packages=( sqlite3 python3-daemon python3-numpy python3-matplotlib iperf3 \
                       dnsutils texlive-latex-base texlive-latex-extra bc nginx gunicorn rabbitmq-server python3-pip)

debian_os_packages=( sqlite3 python3-daemon python3-numpy python3-matplotlib iperf3 dnsutils texlive-latex-base texlive-latex-extra \
                     iw wpasupplicant bc nginx gunicorn rabbitmq-server python3-pip )

centos_os_packages=( epel-release gcc sqlite python3-daemon python3-matplotlib python36-devel iperf3 \
                     bind-utils iw wpa_supplicant bc nginx python3-gunicorn rabbitmq-server python3-pip libqhull texlive-latex \
                     texlive-collection-latexrecommended texlive-titlesec )

fedora_os_packages=( gcc sqlite python3-daemon python3-matplotlib python3-devel iperf3 \
                     bind-utils iw wpa_supplicant bc nginx python3-gunicorn rabbitmq-server python3-pip libqhull texlive-latex \
                     texlive-collection-latexrecommended texlive-titlesec newt cronie cronie-anacron )

pip_packages=( posix_ipc flask flask-socketio requests kombu celery )

printf "\nInstalling software packages required for the Network Performance Monitor project.\n\n"

os_id=$( get_os_id )
printf "System type: $os_id\n\n"

if [[ "$os_id" == "centos" ]]; then
	# install 'newt' for the whiptail command used by this script
	install_os_package newt > /dev/null
	# enable PowerTools repository for libqhull (required for python3-matplotlib on CentOS systems)
	printf "Installing dnf-plugins-core...\n"
	install_os_package dnf-plugins-core > /dev/null
	printf "Enabling PowerTools repository...\n"
	dnf config-manager --set-enabled PowerTools
	os_packages=("${centos_os_packages[@]}")
	# check if SELinux is enforced on this system
	sel_enforced=$( selinux_enforced )
	if [[ "$sel_enforced" == true ]]; then
		# add package that contains the 'semanage' tool
		os_packages+=( policycoreutils-python-utils )
	fi
else
	if [[ "$os_id" == "fedora" ]]; then
		# install 'newt' for the whiptail command used by this script
		install_os_package newt > /dev/null
		os_packages=("${fedora_os_packages[@]}")
		sel_enforced=$( selinux_enforced )
		if [[ "$sel_enforced" == true ]]; then
			# add package that contains the 'semanage' tool
			os_packages+=( policycoreutils-python-utils )
		fi
	else
		if [[ "$os_id" == "raspbian" ]]; then
			os_packages=("${raspbian_os_packages[@]}")
		else
                        if [[ "$os_id" == "debian" ]]; then
                                os_packages=("${debian_os_packages[@]}")
                        else
                                printf "Unsupported operating system: $os_od\n"
                                exit 1
                        fi
		fi
	fi
fi

result=$( whiptail --title "Speedtest client selection" --menu --nocancel \
"The system performs periodic Internet speed tests against Ookla speedtest.net servers. \
There are two speedtest clients available for performing these tests:\n\n\
speedtest-cli: an open source speedtest client that may report inaccurate speeds on faster Internet connections\n\n\
Ookla Speedtest CLI: a closed source proprietary client that reports accurate speeds on faster Internet connections\n\n\
Which client do you want to install?\n\n
" 20 80 2 \
"speedtest-cli" " open source client" \
"Ookla Speedtest CLI" " closed source proprietary client" 3>&1 1>&2 2>&3 )

if [[ "$result" == "speedtest-cli" ]]; then
	# uninstall Ookla client, add speedtest-cli to the package list
	package_installed=$( os_package_installed speedtest )
	if [[ "$package_installed" == true ]]; then
		printf "Removing Ookla Speedtest CLI client...\n"
		remove_os_package speedtest
	fi
	pip_packages+=( speedtest-cli )
else
	# uninstall speedtest-cli, add Ookla client + dependencies to the package list
	package_installed=$( pip_package_installed speedtest-cli )
	if [[ "$package_installed" == true ]]; then
		printf "Removing speedtest-cli client...\n"
		remove_pip_package speedtest-cli > /dev/null
	fi

	# Due to changes in how Ookla distributes its command line tool it must be installed manually
	# before running this package installer script. Refer to the project wiki for instructions:
	# https://github.com/mr-canoehead/network_performance_monitor/wiki/Install-project-files-and-required-software-packages

	command -v speedtest > /dev/null
	if [[ "$?" -ne 0 ]]; then
		printf "The Ookla speedtest client must be installed manually prior to running this script.\n"
		printf "Refer to the project wiki for instructions:\n"
		printf "https://github.com/mr-canoehead/network_performance_monitor/wiki/Install-project-files-and-required-software-packages"
		exit 1
	fi

	#if [[ "$os_id" == "centos" || "$os_id" == "fedora" ]]; then
	#	printf "Adding Ookla repository...\n"
	#	package_installed=$( os_package_installed wget )
	#	if [[ "$package_installed" == false ]]; then
	#		printf "Installing wget...\n"
	#		install_os_package wget > /dev/null
	#	fi
	#	wget https://bintray.com/ookla/rhel/rpm -O bintray-ookla-rhel.repo > /dev/null 2>&1
	#	mv bintray-ookla-rhel.repo /etc/yum.repos.d/ > /dev/null 2>&1
	#	os_packages+=( speedtest )
	#else
	#	if [[ "$os_id" == "raspbian" || "$os_id" == "debian" ]]; then
	#		printf "Adding Ookla repository...\n"
	#		INSTALL_KEY=379CE192D401AB61
	#		DEB_DISTRO=$(lsb_release -sc)
	#		install_os_package gnupg1 > /dev/null
	#		sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys "$INSTALL_KEY"
	#		echo "deb https://ookla.bintray.com/debian ${DEB_DISTRO} main" | sudo tee  /etc/apt/sources.list.d/speedtest.list > /dev/null
	#		os_packages+=( apt-transport-https dirmngr speedtest)
	#	fi
	#fi
fi

printf "Updating repository cache...\n"
update_repository_cache

all_packages_installed=true

printf "Installing required packages...\n"
for os_package in "${os_packages[@]}"
do
	# test if package is already installed
	result=$( os_package_installed "$os_package" )
	if [[ "$result" == true ]]; then
		printf "	$os_package is installed\n"
	else
		#try 3 times to install the package (sometimes Raspbian mirrors can be unreliable)
		package_installed=false
		for i in {1..3}
		do
			printf "	Installing package $os_package... "
			install_os_package "$os_package" > /dev/null
			# check that the package is now installed
			result=$( os_package_installed "$os_package" )
			if [[ "$result" == true ]]; then
				printf " done.\n"
				package_installed=true
				break
			fi
		done
		if [[ "$package_installed" == "false" ]]; then
			printf " error, installation failed.\n"
			all_packages_installed=false
		fi
	fi
done

for pip_package in "${pip_packages[@]}"
do
	# test if package is already installed
	installed=$( pip_package_installed "$pip_package" )
	if [[ "$installed" == true ]]; then
		echo "	$pip_package is installed"
	else
		#try 3 times to install the package (sometimes Raspbian mirrors can be unreliable)
		package_installed=false
		for i in {1..3}
		do
			printf "	Installing pip package $pip_package..."
			install_pip_package "$pip_package" > /dev/null
			# check that the package is now installed
			installed=$( pip_package_installed "$pip_package" )
			if [[ "$installed" == true ]]; then
				printf " done.\n"
				package_installed=true
				break
			fi
		done
		if [[ "$package_installed" == "false" ]]; then
			echo "	error, installation failed.\n"
			all_packages_installed=false
		fi
	fi
done

if [[ "$all_packages_installed" == "false" ]]; then
	echo 
	echo "Error: One or more required packages failed to install. Check your Internet connection from this machine, or switch mirrors."
	echo "       Then run this script again."
else
	echo "All required packages were installed successfully."
fi

