#!/bin/bash

# check only one instan
pidof -o %PPID -x $0 >/dev/null && echo "Script is already running" && exit 1

# get license and proxy settings
while getopts l:p: flag
do 
	case "${flag}" in
		l) license=${OPTARG};;
		p) proxy=${OPTARG};;
	esac
done

# general parameters
microsoftProdPackages="packages-microsoft-prod.deb";
heimdalZipFileName="heimdalInstaller.zip";
heimdalZipAddress="https://heimdalqastorage.blob.core.windows.net/setup-linux/$heimdalZipFileName";
destination="/tmp/heimdal/";
heimdalFolderName="heimdal";
heimdalServiceConfName="heimdal-clienthost.service";
heimdalServiceName="heimdal-clienthost";
installDotnetFileName="dotnet-install.sh";
dotnetVersionToInstall="6.0";

# parameters for installing libssl 1.1 if the version is 22.04 for ubuntu
version22="22.04";
amdArchitecture="amd64";
ubuntu_version=$(lsb_release -rs);
ubuntu_architecture=$(dpkg-architecture -q DEB_BUILD_ARCH);
libssl_i386_url="http://archive.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.1_1.1.1f-1ubuntu2_i386.deb";
libssl_i386_name="libssl1.1_1.1.1f-1ubuntu2_i386.deb";
libssl_amd64_url="http://archive.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.1_1.1.1f-1ubuntu2_amd64.deb";
libssl_amd64_name="libssl1.1_1.1.1f-1ubuntu2_amd64.deb";

check_if_dotnet_installed(){
	chmod +x ./$installDotnetFileName
	./$installDotnetFileName -c $dotnetVersionToInstall --install-dir /usr/share/heimdal-utils --no-path
}

remove_file_if_exists(){
	if test -f "$1"; then
		rm -rf "$1" && echo "Deleted $1";
	fi
}

cleanup(){
	echo "cleanup old install leftovers"
		
    remove_file_if_exists $microsoftProdPackages;
	remove_file_if_exists $heimdalZipFileName;
	remove_file_if_exists $heimdalServiceConfName;
	remove_file_if_exists $heimdalServiceName;
	remove_file_if_exists $installDotnetFileName;
	
	rm -r $heimdalFolderName;
}

check_internet_connection(){
	if nc -zw1 "heimdalsecurity.com" 443; then
		echo "";
	else 
		echo "Failed to connect to the internet."
		exit 1;
	fi
		
}

check_if_sudo_access(){
	if [ $EUID > 0 ];
		then echo "Please run as root"
		exit 1
	fi
}

check_ubuntu_version(){
	apt install unzip

	if [ "$ubuntu_version" = "$version22" ]; then
		apt install dpkg-dev -y
		
		ubuntu_architecture=$(dpkg-architecture -q DEB_BUILD_ARCH);
		
		if [ "$ubuntu_architecture" = "$amdArchitecture" ]; then
			wget $libssl_amd64_url
			dpkg -i $libssl_amd64_name
		else 
			wget $libssl_i386_url
			dpkg -i $libssl_i386_name
		fi
	fi
}

before_install_event(){
	cleanup;
	
	check_internet_connection;
	check_if_sudo_access;
	check_ubuntu_version;
	
	echo "Installing Heimdal Services.";
}

install_prerequisites(){
	check_if_dotnet_installed;
}

after_install_event(){
	cleanup;
	
	echo "Install finished"
}

download_and_extract_zip(){
	wget $heimdalZipAddress
	
	if test -f "$heimdalZipFileName"; then
		echo "Downloaded installer with success"
	else
		echo "Failed to download heimdal installer"
		exit 1;
	fi
	
	unzip $heimdalZipFileName;
}

run_installer(){
	echo "running installer"
	
	cp -p $heimdalServiceConfName "/etc/systemd/system/";
	cp -p -r $heimdalFolderName "/usr/sbin/";
	chmod -R 0755 "/usr/sbin/heimdal";
	
	if [ -n "$license" ]; then
		echo $license > "/usr/sbin/heimdal/license.key";
	fi

	if [ -n "$proxy" ]; then
		echo $proxy > "/usr/sbin/heimdal/proxy.config";
	fi

	systemctl daemon-reload;
	systemctl enable $heimdalServiceConfName;
	systemctl start $heimdalServiceConfName;
}

install_heimdal(){
	download_and_extract_zip;
	check_if_dotnet_installed;
	run_installer
}

before_install_event; #check internet connection, check sudo, check single instance
install_heimdal # download installer and run
after_install_event # cleanup
