#!/bin/sh

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

test -e /usr/lib64 && lib_d="lib64" || lib_d="lib"

prefix=""
install_d="/usr/$lib_d/plesk-9.0"
tmpdir="/usr/local/psa/var"

prog="`basename $0`"
mu="`echo $prog | awk -F '-' '{print $1}'`"

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/mailman_lists_dump-${prefix} | cut -d\  -f 1`"
sum_old="`md5sum $install_d/mailman_lists_dump | cut -d\  -f 1`"
if [ "$sum_old" != "$sum_new" ]; then
	cp -f $tmpdir/mailman_lists_dump-${prefix} $install_d/mailman_lists_dump
	# change permissions
	chown root:root $install_d/mailman_lists_dump
	chmod 0750 $install_d/mailman_lists_dump

	$restorecmd $install_d/mailman_lists_dump
fi

exit 0
