#!/bin/sh

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

prefix=""
tmpdir="/usr/local/psa/tmp"

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

# check-quota
root_d="/usr/local/psa"
hook_d="$root_d/handlers/hooks"
sum_new="`md5sum $tmpdir/${prefix}-check-quota | cut -d\  -f 1`"
sum_old="`md5sum $hook_d/check-quota | cut -d\  -f 1`"
if [ "$sum_old" != "$sum_new" ]; then
	cp -f $tmpdir/${prefix}-check-quota $hook_d/check-quota
	# change permissions
	if [ "$prefix" = "postfix" ]; then
		chown popuser:postfix $hook_d/check-quota
		chmod 6555 $hook_d/check-quota
	else
		chown popuser:popuser $hook_d/check-quota
		chmod 4550 $hook_d/check-quota
	fi

	$restorecmd $hook_d/check-quota
fi

# mail_mailbox_restore
if [ -f /usr/lib/plesk-9.0/mail_mailbox_restore ]; then
	F=/usr/lib/plesk-9.0/mail_mailbox_restore ;
fi;

if [ -f /usr/lib64/plesk-9.0/mail_mailbox_restore ]; then
	F=/usr/lib64/plesk-9.0/mail_mailbox_restore;
fi;

$restorecmd $F
$F

exit 0
