#!/bin/bash

if [ "$1" = "-T" ]; then
	echo -n "(c) ISPsystem.com"
	exit 0
fi

if [ ! -t 0 ]; then
	exec 1>>var/pkg.log
	exec 2>&1
fi


LD_LIBRARY_PATH=""
MGRDIR=/usr/local/mgr5
cd ${MGRDIR} || exit 1
# PREINST_HOOKS needs for repository add
PREINST_HOOKS=""
# $1 - pkgname
if [ -f ${MGRDIR}/lib/pkgsh/hooks/${1}.sh ]; then
	. ${MGRDIR}/lib/pkgsh/hooks/${1}.sh
elif [ -f ${MGRDIR}/lib/pkgsh/hooks/${1} ]; then
	. ${MGRDIR}/lib/pkgsh/hooks/${1}
fi
if [ -n "${PREINST_HOOKS}" ]; then
	for preinst_hook in ${PREINST_HOOKS}; do
		${preinst_hook}
		hexeitcode=$?
		if [ "${hexeitcode}" -ne "0" ]; then
			sbin/mgrctl -m $2 $3 elid=$1 result=$exitcode
			exit ${hexeitcode}
		fi
	done
fi

echo ""
echo -n "*** "
date
echo "*** installing $1"
/usr/bin/yum -y install $1
exitcode=$?
if [ -n "$2" ] && [ -n "$3" ]; then
	sbin/mgrctl -m $2 $3 elid=$1 result=$exitcode
elif [ "$2" = "restart" ] && [ ${exitcode} -eq 0 ]; then
	sbin/mgrctl -l
	sbin/mgrctl -u
fi
exit $exitcode
