# postinstall script for MFWK Patch 121489-01
#

#
# Create the discovery key
#
CFG_MISSING=0

# hpux-dev : added code to support hpux	
PKG_AGENT=sun-mfwk-agent
PKG_CFG=sun-mfwk-cfg
PKG_CACAOCFG=sun-cacao-config
CACAO_DIRNAME=sun/cacao
BASEDIR=/opt
BASE_CFG=/
BASE_CACAOCFG=/

swlist sun-mfwk-cfg > /dev/null 2>&1
if [ $? -ne 0 ] ; then CFG_MISSING=1; fi

if [ ${CFG_MISSING} -ne 0 ] ; then
	echo "!!!!!! WARNING !!!!!! "
	echo "The package ${PKG_CFG} is not installed"
	echo "The generation of the security materials cannot be done."
	echo "Please run the following commands after installation of ${PKG_CFG} package :"
	echo "	${BASEDIR}/sun/mfwk/bin/gendisckey <INSTALL_ROOT of ${PKG_CFG} package>/etc/opt/sun/mfwk/security/disc.key"
	echo "  ${BASEDIR}/sun/mfwk/bin/genkey -d <INSTALL_ROOT of ${PKG_CFG} package>/etc/opt/sun/mfwk/security/"

else
	# Create the agent security materials
	echo "Creating discovery key."
	/opt/sun/mfwk/bin/gendisckey /etc/opt/sun/mfwk/security/disc.key
	echo "Creating the agent security materials."
	(cd /tmp;
	/opt/sun/mfwk/bin/genkey -d /etc/opt/sun/mfwk/security/)
fi


# the mfwk.multicast.disableloopback property must be set to true
if [ -f /etc/opt/sun/mfwk/agent.properties ]
then
	# There is an existent agent.properties.
	grep "mfwk.multicast.disableloopback" /etc/opt/sun/mfwk/agent.properties > /dev/null
	if [ $? -ne 0 ]
	then
		# The property does not exist.
		DATE=`date +%m-%d-%y_%H:%M:%S`
		cp /etc/opt/sun/mfwk/agent.properties /etc/opt/sun/mfwk/agent.properties.${DATE}
		echo "mfwk.multicast.disableloopback=true" >> /etc/opt/sun/mfwk/agent.properties
	fi
else
	echo "mfwk.multicast.disableloopback=true" > /etc/opt/SUNWmfwk/agent.properties
fi

# hpux-dev : Handle xml files on HPUX
sed -e 's;${etc_SUNWcacao};/etc/opt/sun/cacao;g' -e 's/private\/share\/lib/lib/g' -e 's/share\/lib/lib/g' /opt/sun/mfwk/xml/com.sun.mfwk.mfwk_module.xml > /opt/sun/mfwk/xml/com.sun.mfwk.mfwk_module.xml.tmp
mv /opt/sun/mfwk/xml/com.sun.mfwk.mfwk_module.xml.tmp /opt/sun/mfwk/xml/com.sun.mfwk.mfwk_module.xml	

sed -e 's;${var_SUNWmfwk};/var/opt/sun/mfwk;g' -e 's;${opt_SUNWmfwk};/opt/sun/mfwk;g' /etc/opt/sun/mfwk/agent.properties.sample > /etc/opt/sun/mfwk/agent.properties.sample.tmp
mv /etc/opt/sun/mfwk/agent.properties.sample.tmp /etc/opt/sun/mfwk/agent.properties.sample

# Take backup of the previous bunch of jar files.
if [ ! -d "/opt/sun/mfwk/lib/.backup" ]
then
	mkdir /opt/sun/mfwk/lib/.backup
fi

cp /opt/sun/mfwk/lib/*.jar /opt/sun/mfwk/lib/.backup

cp /opt/sun/mfwk/private/share/lib/*.jar /opt/sun/mfwk/lib
cp /opt/sun/mfwk/share/lib/*.jar /opt/sun/mfwk/lib
        
find ${BASEDIR}/sun/mfwk -name com.sun.mfwk.*.xml -print | while read j
do
	sed -e "s;BASEDIR/opt;${BASEDIR};g" $j > /tmp/temp
	mv /tmp/temp $j
done

if [ -f /etc/opt/sun/mfwk/agent.properties.sample ] ;
then 
	sed -e "s;BASEDIR/opt;${BASEDIR};g" /etc/opt/sun/mfwk/agent.properties.sample > /tmp/temp
	mv /tmp/temp /etc/opt/sun/mfwk/agent.properties.sample
fi


#
# Copy Java ES MF deployment descriptor in Cacao's directory
#

# Take backup of the previous bunch of jar files.
if [ ! -d "/${BASE_CACAOCFG}/etc/opt/${CACAO_DIRNAME}/modules/.backup" ]
then
	mkdir /${BASE_CACAOCFG}/etc/opt/${CACAO_DIRNAME}/modules/.backup
fi

cp /${BASE_CACAOCFG}/etc/opt/${CACAO_DIRNAME}/modules/com.sun.mfwk.mfwk_module.xml /${BASE_CACAOCFG}/etc/opt/${CACAO_DIRNAME}/modules/.backup

cp ${BASEDIR}/sun/mfwk/xml/com.sun.mfwk.mfwk_module.xml ${BASE_CACAOCFG}/etc/opt/${CACAO_DIRNAME}/modules/

# invoke cacaoadm command 
${BASEDIR}/${CACAO_DIRNAME}/bin/cacaoadm enable
