#!/bin/ksh
#
# ident "@(#)smcwbemserver.sh	1.26 06/08/04 SMI"
# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.

. ${VIPER_HOME}/lib/functions

# add WBEM CLASSPATH and LD_LIBRARY_PATH
# we have to find out where WBEM stuff resides
# 1) if this is run from workspace
# 2) if this is run from toolkit
# 3) if this is run from installed packages..(most likely)
#
# Right now we force user to have /usr/sadm/lib/wbem dir with
# appropriate jars and libs in place.

WBSERHOME=${WBBASEDIR:-/}usr/sadm
WBSERLIB=${WBSERHOME}/lib/wbem

LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${WBSERLIB}:/usr/snadm/lib
PATH=${PATH}:/usr/sbin

CLASSPATH=${WBSERLIB}:${VIPER_JSDK}:${JARPATH}/smcwbemserver_rt.jar:${SERVER_RT}:${CLASSPATH}:${WBSERLIB}/prodregapi.jar:${WBSERLIB}/jini-core.jar:${WBSERLIB}/jini-ext.jar:${WBSERLIB}/outrigger.jar:${WBSERLIB}/transient-outrigger.jar:${WBSERLIB}/cimrepository.jar:${WBSERLIB}/snmpprovider.jar:${WBSERLIB}/jsnmpapi.jar:${WBSERLIB}/nativeprovider.jar:${WBSERLIB}/osserver.jar:${WBSERLIB}/mofcomp.jar:${WBSERLIB}/suncimom.jar:${WBSERLIB}/sunwbem.jar:${WBSERLIB}/wbemstartup.jar

umask 022

#Check if the SMC Server Key-Pair & Certificate exists or not and if it is valid
PROPDIR=${BASEDIR}/var/sadm/smc/properties
KEYSTORE=$PROPDIR/.server
KEYTOOL=${BASEDIR}/usr/bin/keytool
ALIAS=smcserverkey
KEYPASS=passphrase
STOREPASS=passphrase

if [ ! -d PROPDIR ]; then
	mkdir -p $PROPDIR
fi

#Check the certificate for validity.
if [ -f $KEYSTORE ]; then
	deleteFile=`/usr/bin/ls -l $KEYSTORE | /usr/bin/tr -s'' ' ' | /usr/bin/cut -d' ' -f8 | /usr/bin/grep ':' | /usr/bin/wc -l`
	[ $deleteFile -eq 0 ] && /usr/bin/rm -f $KEYSTORE
fi

#Re-create the certificate if it is not there.
if [ ! -f $KEYSTORE ]; then
        echo ""
        echo "Generating SMC Server Key-Pair & Certificate"

        CN=`/usr/bin/hostname`
        OU=`/usr/bin/domainname`

# If hostname and domainname are not set then set them
	if [ "x$CN" = "x" ]; then
		CN=`/usr/sbin/ifconfig -a | /usr/bin/grep inet | /usr/bin/cut -d' ' -f 2 | /usr/bin/grep -v '127.0.0.1' | /usr/bin/head -1`
		[ "x$CN" = "x" ] && CN="localhost"
	fi
	[ "x$OU" = "x" ] && OU="localdomain"
        $KEYTOOL -genkey -alias $ALIAS -keyalg RSA -keysize 1024 -dname "CN=$CN,OU=$OU,OU=SMC Server" -keypass $KEYPASS -storepass $STOREPASS -validity 365 -keystore $KEYSTORE
        chmod 700 $KEYSTORE
fi

java_home=
if [ "x$JAVA_HOME" != "x" ]
then
        java_home=$JAVA_HOME
fi

if [ "x$java_home" = "x" ]
then
        if [ -d /usr/java/bin ]
        then
                java_home=/usr/java
        fi
        if [ -d /usr/j2se/bin ]
        then
                java_home=/usr/j2se
        fi
fi

if [ "x$java_home" != "x" ]
then
        # SMC's root ca certificate store is a copy from the JDK
        cp -f $java_home/jre/lib/security/cacerts /var/sadm/smc/properties/cacerts
        chmod 644 /var/sadm/smc/properties/cacerts

        # Change the password to SMC's default. Need a utility to allow
        # admin to change this at a later date.
        $KEYTOOL -storepasswd -keystore /var/sadm/smc/properties/cacerts -new passphrase -storepass changeit
fi

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

${JAVA} "${JAVAARGS[@]}" \
	-Xmx128m \
	-Djava.security.policy=${VIPER_HOME}/policy/smcwbemserver.policy \
	-Dviper.argv0=$0 \
	-Dviper.home=${VIPER_HOME} \
	-Dviper.rthome=${SMC_RTHOME} \
	-Dviper.display=${X_DISPLAY} \
	-Dpropdir=${WBSERLIB} \
	${JAVARMIOPTS} \
	com.sun.management.viperimpl.server.ViperWbemServer "$@" |&

# Get information about the server command we just started
. "${VIPER_HOME}/lib/server_track.1"

while true; do
	sIFS="$IFS"
	IFS="
"
	read -p bkg
	[ $? -ne 0 ] && exit 1
	IFS="sIFS"
	if expr "$bkg" : '^[0-9]$' > /dev/null 2>&1; then
		code=$bkg
		break
	fi
	echo "$bkg"
done

# Get status of server command and do any cleanup in case
# it's in an unstable state.
. "${VIPER_HOME}/lib/server_track.2"

exit $code
