#!/bin/sh
# Copyright (c) 2009 by Rack-Soft, LLC
# Copyright (c) 2009 by 4PSA (www.4psa.com)
# All rights reserved

# This script installs or updates 4PSA products for Plesk on supported RPM operating systems

# Modified: $DateTime: 2011/10/26 10:43:26 $
# Revision: $Revision: #9 $ with $Change: 84977 $

is_root()
{
current_user="`id`"
case "$current_user" in
	uid=0\(root\)*)
	;;
	*)
		echo "$0: You must have root privileges in order to install 4PSA products"
		echo "Su as root and try again"
		echo
		exit 1
	;;
esac
}

err_msg()
{
echo -e "\E[31m ERROR: $* \033[0m"

}
die()
{
	echo -e "\E[31mERROR: $* "
	echo "Check the error reason, fix and try again"
	echo -e "\033[0m"
	exit 1
}


download_yuminf()
{
	rm -f yum.inf
	${wget_path}wget -T20 ${dsite}/${REPO}/yum.inf >/dev/null 2>&1
	if [ -f yum.inf ];then
		yumconf_location=`grep "^${sys}${release}" yum.inf | grep "${arch}" | awk '{print $2}'`
		if [ -z "${yumconf_location}" ];then
			echo
			die "The remote repository does not contain information for this OS!"
		fi
	else
		die "Could not get the yum information file."
	fi
}


import_4psa_key()
{
	rpm --import http://www.4psa.com/software/4PSA-GPG-KEY
}


check_yum()
{
which yum >/dev/null 2>&1
if [ $? -gt 0 ];then
	# yum is not in path
	rpm -q yum >/dev/null 2>&1
	if [ $? -gt 0 ];then
		#yum must be downloaded and installed
		echo "You must have yum installed in order for this installer to work"
		echo "Check your distribution manual to see how to install yum and try again"
		echo
	else
		#yum seems installed, but not in path
		if [ -f /usr/bin/yum ];then
			#yum was found in /usr/bin
			yum_path='/usr/bin/'
		else
			echo "The RPM package manager yum is installed on your system, but 4PSA products installer can not find it."
			die "It's not in the system PATH or in a standard location"
		fi
	fi
fi
}


check_wget()
{
which wget >/dev/null 2>&1
if [ $? -gt 0 ];then
	if [ -f /usr/bin/wget ];then
		wget_path='/usr/bin/'
	else
		echo "wget binary can not be found."
		die "It's not in the system PATH or in a standard location."
	fi
fi
}


get_yumconf()
{
	download_yuminf
	rm -f 4plesk-yum.conf
	
	${wget_path}wget -T20 ${dsite}${yumconf_location} >/dev/null 2>&1
	if [ ! -f 4plesk-yum.conf ];then
		die "Could not get 4plesk-yum.conf"
	fi
}


check_plesk()
{
	PLESK_VER=""
	local PRODUCT_ROOT=""
	if [ -d "/etc/psa/" ];then
		echo
		echo "		Parallels Plesk was detected on this server."
		if [ -f "/etc/psa/psa.conf" ];then				
			PRODUCT_ROOT_D=`awk '/^[ \t]*PRODUCT_ROOT_D/{print $2}' /etc/psa/psa.conf`
			if [ -f "${PRODUCT_ROOT_D}/version" ];then
				PLESK_VER=`cat ${PRODUCT_ROOT_D}/version|cut -f 1 -d ' '|awk -F '.' '{print $1$2$3}'`
			fi
		fi
	fi
	if [ -n "${PLESK_VER}" ] ;then
		if [ ${PLESK_VER} -lt 800 ];then 
			die "4PSA products  requires at least Plesk 8.0.0 to work"
		fi	
		if [ "${PLESK_VER}" -lt 900 ];then
			psa_suffix="psa8"
		fi
		if [ "${PLESK_VER}" -lt 1000 ];then
			psa_suffix="psa9"
		else
			psa_suffix="psa10"
		fi
		
	else
		die "Parallels Plesk version could not be detected, installation is incomplete"
	fi
	
}

welcome() {
	
	action='Upgrade'
	action='Installation'

curdepth=`pwd | wc -c`

if [ "${curdepth}" -lt 4 ];then
	die "Please do not execute this script from system /"
fi

echo
echo	
echo	"4PSA for Plesk products command Line Auto-installer."
echo
echo	"Please read the Release Notes before running this script."
echo
echo	"Thank you for choosing to use our products"
echo	"${action} procedure can start in a few moments."
echo
echo "Do you want to continue? [N] "

read answer

case "${answer}" in
	y*|Y*)
	;;
	*)
		echo
		echo "You will be able to install/update our products later."
		echo
		exit 1
	;;
esac
echo
}

clamavoption() {
	echo
	echo	"You must install ClamAV antivirus if you want to use local scanning engine"
	echo
	echo	"=========================================================================="
	echo 	"== NOTICE: You can use only remote nodes for mail scanning              =="	
	echo	"=========================================================================="
	echo
	echo 	"Do you want to install ClamAV on local server? [N] "

	read answer

	case "${answer}" in
		y*|Y*)
			installclamav=1
		;;
		*)
			installclamav=0
		;;
	esac
}

check_os()
{
config="/etc/psa/psa.conf"
TARGET_OS="`uname -a`"
case "$TARGET_OS" in
	FreeBSD*i386)
		
		echo  "                This installer does not supports FreeBSD.                     "
		echo  "In order to download the FreeBSD installer, download it from the Clients Area:"
		echo  "                       http://www.4psa.com/clients/                           "
		exit 1
		;;
	Linux*2.?.*?86*|Linux*ppc*)
                if [ -f /etc/debian_version ];then
		
			echo  "                 This installer does not supports Debian.                     "
			echo  "In order to download the Debian installer, download it from the Clients Area: "
			echo  "                        http://www.4psa.com/clients/                          "
			exit 1
		fi		
		if [ -f /etc/SuSE-release ];then
			sys="suse"
			release=`grep "^VERSION" /etc/SuSE-release | cut -d= -f2 | awk '{print $1}'`
			if [ "`uname -i|grep -Ec 'i?86'`" == "1" ];then 
				arch='i586'
			else
				arch='x86_64'
			fi
		fi
   		if [ -f /etc/redhat-release ];then
			fedora=`grep "^Fedora" /etc/redhat-release`
			rhel=`grep "^Red Hat Enterprise Linux" /etc/redhat-release`
			centos=`grep "^CentOS" /etc/redhat-release`
	
			if [ ! -z "${fedora}" ];then
				sys="fc"
				release=`cat /etc/redhat-release|grep -Eo "release[ \t0-9]*"|awk '{print $2}'`
			fi
			if [ ! -z "${rhel}" ];then
				sys="rhel"
				release=`cat /etc/redhat-release | awk '{print $7}' | cut -d'.' -f1`
			fi
			if [ ! -z "${centos}" ];then
				sys="centos"
				release=`cat /etc/redhat-release | sed 's/linux//gi'| awk '{print $3}' | cut -d'.' -f1`
			fi
			arch=`uname -i`
   		fi
   		if [ -z "${sys}" -o -z "${release}" ];then
   			err_msg "Can not determine Linux distribution."
   			exit 127
   		fi
   		
		;;
		*)
			err_msg "You do not have a supported OS."
			exit 127
		;;
esac

if [ -z "${sys}" -o -z "${release}" ];then
	err_msg "You do not have a supported OS."
	exit 127
fi

}
check_repo_cache()
{
get_yumconf
cache_dir=`grep "^cachedir" 4plesk-yum.conf | cut -d'=' -f2`
    
if [ ! -d ${cache_dir} ];then
        mkdir -p ${cache_dir}
fi
}

import_gpg_keys()
{
# this is a hack
for gpgkey in `grep "^gpgkey" 4plesk-yum.conf | cut -d'=' -f2 | awk '{print $1}'`;do
	rpm --import ${gpgkey} >/dev/null 2>&1
done
}

main_menu()
{
YOFF=5
XOFF=10
TITLEY=$((${YOFF}-2))
TITLEX=$((${XOFF}-7))
OFFSET=40
exit_menu=0
astats=0
clogin=0
cbackup=0
cserver=0
integrator=0
notif=0
qmanager=0
sassistant=0
sguardian=0
tbackup=0
tdomains=0
voipmodule=0 
oxtender=0

until [ "${exit_menu}" == "1" ];do
	tput clear
									     
	tput cup ${TITLEY} ${TITLEX}; echo  "===========> 4PSA Products for Plesk $1 <============="	
        tput cup $(( ${YOFF} + 1 )) ${XOFF}; echo -n " 1.  4PSA Central Login   "
        tput cup $(( ${YOFF} + 1 )) $((${XOFF}+${OFFSET}));if [ "${clogin}" == "1" ];then echo -n "[x]" ;else echo -n "[ ]";fi
	tput cup $(( ${YOFF} + 2 )) ${XOFF}; echo -n " 2.  4PSA Clean Server    "	
        tput cup $(( ${YOFF} + 2 )) $((${XOFF}+${OFFSET}));if [ "${cserver}" == "1" ];then echo -n "[x]" ;else echo -n "[ ]";fi
	tput cup $(( ${YOFF} + 3 )) ${XOFF}; echo -n " 3.  4PSA Spam Guardian    "
        tput cup $(( ${YOFF} + 3 )) $((${XOFF}+${OFFSET}));if [ "${sguardian}" == "1" ];then echo -n "[x]" ;else echo -n "[ ]";fi
	tput cup $(( ${YOFF} + 4 )) ${XOFF}; echo -n " 4.  4PSA Total Backup     " 
        tput cup $(( ${YOFF} + 4 )) $((${XOFF}+${OFFSET}));if [ "${tbackup}" == "1" ];then echo -n "[x]" ;else echo -n "[ ]";fi
	tput cup $(( ${YOFF} + 5 )) ${XOFF}; echo -n " 5.  4PSA OXtender for  Plesk     " 
        tput cup $(( ${YOFF} + 5 )) $((${XOFF}+${OFFSET}));if [ "${oxtender}" == "1" ];then echo -n "[x]" ;else echo -n "[ ]";fi
	if [ "${PLESK_VER}" -lt 1000 ];then
		tput cup $(( ${YOFF} + 6 )) ${XOFF}; echo -n " 6.  4PSA Advance Statistics    " 
	        tput cup $(( ${YOFF} + 6 )) $((${XOFF}+${OFFSET}));if [ "${astats}" == "1" ];then echo -n "[x]" ;else echo -n "[ ]";fi
		tput cup $(( ${YOFF} + 7 )) ${XOFF}; echo -n " 7.  4PSA Client Backup    " 
	        tput cup $(( ${YOFF} + 7 )) $((${XOFF}+${OFFSET}));if [ "${cbackup}" == "1" ];then echo -n "[x]" ;else echo -n "[ ]";fi
		tput cup $(( ${YOFF} + 8 )) ${XOFF}; echo -n " 8.  4PSA Integrator    " 
	        tput cup $(( ${YOFF} + 8 )) $((${XOFF}+${OFFSET}));if [ "${integrator}" == "1" ];then echo -n "[x]" ;else echo -n "[ ]";fi
		tput cup $(( ${YOFF} + 9 )) ${XOFF}; echo -n " 9.  4PSA Notification    " 
	        tput cup $(( ${YOFF} + 9 )) $((${XOFF}+${OFFSET}));if [ "${notif}" == "1" ];then echo -n "[x]" ;else echo -n "[ ]";fi
		tput cup $(( ${YOFF} + 10 )) ${XOFF}; echo -n " 10. 4PSA Qmail Manager     " 
	        tput cup $(( ${YOFF} + 10 )) $((${XOFF}+${OFFSET}));if [ "${qmanager}" == "1" ];then echo -n "[x]" ;else echo -n "[ ]";fi
		tput cup $(( ${YOFF} + 11 )) ${XOFF}; echo -n " 11. 4PSA Server Assistant    " 
	        tput cup $(( ${YOFF} + 11 )) $((${XOFF}+${OFFSET}));if [ "${sassistant}" == "1" ];then echo -n "[x]" ;else echo -n "[ ]";fi
		tput cup $(( ${YOFF} + 12 )) ${XOFF}; echo -n " 12. 4PSA Total Domains " 
	        tput cup $(( ${YOFF} + 12 )) $((${XOFF}+${OFFSET}));if [ "${tdomains}" == "1" ];then echo -n "[x]" ;else echo -n "[ ]";fi
		tput cup $(( ${YOFF} + 13 )) ${XOFF}; echo -n " 13. 4PSA VoipNow Plesk Module " 
	        tput cup $(( ${YOFF} + 13 )) $((${XOFF}+${OFFSET}));if [ "${voipmodule}" == "1" ];then echo -n "[x]" ;else echo -n "[ ]";fi
	fi

	echo 
	if [ "${tput_invalid}" == 1 ];then
		tput cup $((${YOFF}+16)) ${XOFF};printf  "Enter : 1 2 ...             - To select/deselect a product"
		tput cup $((${YOFF}+17)) ${XOFF};printf  "Enter : i                   - To begin installation "
		tput cup $((${YOFF}+18)) ${XOFF};printf  "Enter : x                   - To abort installation"
		tput cup $((${YOFF}+19)) ${XOFF};echo "Invalid values, please try again."
		tput cup $((${YOFF}+20)) ${XOFF};printf  "Select the product(s) : "
		tput_invalid=0
	else
		tput cup $((${YOFF}+15)) ${XOFF};printf  "Enter : 1 2 ...             - To select/deselect a product"
		tput cup $((${YOFF}+16)) ${XOFF};printf  "Enter : i                   - To begin installation "
		tput cup $((${YOFF}+17)) ${XOFF};printf  "Enter : x                   - To abort installation"
		tput cup $((${YOFF}+19)) ${XOFF};printf  "Select the product(s) : "
		tput_invalid=0
	fi
	read  answer
	for product in ${answer}; do
	case "${product}" in
		i|I)	
			exit_menu=1
		;;
		x|X)
			exit
		;;
		1)	
			if [ "${clogin}" == 0 ];then
				clogin=1
			else
				clogin=0
			fi
		;;
		2)	
			if [ "${cserver}" == 0 ];then
				cserver=1
			else
				cserver=0
			fi
		;;

		3)
			if [ "${sguardian}" == 0 ];then
				sguardian=1
			else	
				sguardian=0
			fi
		;;
		4)
			if [ "${tbackup}" == 0 ];then
				tbackup=1
			else
				tbackup=0
			fi
		;;
		5)
			if [ "${oxtender}" == 0 ];then
				oxtender=1
			else
				oxtender=0
			fi
		;;
		6)
			if [ "${astats}" == 0 ];then
				astats=1
			else
				astats=0
			fi
		;;
		7)
			if [ "${cbackup}" == 0 ];then
				cbackup=1
			else
				cbackup=0
			fi
		;;
		8)
			if [ "${integrator}" == 0 ];then
				integrator=1
			else
				integrator=0
			fi
		;;
		9)
			if [ "${notif}" == 0 ];then
				notif=1
			else
				notif=0
			fi
		;;
		10)
			if [ "${qmanager}" == 0 ];then
				qmanager=1
			else 
				qmanager=0
			fi
		;;
		11)
			if [ "${sassistant}" == 0 ];then
				sassistant=1
			else	
				sassistant=0
			fi

		;;
		12)
			if [ "${tdomains}" == 0 ];then
				tdomains=1
			else
				tdomains=0
			fi
		;;
		13)
			if [ "${voipmodule}" == 0 ];then
				voipmodule=1
			else
				voipmodule=0
			fi
		;;
		*)
			tput_invalid=1

		;;
	esac
	done
	tput clear
done	
}

install_product()
{
	local product="$1"
	local product_name="$2"
	rpm -qi ${product} 1>/dev/null 2>&1
	if [ $? -eq 0 ];then
		yum -y -c 4plesk-yum.conf update ${product}
	else
		echo 
		echo "==> Trying to install ${product_name} ..."
		echo
		yum -y -c 4plesk-yum.conf install ${product}
	fi

	if [ $? -eq 0 ];then
		echo
		rpm -qi ${product} 1>/dev/null 2>&1
		if [ $? -eq 0 ];then
			echo "${product_name} ${action} completed succesfully!"
			return 0
		else
			echo -e "\E[31m" 
	                echo "Could not install ${product_name}!"
                	echo
        	        echo "If ${product_name} is conflicting with an installed package, uninstall the offending package with:"
	                echo "rpm -e package_name"
        		echo 
			echo "Please contact 4PSA support by opening a ticket in the Help Desk at:"
			echo "				https://help.4psa.com	"
        	        echo  -e "\033[0m"
	                return 1
		fi
	else
		echo -e "\E[31m" 
		echo "Could not install ${product_name}!"
		echo
		echo "If ${product_name} is conflicting with an installed package, uninstall the offending package with:"
		echo "rpm -e package_name"
		echo
		echo "Please contact 4PSA support by opening a ticket in the Help Desk at:"
		echo "				https://help.4psa.com	"
       	        echo  -e "\033[0m"
		return 1
	fi

}

remove_product()
{

local product="$1"
local product_name="$2"
rpm -qi ${product} 1>/dev/null 2>&1
if [ "$?" == "1" ];then	
	echo 
	echo "${product_name} must be installed first"
	echo "Note this only remove RPM version of the product"
	echo
	return 1
fi
echo
echo
echo
echo	"ARE YOU SURE THAT YOU WANT TO UNINSTALL ${product_name}?"
echo 
echo	"You will loose EVERYTHING related to ${product_name}"
echo	"and you can recover later. If you are sure, type AGREE (in capitals)"
echo
echo "Do you want to continue? [N] "

read answer

case "${answer}" in
	AGREE)
		yum -y -c 4plesk-yum.conf remove ${product}
		if [ $? -eq 0 ];then
			echo
			die "An error occured while uninstalling ${product_name}."
		else
			echo
			echo "${product_name} uninstalled succesfully."
			return 0
		fi

	;;
	*)
		echo
		echo "Did you hear the voice? Wise choice."
		echo
		return 0
	;;
esac
echo
}


install_with_yum()
{
if [ "${cserver}" == 1 ];then
	if [ ${PLESK_VER} -lt 820 ];then
		echo "4PSA Clean Server requires at least Plesk 8.2.0 to be installed"
		cserver=0
	else
		clamavoption
		if [ "${installclamav}" == "1" ];then
			yum -y -c 4plesk-yum.conf groupupdate 4PSA-Clamav
		fi
	fi
fi
clear
echo
echo "==> Starting 4PSA Plesk products instalation procedure."
echo "This process will take a while ..."
echo

if [ "${astats}" == 1 ];then
	install_product "4psa-astats-${psa_suffix}" "4PSA Advanced Statistics"
fi
if [ "${cbackup}" == 1 ];then
	install_product "4psa-cbackup-${psa_suffix}" "4PSA Client Backup"
fi
if [ "${clogin}" == 1 ];then
	install_product "4psa-clogin-${psa_suffix}" "4PSA Central Login"
fi
if [ "${cserver}" == 1 ];then
		install_product "4psa-cserver-${psa_suffix}" "4PSA Clean Server"
fi
if [ "${integrator}" == 1 ];then
	install_product "4psa-integrator-${psa_suffix}" "4PSA Integrator"
fi
if [ "${notif}" == 1 ];then
	install_product "4psa-notifications-${psa_suffix}" "4PSA Notifications"
fi
if [ "${qmanager}" == 1 ];then
	install_product "4psa-qmanager-${psa_suffix}" "4PSA Qmail Manager"
fi
if [ "${sassistant}" == 1 ];then
	install_product "4psa-sassistant-${psa_suffix}" "4PSA Server Assistant"
fi
if [ "${sguardian}" == 1 ];then
	install_product "4psa-sguardian-${psa_suffix}" "4PSA Spam Guardian"
fi
if [ "${tbackup}" == 1 ];then
	install_product "4psa-tbackup-${psa_suffix}" "4PSA Total Backup"
fi
if [ "${tdomains}" == 1 ];then
	install_product "4psa-tdomains-${psa_suffix}" "4PSA Total Domains"
fi
if [ "${voipmodule}" == 1 ];then
	install_product "voipnow4plesk-${psa_suffix}" "4PSA VoipNow Plesk Module"
fi
if [ "${oxtender}" == 1 ];then
	install_product "ox4psa-${psa_suffix}" "4PSA OXtender for Plesk"
fi

}

remove_with_yum()
{
clear
echo 
echo "==> Starting 4PSA Plesk products instalation procedure."
echo "This process will take a while ..."
echo 
if [ "${astats}" == 1 ];then
	remove_product "4psa-astats-${psa_suffix}" "4PSA Advanced Statistics"
fi
if [ "${cbackup}" == 1 ];then
	remove_product "4psa-cbackup-${psa_suffix}" "4PSA Client Backup"
fi
if [ "${clogin}" == 1 ];then
	remove_product "4psa-clogin-${psa_suffix}" "4PSA Central Login"
fi
if [ "${cserver}" == 1 ];then
	remove_product "4psa-cserver-${psa_suffix}" "4PSA Clean Server"
fi
if [ "${integrator}" == 1 ];then
	remove_product "4psa-integrator-${psa_suffix}" "4PSA Integrator"
fi
if [ "${notif}" == 1 ];then
	remove_product "4psa-notifications-${psa_suffix}" "4PSA Notifications"
fi
if [ "${qmanager}" == 1 ];then
	remove_product "4psa-qmanager-${psa_suffix}" "4PSA Qmail Manager"
fi
if [ "${sassistant}" == 1 ];then
	remove_product "4psa-sassistant-${psa_suffix}" "4PSA Server Assistant"
fi
if [ "${sguardian}" == 1 ];then
	remove_product "4psa-sguardian-${psa_suffix}" "4PSA Spam Guardian"
fi
if [ "${tbackup}" == 1 ];then
	remove_product "4psa-tbackup-${psa_suffix}" "4PSA Total Backup"
fi
if [ "${tdomains}" == 1 ];then
	remove_product "4psa-tdomains-${psa_suffix}" "4PSA Total Domains"
fi
if [ "${voipmodule}" == 1 ];then
	remove_product "voipnow4plesk-${psa_suffix}" "4PSA VoipNow Plesk Module"
fi
if [ "${oxtender}" == 1 ];then
	remove_product "ox4psa-${psa_suffix}" "4PSA OXtender for Plesk"
fi

}

clean_up()
{
	if [ -f "4plesk-yum.conf" ];then
		rm -f 4plesk-yum.conf	
	fi
	if [ -f "yum.inf" ];then
		rm -f yum.inf
	fi
}
# main

cd `dirname $0`
REPO='downloads'
yum_path=''
wget_path=''
dsite='http://plesk.4psa.com'
is_root

if [ "$1" = "remove" ];then
	check_plesk
	main_menu "uninstaller"
	remove_with_yum
else
	check_plesk
	check_os
	welcome
	check_wget
	check_yum
	download_yuminf
	import_4psa_key
	check_repo_cache
	import_gpg_keys
	main_menu "installer"
	install_with_yum
fi
clean_up
exit 0
