#!/bin/sh

# Copyright 08/21/99 Sun Microsystems, Inc. All Rights Reserved.
#pragma ident  "@(#)wbemconfig.sh	1.81 03/04/28 SMI"

ECHO=/bin/echo
GREP=/usr/bin/grep
PGREP=/usr/bin/pgrep
PKILL=/usr/bin/pkill
#
#

PKG=SUNWwbcor
BASEDIR=`/bin/pkgparam $PKG BASEDIR 2>/dev/null`
if [ -z "$BASEDIR" ]    # pkg not installed or installed several times
then
        BASEDIR=/    # set default. Maybe the dir is nfs mounted
fi

# WBEM services directory
WBSERHOME=${BASEDIR}usr/sadm
USRLIB=${WBSERHOME}/lib
WBSERLIB=${WBSERHOME}/lib/wbem
WBSERBIN=${WBSERHOME}/bin
WBSERMOF=${WBSERHOME}/mof
WBSEREXT=${WBSERHOME}/lib/wbem/extension
WBEMLOGDIR=${BASEDIR}var/sadm/wbem/logr
JSLOGDIR=${BASEDIR}var/sadm/wbem/log
WBEMLOGROOT=${BASEDIR}var/sadm/wbem
#
# Libraries required by providers
PROVREQLIB=${BASEDIR}usr/snadm/lib


ARCH=`uname -p`
JAVACMD="java"
JAVAOPTS="-server -Xmx128m"

# Add the security manager stuff
JAVAOPTS="${JAVAOPTS} -Djava.security.manager -Djava.security.policy=${WBSERLIB}/cimom.policy"

# Add code to prevent the RMI GC Deamon from running every minute
JAVAOPTS="${JAVAOPTS} -Dsun.rmi.dgc.server.gcInterval=0x7fffffffffffffff -Dsun.rmi.dgc.client.gcInterval=360000"

if [ "$BASEDIR" != "/" ]; then
    JAVAOPTS="${JAVAOPTS} -DBaseDir=${BASEDIR}"
fi

FLAG=$1
if [ -z "$FLAG" ]; then
    FLAG="notsetup"
fi
OLDLOGR=$2
if [ -z "$OLDLOGR" ]; then
    OLDLOGR=${WBEMLOGROOT}/logru3
fi

set `/usr/bin/id`
if [ $1 != "uid=0(root)" ]; then
        $ECHO "You must be root to run this command"
        exit 1
fi

CIMOMSERVER=com.sun.wbem.bootstrap.StartWBEMServices
CONVERTER=com.sun.wbem.upgrade.Convert
UPGRADER=com.sun.wbem.upgrade.RepositoryUpgrader
COMSUNUPGRADER=com.sun.wbem.upgrade.ComSunUpgrader
CIMOMPID=${WBEMLOGDIR}/cimomserver.pid
startCimom () {
        [ -d ${WBEMLOGDIR} ] || mkdir -pm 0700 ${WBEMLOGDIR}
	umask 066
	pid=$$
        $ECHO ${pid} > ${CIMOMPID}; /bin/chmod 0440 ${CIMOMPID}
	exec ${JAVACMD} $JAVAOPTS $CIMOMSERVER ${WBSERLIB}/WBEMServices.properties "$@"
}
# --------- end startCimom

setJavaPath() {
  if [ -z "$JAVA_HOME" ]
  then 
    JAVA_HOME=/usr/java
    JAVAPATH=${JVBASEDIR}/usr/java/bin
    JAVALIBS=${JVBASEDIR}/usr/java/lib
  else 
    JAVAPATH=${JAVA_HOME}/bin
    JAVALIBS=${JAVA_HOME}/lib
  fi
  PATH=${JAVAPATH}:${PATH}:$OLDPATH
  LD_LIBRARY_PATH=${JAVALIBS}:${LD_LIBRARY_PATH}
  export PATH
  export LD_LIBRARY_PATH
# jre doesn't update CLASSPATH if a CLASSPATH is set !
  if [ "${JAVACMD}" = "jre" ]
  then
    CLASSPATH=${JAVA_HOME}/lib/rt.jar:${JAVA_HOME}/lib/i18n.jar:${CLASSPATH}
    CLASSPATH=${JAVA_HOME}/classes:${JAVA_HOME}/lib/classes.zip:${CLASSPATH}
    export CLASSPATH
  fi
}
# --------- end setJavaPath


OLDPATH=$PATH
PATH=/usr/sbin:/sbin:/usr/bin:${WBSERLIB}

LD_LIBRARY_PATH=${WBSERLIB}:${PROVREQLIB}:${LD_LIBRARY_PATH}

# Set primary directory and jar files on class path
CLASSPATH=${WBSERLIB}/wbemstartup.jar

export PATH
export LD_LIBRARY_PATH
export CLASSPATH

setJavaPath

#
#---------
#
SPACEPID=${JSLOGDIR}/space.pid
findJavaSpace () {
    /bin/rm -f ${SPACEPID}
    pjs=`/bin/ps -ef | /bin/awk '{print $3 " " $2}' | grep \^${rpid}' ' | /bin/awk '{print $2}'`
    #The second one is the child of the first
    cjs=`/bin/ps -ef | /bin/awk '{print $3 " " $2}' | grep \^${pjs}' ' | /bin/awk '{print $2}'`
    echo ${cjs} " " ${pjs} > ${SPACEPID}; /bin/chmod 0440 ${SPACEPID}
}
# --------- end findJavaSpace
#

setConvertClassPath () {
    CLASSPATH=${CLASSPATH}:$WBSERLIB/comsunrepository.jar:$WBSERLIB/sunwbem.jar:$WBSERLIB/spaces.jar:$WBSERLIB/cimapi.jar:$WBSERLIB/wbem.jar:$WBSERLIB/cimom.jar:$WBSERLIB/suncimom.jar
    export CLASSPATH
}

case "${FLAG}" in
'datastore')
	setConvertClassPath

	#---Save lock, PID and Database files remove other files in log directory
	if [ ! -d ${JSLOGDIR}/outriggerDir ]; then
	    exit 1
	else
	    cp ${JSLOGDIR}/outriggerDir/JavaSpace.* ${WBEMLOGROOT}
	fi
	[ ! -f ${JSLOGDIR}/*.lock ] || /usr/bin/mv -f ${JSLOGDIR}/*.lock ${WBEMLOGROOT}
	/usr/bin/mv -f ${JSLOGDIR}/*.pid ${WBEMLOGROOT}
	rm -rf ${JSLOGDIR}; mkdir -pm 0700 ${JSLOGDIR}/outriggerDir
	[ ! -f ${WBEMLOGROOT}/*.lock ] || /usr/bin/mv -f ${WBEMLOGROOT}/*.lock ${JSLOGDIR}
	/usr/bin/mv -f ${WBEMLOGROOT}/*.pid ${JSLOGDIR}
	/usr/bin/mv -f ${WBEMLOGROOT}/JavaSpace.* ${JSLOGDIR}/outriggerDir


	#---Starting RMID
	RMIDPID=${JSLOGDIR}/rmid.pid
	rmid -J-Dsun.rmi.activation.execPolicy=none -C-Dcom.sun.jini.outrigger.maxOps=100 -C-Dcom.sun.jini.outrigger.basicspace.reapingInterval=5000 -log ${JSLOGDIR} &
	pid=$!
	$ECHO ${pid} > ${RMIDPID}; /bin/chmod 0440 ${RMIDPID}

	#---Start java space
	sleep 20
	# For i386 turn off java (just in time) compiler
	JITSTATE=""
	if [ ${ARCH} = "i386" ]; then
	    JITSTATE="-Djava.compiler=none"
	fi
	java -jar -Djava.security.policy=${WBSERLIB}/policyBooks.all -Dcom.sun.jini.outrigger.spaceName=JavaSpaces -Dcom.sun.jini.outrigger.maxOps=100 -Dcom.sun.jini.outrigger.basicspace.reapingInterval=5000 ${WBSERLIB}/outrigger.jar file:///${WBSERLIB}/outrigger-dl.jar ${WBSERLIB}/policyBooks.all ${JSLOGDIR}/outriggerDir none ${JITSTATE} &

	sleep 25

	rpid=`cat ${RMIDPID}`
	findJavaSpace
;;

convert)
#
# If this is the first time we are running, we need to copy the precompiled
# datastore

# Check for appropriate JDK version 1.2.1_04a
	/etc/init.d/init.wbem stop
# If cimom.jar does not exist, quit
	[ -f $WBSERLIB/cimom.jar ] || exit 1

	SAVEDCLASSPATH=$CLASSPATH
	# Check if we have an old reliable log repository
	if [ -d ${OLDLOGR} ]; then
	    # Check if this is a com.sun or javax version
	    VERSION=`cat $OLDLOGR/README | $GREP 'javax\.1'`
	    if [ -n "$VERSION" ]
	    then
	    # This is a javax repository
		UPGRADECLASS=$UPGRADER
	    else
	    # This is a com.sun repository
		UPGRADECLASS=$COMSUNUPGRADER
	    fi
	    [ -d ${WBEMLOGDIR} ] || mkdir -pm 0700 ${WBEMLOGDIR}
	    ${JAVACMD} $JAVAOPTS -DpassAllProviderCheck=true $CIMOMSERVER ${WBSERLIB}/WBEMServices.properties &
	    pid=$!
	    setConvertClassPath
	    ${JAVACMD} $UPGRADECLASS root ${OLDLOGR}
	    kill -9 $pid
	fi

	# Check if we used to have JavaSpaces; we have to convert the store

	# if logprepatch directory contains JavaSpace stuff; Do convert
	if [ ! -d ${JSLOGDIR}/outriggerDir ]; then 
	    if [ -d ${WBEMLOGROOT}/logprepatch/outriggerDir ]; then
		mv ${JSLOGDIR} ${JSLOGDIR}.bak 2>/dev/null
		mv ${WBEMLOGROOT}/logprepatch ${JSLOGDIR}	
	    fi
	fi 

	if [ -d ${JSLOGDIR}/outriggerDir ]; then
	    if [ ! -d ${WBEMLOGDIR}/notFirstTime ]; then
		mkdir -pm 0700 ${WBEMLOGDIR}/notFirstTime
		cp ${WBSERLIB}/Logfile.1 ${WBEMLOGDIR}
		cp ${WBSERLIB}/Snapshot.1 ${WBEMLOGDIR}
		cp ${WBSERLIB}/Version_Number ${WBEMLOGDIR}
		cp ${WBSERLIB}/store ${WBEMLOGDIR}
	    fi

	    setConvetClassPath
	    ${JAVACMD} $JAVAOPTS $CONVERTER ${WBEMLOGDIR}/succ ${WBEMLOGDIR}/dup ${WBEMLOGDIR}/err ${WBEMLOGDIR}/cycle

	    # Clean the java space processes and move the datastore
	    for daemon in space rmid; do
		pidfile=/var/sadm/wbem/log/$daemon.pid
		if [ -f $pidfile ]; then
			pid=`/usr/bin/cat $pidfile`
			[ "$pid" -gt 0 ] && kill $pid >/dev/null 2>&1
			rm -f $pidfile
		fi
	    done

	    mv ${JSLOGDIR} ${JSLOGDIR}.js.old

	    if [ -d ${JSLOGDIR}.bak ]; then
		mv ${JSLOGDIR}.bak ${JSLOGDIR} 2>/dev/null
	    fi
	fi
	CLASSPATH=$SAVEDCLASSPATH
	/etc/init.d/init.wbem start
;;

*)
	# If CIM Object Manager is already running, quit
	if [ -f ${BASEDIR}/var/sadm/wbem/logr/cimomserver.pid ]
	then
	    ps -ef | grep -v grep | grep `cat ${BASEDIR}/var/sadm/wbem/logr/cimomserver.pid` >/dev/null 2>&1
            [ $? -eq 0 ]  && exit 0
        fi
        startCimom

;;

esac

exit 0
