#!/bin/sh

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

# 0 - on, 1 - off
QMAIL=1
POSTFIX=1

prefix="postfix"
tmpdir="/usr/local/psa/tmp"
changed=0

if which rpm >/dev/null 2>&1; then
	if [ $POSTFIX = 1 ]; then
		rpm -qa | grep -q plesk-mail-pc-driver
		POSTFIX=$?
	fi;
	if [ $QMAIL = 1 ]; then
		rpm -qa | grep -q plesk-mail-qc-driver
		QMAIL=$?
	fi;
fi

if which dpkg >/dev/null 2>&1; then
	if [ $POSTFIX = 1 ]; then
		dpkg --list | grep -q plesk-mail-pc-driver
		POSTFIX=$?
	fi;
	if [ $QMAIL = 1 ]; then
		dpkg --list | grep -q plesk-mail-qc-driver
		QMAIL=$?
	fi;
fi

# detection went wrong - abstain from taking any actions
if [ $POSTFIX = $QMAIL ]; then
	if [ -n "$PLESK_INSTALLER_DEBUG" -o -n "$PLESK_INSTALLER_VERBOSE" ]; then
		echo ERROR: both qmail and postfix detected
	fi
	exit 1;
fi

[ "$POSTFIX" = "0" ] || prefix="qmail"

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

# all other
[ "`uname -m`" = "x86_64" ] && libdir="lib64" || libdir="lib"
file="mailsrv_conf_init"
sum_new="`md5sum $tmpdir/${prefix}-$file | cut -d\  -f 1`"
sum_old="`md5sum /usr/$libdir/plesk-9.0/$file | cut -d\  -f 1`"
if [ "$sum_old" != "$sum_new" ]; then
	cp -f $tmpdir/${prefix}-$file /usr/$libdir/plesk-9.0/$file
	$restorecmd /usr/$libdir/plesk-9.0/$file
	changed=1
fi
