#!/bin/sh

set -efu

die()
{
	echo "ERROR: $*" >&2
	exit 1
}

verbose()
{
	if [ -n "$verbose" ]; then
		echo "$@" >&2
	fi
}

check_root()
{
	if [ `id -u` -ne 0 ]; then
		die "You should have superuser privileges to install Plesk"
	fi
}

check_for_upgrade()
{
	local prefix
	local version=
	for prefix in /opt/psa /usr/local/psa; do
		if [ -e "$prefix/version" ]; then
			version=`cat $prefix/version |  awk '{ print $1 }'`
			break
		elif [ -f "$prefix/core.version" ]; then
			version=`cat $prefix/core.version |  awk '{ print $1 }'`
			break
		fi
	done

	if [ -n "$version" ]; then
		echo "You have Plesk v $version installed."
		echo "Please, use Parallels installer from Web Interface or CLI"
		exit 0
	fi
}

fetch_file()
{
	local url=$1
	local target=$2

	if [ -x "/usr/bin/wget" ]; then
		cmd="/usr/bin/wget -q $url -O $target"
	elif [ -x "/usr/bin/curl" ]; then
		cmd="/usr/bin/curl $url -o $target"
	elif [ -x "/usr/bin/fetch" ]; then
		cmd="/usr/bin/fetch $url-o $target"
	else
		die "Unable to find download manager(fetch, wget, curl)"
	fi

	verbose "Trasnport command is $cmd"

	if ! $cmd; then
		die "Unable to fetch Parallels Installer"
	fi
}

fetch_autoinstaller()
{
	if [ -z "${1:-}" ]; then
		ai_dest="/root/parallels_installer"
	fi

	rm -f "$ai_dest" >/dev/null 2>&1
	fetch_file "http://autoinstall.plesk.com/Parallels_Installer/$ai_name" "$ai_dest"
	chmod 0700 "$ai_dest"
}

get_ai_name()
{
	[ -e '/bin/uname' ] && uname='/bin/uname' || uname='/usr/bin/uname'
	local arch=`uname -m`
	local os_sn

	case $arch in
		i?86) arch="i386" ;;
		*) : ;;
	esac

	opsys=`uname -s`
	if [ "$opsys" = 'Linux' ]; then
		if [ -e '/etc/debian_version' ]; then
			if [ -e '/etc/lsb-release' ]; then
# Mostly ubuntu, but debian can have it
				. /etc/lsb-release
				os_name=$DISTRIB_ID
				os_version=$DISTRIB_RELEASE
			else
				os_name='Debian'
				os_version=`head -1 /etc/debian_version`
			fi
			case $os_name in
				Debian) os_version=`echo $os_version | grep -o "^[0-9]*\.[0-9]*"`;;
				Ubuntu) ;;
				*) die "Unknown OS: $os_name-$os_version-$arch" ;;
			esac
		elif [ -e '/etc/SuSE-release' ]; then
			os_name='SuSE'
			os_version=`head -1 /etc/SuSE-release | sed -e 's/[^0-9.]*\([0-9.]*\).*/\1/g'`
			if grep -q 'Enterprise Server' /etc/SuSE-release; then
				os_version="es$os_version"
			fi
		elif [ -e '/etc/fedora-release' ]; then
			os_name='FedoraCore'
			os_version=`head -1 /etc/fedora-release | sed -e 's/[^0-9.]*\([0-9.]*\).*/\1/g'`
		elif [ -e '/etc/redhat-release' ]; then
			os_name=`awk '{print $1}' /etc/redhat-release`
			os_version=`head -1 /etc/redhat-release | sed -e 's/[^0-9.]*\([0-9.]*\).*/\1/g'`
# for rh based os get only major
			os_version=`echo $os_version | awk -F'.' '{print $1}'`
			case $os_name$os_version$arch in
				CentOS4*i386) os_version="4.2" ;;
				CentOS4*x86_64) os_version="4.3" ;;
				CentOS*|Cloud*) os_version=`echo $os_version | awk -F'.' '{print $1}'` ;;
				Red*) os_name="RedHat"; os_version="el`echo $os_version | awk -F'.' '{print $1}'`" ;;
				*) die "Unknown OS: $os_name-$os_version-$arch" ;;
			esac
		else
			die "Unable to detect OS"
		fi
	elif [ "$opsys" = 'FreeBSD' ]; then
		os_name='FreeBSD'
		os_version=`$uname -r | sed -e 's/[^0-9.]*\([0-9.]*\).*/\1/g'`
	else
		die "Unable to detect OS"
	fi

	verbose "Detected os $os_name-$os_version-$arch"
	echo "parallels_installer_${os_name}_${os_version}_${arch}"
}

verbose=
dry_run=

while getopts "vnh" Option; do
	case $Option in
		v) verbose=1 ;;
		n) dry_run=1 ;;
		*) verbose "Unknown option $Option, skipping" ;;
	esac
done
shift $(($OPTIND - 1))

check_root
check_for_upgrade

ai_name=`get_ai_name`
if [ -z "$ai_name" ]; then
	die "Cannot obtain OS information"
fi
#ai_dest=`mktemp -u`
ai_dest='/root/parallels_installer'
fetch_autoinstaller "$ai_name" "$ai_dest"

ai_cmd="$ai_dest --source http://autoinstall-ctp.plesk.com/PP11.0-preview/unix/ --select-product-id=plesk --select-release-latest --installation-type=Typical"

if [ -n "$dry_run" ]; then
	verbose "Follwing command will run: $ai_cmd"
	rm -f $ai_dest
else
	exec $ai_cmd
fi
