#! /bin/sh
#
#ident	"@(#)postinstall	1.16	01/04/18 SMI"
#
# Copyright (c) 2001 by Sun Microsystems, Inc.
#
# post-installation script for volume manager CIM object installation

echo "Included with the Solaris(TM) Operating Environment are additional new"
echo "technologies developed by Sun Microsystems and made available for your"
echo "evaluation."
echo
echo "These technologies are provided to highlight future functionality being"
echo "developed for the Solaris Operating Environment. Please note any"
echo "documentation or descriptive files that call attention to its use."
echo
echo "This software is offered for evaluation purposes only and is not"
echo "recommended or intended for commercial, production, or any non-trial"
echo "deployment or use. Sun's product warranties do not extend to this software."
echo
echo "Any and all feedback on this software is welcomed by Sun Microsystems, Inc."
echo

ADMIN_LIBDIR=${BASEDIR}/usr/sadm/lib
VIPER_HOME=${BASEDIR}/usr/sadm/lib/smc
export BASEDIR
export VIPER_HOME

# need java to run mofcomp and smcconf
JAVACMD=${BASEDIR}/usr/bin/java
if [ ! -z "$JAVA_HOME" ]; then
	JAVACMD=${JAVA_HOME}/bin/java
fi

if [ ! -x "$JAVACMD" ]; then
	echo "Missing java, unable to install the CIM objects.  After"
	echo "installing the prerequisite packages, run the following command"
	echo "to complete the installation."
	echo "${BASEDIR}/usr/sadm/bin/mofcomp ${BASEDIR}/usr/sadm/mof/Solaris_VM1.0.mof"
	exit 0
fi

JARCMD=${BASEDIR}/usr/bin/jar
if [ ! -z "$JAVA_HOME" ]; then
	JARCMD=${JAVA_HOME}/bin/jar
fi

if [ ! -x "$JARCMD" ]; then
	echo "Missing java developers environment, unable to register services."
  	echo "After installing the prerequisite packages, run the following" 
	echo "command to complete the installation."
	echo "${BASEDIR}/usr/sadm/bin/smcconf repository add -f bean ${BASEDIR}/usr/sadm/lib/volmgr/SVM.jar" 
	exit 0
fi

# need cim installed to run mofcomp
MOFCOMP=${BASEDIR}/usr/sadm/bin/mofcomp
if [ ! -x ${MOFCOMP} ]; then
	echo "Missing mofcomp, unable to install the CIM objects. After"
	echo "installing the prerequisite packages, run the following command"
	echo "to complete the installation."
	echo "${BASEDIR}/usr/sadm/bin/mofcomp ${BASEDIR}/usr/sadm/mof/Solaris_VM1.0.mof"
	exit 0
fi

if [ ! -x /etc/rc2.d/S90wbem ]; then
	echo "Missing /etc/rc2.d/S90wbem, unable to install the CIM objects."
	echo "After installing the prerequisite packages, run the following"
	echo "command to complete the installation."
	echo "${BASEDIR}/usr/sadm/bin/mofcomp ${BASEDIR}/usr/sadm/mof/Solaris_VM1.0.mof"
	exit 0
fi

CLASSPATH="${BASEDIR}/usr/sadm/lib/wbem/cimapi.jar:${BASEDIR}/usr/sadm/lib/wbem/cimom.jar:${BASEDIR}/usr/sadm/lib/wbem/mofcomp.jar:${BASEDIR}/usr/sadm/lib/wbem/drmproviders.jar:${BASEDIR}/usr/sadm/lib/volmgr/SVM.jar"
export CLASSPATH

# we cannot tell if the cimom is running since SUNWwbmc runs the cimom
# under a process arg that is so long we can't see what is being run other
# than java

# first stop the cimom
# We cannot run 'mofcomp -o' when the cimom is running and we need to be sure
# the cimom is not running before we try to start it if we need to initialize
# the repository.
/etc/rc2.d/S90wbem stop >/dev/null 2>&1
sleep 5

# see of the repository has been initialized
if [ ! -f ${BASEDIR}/var/sadm/wbem/logr/store ]; then
    # if not, try to initialize it by running a command through the cim
    /etc/rc2.d/S90wbem start >/dev/null 2>&1
    sleep 5

    ${JAVACMD} com.sun.wbem.solarisprovider.lvm.Solaris_VMTc \
	-n Solaris_ComputerSystem >/dev/null 2>&1

    /etc/rc2.d/S90wbem stop >/dev/null 2>&1
    sleep 5
fi

# The mofcomp script always exits with 0, so we cannot redirect stdout and
# check the exit status.  Instead, just let the user see what mofcomp is
# doing so that they might see if an error occurs.
${MOFCOMP} -o ${BASEDIR}/var/sadm/wbem/logr ${BASEDIR}/usr/sadm/mof/Solaris_VM1.0.mof

# See if we need to install the smc java policy for SVM
POLICY_FILE="${BASEDIR}/usr/sadm/lib/smc/policy/smcwbemserver.policy"
if [ -f ${POLICY_FILE} ]; then
    # smc is installed, see if we need to update the java policy
    egrep -s volmgr ${POLICY_FILE}
    if [ $? -ne 0 ]; then
	echo >> ${POLICY_FILE}
	echo "/* permit SVM related classes and jars to do anything" >> ${POLICY_FILE}
	echo "*/" >> ${POLICY_FILE}
	echo "grant codeBase \"file:/usr/sadm/lib/volmgr/-\" {" >> ${POLICY_FILE}
	echo "    //permit all actions in SVM jars and classes" >> ${POLICY_FILE}
	echo "    permission java.security.AllPermission;" >> ${POLICY_FILE}
	echo "};" >> ${POLICY_FILE}
    fi
fi

/etc/rc2.d/S90wbem start >/dev/null 2>&1

exit 0
