#!/bin/sh

set -e

PPA_URL=http://autoinstall.plesk.com/PPA_11.5.0
PPA_MOVING_GUIDE_URL=http://download1.parallels.net/ppa/11.5/docs/en-US/pdf/ppa_moving_to.pdf
PPA_OPERATIONS_GUIDE_URL=http://download1.parallels.com/ppa/11.5/docs/en-US/pdf/ppa_operations_guide.pdf
# reminder: change the list packages to install in common/migrator.py: _check_updates() whenever you change it below
PACKAGES_TO_INSTALL="ppa-migrator-plesks-migrator ppa-migrator-expand-migrator ppa-migrator-pbas-migrator ppa-migrator-move-subscriptions ppa-migrator-hsphere-migrator ppa-migrator-helm-migrator ppa-migrator-helm3-migrator ppa-migrator-confixx-migrator"

die() {
	local error="$1"
	local solution="$2"
	echo "ERROR: $error" 2>&1
	echo "$solution" 2>&1
	exit 1
}

die_os() {
	die "Your operating system is not supported." "The tools support only CentOS 5 and 6 or RHEL 5 and 6."
}

print_congratulations() {
	local action_performed="$1"
	
	cat << END

CONGRATULATIONS! You have successfully $action_performed the PPA moving tools.

To get help on the tools, run the following commands:
For the transfer tool:
  ppa-transfer help
For the move subscriptions between nodes tool:
  ppa-move-subscriptions help

To get the detailed instructions on how to perform the move to PPA,
follow the Moving to Parallels Plesk Automation guide at $PPA_MOVING_GUIDE_URL
To get the detailed instructions on how to perform the move subscriptions between PPA nodes,
follow the Moving Subscriptions Between Nodes section in Operations guide at $PPA_OPERATIONS_GUIDE_URL
END
}

die_invalid_usage() {
	local message="$1"
	cat << END
Invalid arguments passed.
Usage: installer.sh [--upgrade]
To install the PPA moving tools run the script without any arguments:
# ./installer.sh
To upgrade the PPA moving tools to the latest version run the script with "--upgrade" argument:
# ./installer.sh --upgrade
END
	exit 1
}

log_step() {
	local message="$1"
	echo "`date` ===> $message..."
}

download_repo_file() {
	log_step "Downloading the PPA moving tools repo file"
	repo_url=$PPA_URL/migration-tools/ppa-migrator-$osname.repo
	wget $repo_url -O /etc/yum.repos.d/ppa-migrator.repo
}

upgrade() {
	download_repo_file
	log_step "Upgrading the PPA moving tools packages"
	yum makecache
	remove_old_packages

	for pkg in $PACKAGES_TO_INSTALL; do
		if ! package_installed "$pkg"; then
			yum -y install "$pkg"
		else # package is already installed - update it
			yum -y update "$pkg"
		fi
	done

	print_congratulations "upgraded"
}

remove_old_packages() {
	# in old PPA 11.1 migration tool releases (not the latest PPA 11.1 tool releases!), several Python packages did not have python2.7-* prefix
	# on upgrade new packages that have such prefix conflict with those "old" packages by files, making upgrade to fail
	# we simply remove these "old" packages before upgrade 
	old_python_packages="PyYAML paramiko pysmb psycopg2"
	old_python_packages_installed=""
	for pkg in $old_python_packages; do
		if package_installed "$pkg"; then
			old_python_packages_installed="$old_python_packages_installed $pkg"
		fi
	done

	if [ ! -z "$old_python_packages_installed" ]; then
		log_step "Remove old packages"
		# remove "core" migrator packages (with all migrator packages that depend on "core"), 
		# so there are no more packages that depend on "old" Python packages
		if package_installed ppa-migrator-common; then
			yum -y remove ppa-migrator-common
		fi
		# remove "old" Python packages
		# in case there are dependencies apart of migration tool - upgrade will fail 
		# as we don't know who depends on them and whether it is safe to remove it
		# and in that case user should resolve dependencies manually
		rpm -e $old_python_packages_installed
	fi
}

package_installed() {
	package_name=$1
	rpm -q $package_name > /dev/null 2>&1
}

install() {
	download_repo_file
	log_step "Installing the PPA moving tools packages"
	yum -y install $PACKAGES_TO_INSTALL
	# it takes to check packages existence after yum as it does not set error when there are no packages
	for pkg in $PACKAGES_TO_INSTALL; do
		if ! rpm -q $pkg > /dev/null 2>&1; then
			die "Unable to install $pkg." "Install $pkg manually by running: yum install $pkg"
		fi
	done

	print_congratulations "installed"
}


# RedHat-like system is checked before programs, to save debian users from installing yum
if [ ! -f /etc/redhat-release ]; then
	die_os
fi

############################ check script requirements ##############################
required_programs="rpm yum wget sed"
for program in $required_programs; do
	$program --version > /dev/null || die "Unable to run $program." "The installer requires the following programs: $required_programs."
done

############################ determine OS ###########################################
osarch=`uname -m`

osrel=`cat /etc/redhat-release | sed 's/.*release //' | sed 's/\..*//'`
if [ "$osrel" != "5" -a "$osrel" != "6" ]; then
	die_os
fi

case `cat /etc/redhat-release` in
	CentOS*)
		osname=CentOS
		;;
	Red\ Hat\ Enterprise\ Linux\ Server*)
		osname=RedHat
		osrel="${osrel}Server"
		;;
	Red\ Hat\ Enterprise\ Linux*)
		cat 2>&1 << END
The installer has detected that you use a RHEL edition other than Server. The installer does not support such systems.
To install the tools, you should:
1. Update the yum repositories directory with the command 
wget $PPA_URL/migration-tools/ppa-migrator-RedHat.repo -O /etc/yum.repos.d/ppa-migrator.repo
2. Set the \$releasever parameter in the /etc/yum.repos.d/ppa-migrator.repo file to 5Server (for RHEL 5.x) or 6Server (for RHEL 6.x).
3. Install the tools with the command
yum install $PACKAGES_TO_INSTALL
END
		exit 1
		;;
	*)
		die_os
		;;
esac

############################# perform installation/upgrade ##################################
pkgs_installed=""
if [ $# -eq 0 ]; then
	for pkg in $PACKAGES_TO_INSTALL; do
		if package_installed $pkg; then
			pkgs_installed="$pkgs_installed $pkg"
		fi
	done

	if [ ! -z "$pkgs_installed" ]; then # if there are any root migration tool package - consider that migration tools are already installed (at least partially)
		echo 'The tools are already installed.'
		echo 'If you want to upgrade the PPA moving tools to the latest version run installer with "--upgrade" argument'
	else
		install
	fi
elif [ $# -eq 1 ]; then
	if [ "$1" = '--upgrade' ]; then
		upgrade
	else
		die_invalid_usage
	fi
else
	die_invalid_usage
fi

exit 0
