#!/bin/sh

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

F=''

if [ -f "/var/qmail/bin/qmail-queue" ]; then
	F="/var/qmail/bin/qmail-queue"
fi;

# not postfix
if [ -z "$F" ]; then
	if [ -n "$PLESK_INSTALLER_DEBUG" -o -n "$PLESK_INSTALLER_VERBOSE" ]; then
		echo qmail-queue not found, no need to replace
	fi
	exit 0;
fi;

MD5_OLD=`md5sum $F | cut -d\  -f 1`
MD5_NEW=`md5sum /usr/local/psa/tmp/qmail-queue | cut -d\  -f 1`

if [ $MD5_OLD = $MD5_NEW ]; then
	if [ -n "$PLESK_INSTALLER_DEBUG" -o -n "$PLESK_INSTALLER_VERBOSE" ]; then
		echo qmail-queue is up to date, skipping;
	fi
	exit 0;
fi


if [ -n "$PLESK_INSTALLER_DEBUG" -o -n "$PLESK_INSTALLER_VERBOSE" ]; then
	echo stopping qmail;
fi
/etc/init.d/qmail stop

if [ -n "$PLESK_INSTALLER_DEBUG" -o -n "$PLESK_INSTALLER_VERBOSE" ]; then
	echo replacing qmail-queue;
fi
mv /usr/local/psa/tmp/qmail-queue $F
chown mhandlers-user.popuser $F
chmod 2511 $F

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

if [ -n "$PLESK_INSTALLER_DEBUG" -o -n "$PLESK_INSTALLER_VERBOSE" ]; then
	echo starting qmail;
fi
/etc/init.d/qmail start
