#!/bin/sh

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

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

if [ "$prefix" = "postfix" ]; then
	if [ -n "$PLESK_INSTALLER_DEBUG" -o -n "$PLESK_INSTALLER_VERBOSE" ]; then
		echo "postfix detected. skipping.."
	fi
	exit 0
fi

if [ -n "$PLESK_INSTALLER_DEBUG" -o -n "$PLESK_INSTALLER_VERBOSE" ]; then
	echo "qmail detected"
fi

if [ -e /usr/local/psa/tmp/qmail-remote.moved ]; then
	mv /usr/local/psa/tmp/qmail-remote.moved /var/qmail/bin/qmail-remote.moved
	chown root:qmail /var/qmail/bin/qmail-remote.moved
	chmod 0555 /var/qmail/bin/qmail-remote.moved

	which restorecon > /dev/null
	if [ $? = 0 ]; then
		restorecon /var/qmail/bin/qmail-remote.moved
	fi;
fi

