#!/bin/bash

MOUNTDIR=`dirname $0`

# Install the udev trigger
cat $MOUNTDIR/90-guest_iso.rules > /etc/udev/rules.d/90-guest_iso.rules
udevadm control --reload

# Install rpms
ARCH=`uname -m`

PKGMNGR=""
FALLBACK_PKGMNGR=""

# Ignore signals to be able to update guest tools even if guest service is restarted
# (this can break 'prlctl enter' connection but update process should not die in this case)
trap 'true' 1 15

function get_pkg_arch() {
	local format=$1

	if [ "x$ARCH" != "xx86_64" ]; then
		pkg_arch="i?86"
	else
		if [ $format = "rpm" ]; then
			pkg_arch="x86_64"
		else
			pkg_arch="amd64"
		fi
	fi

	echo $pkg_arch
}

function get_os_id()
{
	local id="unknown"
	local src="/etc/os-release"

	if [ -e "$src" ]; then
		id=`. $src 2>/dev/null && echo $ID`
	fi

	echo $id
}

function get_os_version()
{
	local ver="0"
	local src="/etc/os-release"

	if [ -e "$src" ]; then
		ver=`. $src 2>/dev/null && echo $VERSION_ID | sed 's/\.//g'`
	fi

	echo $ver
}

function try_install_pkg()
{
	local pkgs=$1

	$PKGMNGR $pkgs || $FALLBACK_PKGMNGR $pkgs
}

function install_packages()
{
	os_id=$(get_os_id)
	os_version=$(get_os_version)

	if [ "$os_id" = "fedora" ]; then
		PKGMNGR="dnf -y install"
		FALLBACK_PKGMNGR="rpm -Uvh"
		suffix="el7"
		format="rpm"
		if [ "$os_version" -ge "23" ]; then
			dkms_suffix="fc23"
		fi
	elif [ -f /etc/redhat-release ]; then
		PKGMNGR="yum -y install"
		FALLBACK_PKGMNGR="rpm -Uvh"
		format="rpm"
		if [ "$os_version" -eq "6" ]; then
			dkms_suffix="el6"
			suffix="el6"
		elif [ "$os_version" -eq "7" -o "$os_version" -eq "70" ]; then
			dkms_suffix="el7"
			suffix="el7"
		else
			echo "WARNING: Failed to detect system version on the basis of /etc/os-release"
			readlink /sbin/init 2>/dev/null | grep systemd > /dev/null 2>&1
			[ $? -eq 0 ] && suffix="el7" || suffix="el6"
			dkms_suffix=$suffix
		fi
	elif [ "$os_id" = "debian" -o "$os_id" = "ubuntu" -o -f /etc/debian_version ]; then
		DEBIAN_FRONTEND=noninteractive apt-get -y install gdebi
		PKGMNGR="gdebi -n"
		FALLBACK_PKGMNGR="dpkg -i"
		format="deb"
		suffix=""
		if [ "$os_version" -eq "7" ]; then
			dkms_suffix="debian7"
		elif [ "$os_version" -eq "8" ]; then
			dkms_suffix="debian8"
		elif [ "$os_version" -eq "1404" ]; then
			dkms_suffix="ubuntu14"
		elif [ "$os_version" -eq "1504" ]; then
			dkms_suffix="ubuntu1504"
		elif [ "$os_version" -eq "1510" ]; then
			dkms_suffix="ubuntu1510"
		elif [ "$os_version" -ge "1604" ]; then
			dkms_suffix="ubuntu16"
		fi
	elif [ "$os_id" = "opensuse" -o "$os_id" = "sles" -o -f /etc/SuSE-release ]; then
		PKGMNGR="zypper --non-interactive install"
		FALLBACK_PKGMNGR="rpm -Uvh"
		format="rpm"
		suffix="suse"
		if [ "$os_version" -ge "421" ]; then
			dkms_suffix="suse42.1"
		fi
	fi

	if [ x"$format" = "x" ]; then
		echo "Unsupported distribution! Nothing to install!" && return
	fi

	# Install dkms, if possible
	if [ x"$dkms_suffix" = "x" ]; then
		echo "DKMS installation is not supported for this system"
	else
		if [ $format != "rpm" ]; then
			for pkg in $MOUNTDIR/dkms/dkms*${dkms_suffix}* \
					$MOUNTDIR/dkms/vzvirtio*${dkms_suffix}*; do
				try_install_pkg "${pkg}"
			done
		else
			try_install_pkg "$MOUNTDIR/dkms/*${dkms_suffix}*"
		fi
		mkdir -p /etc/modprobe.d >/dev/null 2>&1
		echo "blacklist virtio_balloon" > /etc/modprobe.d/vzvirtio-backlist.conf
	fi

	# Install other packages
	arch=$(get_pkg_arch $format)
	echo "WARNING: Installing/updating guest tools service, connection to VM can be lost"
	if [ $format = "rpm" ]; then
		try_install_pkg "$MOUNTDIR/guest_pkgs/qemu-*.$suffix.$arch.rpm"
		try_install_pkg "$MOUNTDIR/guest_pkgs/prl_nettool*.$suffix.$arch.rpm"
		if [ $suffix = "suse" ]; then
			try_install_pkg "$MOUNTDIR/guest_pkgs/vz-guest-udev*suse.noarch.rpm"
		else
			try_install_pkg "$MOUNTDIR/guest_pkgs/vz-guest-udev*el.noarch.rpm"
		fi
	else
		for pkg in $MOUNTDIR/guest_pkgs/qemu*$arch.deb \
				$MOUNTDIR/guest_pkgs/prl-nettool*$arch.deb \
				$MOUNTDIR/guest_pkgs/vz-guest-udev*all.deb ; do
			try_install_pkg "${pkg}"
		done
	fi
}

function install_prl_backup() {
	install -m 755 $MOUNTDIR/prl_backup /usr/bin
	if [ $? -ne 0 ]; then
		echo "Failed to install prl_backup utility"
	else
		echo "Successfully installed prl_backup utility"
	fi
}

install_packages
install_prl_backup

echo "Done!"
