#!/bin/sh

if [ -n "$PLESK_INSTALLER_DEBUG" -o -n "$PLESK_INSTALLER_VERBOSE" ]; then
	set -x;
fi

prefix=""
root_d="/usr/local/psa"
tmpdir="$root_d/var"
prog="`basename $0`"
mu="`echo $prog | awk -F '-' '{print $1}'`"

[ -f /usr/lib/plesk-9.0/mailsrv_conf_init ] && install_d=/usr/lib/plesk-9.0
[ -f /usr/lib64/plesk-9.0/mailsrv_conf_init ] && install_d=/usr/lib64/plesk-9.0
[ -z "$install_d" ] && exit 0

detect_mta()
{
	local mta_name str

	if which rpm >/dev/null 2>&1; then
		str="`rpm -qa|grep plesk-mail-[pq]c-driver`"
	fi
	if which dpkg >/dev/null 2>&1; then
		str="`dpkg --get-selections plesk-mail-[pq]c-driver | awk '{if ($2 == \"install\") {print $1}}'`"
	fi

	case "$str" in
		plesk-mail-pc-driver*) mta_name="postfix";;
		plesk-mail-qc-driver*) mta_name="qmail" ;;
		*)
			echo "WARNING!!! Unable to detect your MTA service driver. Microupdate will not be installed."
			return 1
		;;
	esac

	echo "$mta_name"
	return 0
}

prefix="`detect_mta`" || exit 0

restorecmd=":"
which restorecon > /dev/null
if [ $? = 0 ]; then
        restorecmd="restorecon"
fi

# replace mailmng
sum_new="`md5sum $tmpdir/${prefix}-mailsrv_conf_init | cut -d\  -f 1`"
sum_old="`md5sum $install_d/mailsrv_conf_init | cut -d\  -f 1`"
if [ "$sum_old" != "$sum_new" ]; then
	cp -f $tmpdir/${prefix}-mailsrv_conf_init $install_d/mailsrv_conf_init
	# change permissions
	chown root:root $install_d/mailsrv_conf_init
	chmod 0550 $install_d/mailsrv_conf_init

	$restorecmd $install_d/mailsrv_conf_init
fi

exit 0
