#!/bin/sh

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

echo_msg()
{
	if [ -z "$PLESK_INSTALLER_DEBUG" -a -z "$PLESK_INSTALLER_VERBOSE" ]; then
		return 0
	fi

	echo "$*"
}

replace_bin()
{
	local src="$1"
	local dst="$2"	

	echo_msg "stopping qmail"
	/etc/init.d/qmail stop

	echo_msg "replacing qmail-local"
	mv $src $dst
	chown root:qmail $dst
	chmod 0555 $dst

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

	echo_msg "starting qmail"
	/etc/init.d/qmail start
}

qmail_bin="/var/qmail/bin/qmail-local"
qmail_enabled="0"
qmail_sum=""

qmail_bin_new="/usr/local/psa/var/qmail-local"
qmail_sum_new="`md5sum $qmail_bin_new | awk '{print $1}'`"

# is qmail enabled?
if [ -f "$qmail_bin" ]; then
	qmail_enabled="1"
	qmail_sum="`md5sum $qmail_bin | awk '{print $1}'`"
else
	echo_msg "qmail-queue not found, no need to replace"
	exit 0;
fi

if [ "$qmail_sum" = "$qmail_sum_new" ]; then
        echo_msg "qmail-queue is up to date, skipping"
        exit 0
fi
replace_bin "$qmail_bin_new" "$qmail_bin"

exit 0
