#!/bin/sh

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

F=''

if [ -f /usr/lib/plesk-9.0/psa-pc-remote ]; then
	F=/usr/lib/plesk-9.0/psa-pc-remote ;
fi;

if [ -f /usr/lib64/plesk-9.0/psa-pc-remote ]; then
	F=/usr/lib64/plesk-9.0/psa-pc-remote ;
fi;

# not postfix
if [ -z "$F" ]; then
	if [ -n "$PLESK_INSTALLER_DEBUG" -o -n "$PLESK_INSTALLER_VERBOSE" ]; then
		echo psa-pc-remote 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/psa-pc-remote | cut -d\  -f 1`

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


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

if [ -n "$PLESK_INSTALLER_DEBUG" -o -n "$PLESK_INSTALLER_VERBOSE" ]; then
	echo replacing psa-pc-remote;
fi
mv /usr/local/psa/tmp/psa-pc-remote $F
chown postfix.popuser $F
chmod g+s $F

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

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