#!/bin/sh

#
# ident	"@(#)mofcomp.sh	1.25	00/10/16 SMI"
#
# Copyright (c) 1999 by Sun Microsystems, Inc.
# All rights reserved.
#

#
# Set localization environment variables
#
TEXTDOMAIN=SUNW_WBEM_SCRIPT
export TEXTDOMAIN
#
#
#unset ENV

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

# Get the path to sadm directory
SADMDIR=${BASEDIR}usr/sadm
getsadmdir () {
# Wbem could be installed on a user selected directory - when using Wbem installer -
# in that case wbemInstallDir.properties file will contain the installed directory path.
# Wbem installer uses java "user.home" system property to create this file.
if [ -f /wbemInstallDir.properties ]  # "user.home" = "/" on Solaris
then
    SADMDIR=`grep WBEMINSTALLDIRECTORY /wbemInstallDir.properties|cut -d"=" -f2`"/sadm"
elif [ -f /root/wbemInstallDir.properties ] # "user.home" = "/root" on Linux
then
    SADMDIR=`grep WBEMINSTALLDIRECTORY /root/wbemInstallDir.properties|cut -d"=" -f2`"/sadm"
else
    SADMDIR=..   # properties file not found, use relative path
fi
}
# --------- end getsadmdir
if [ ! -f $SADMDIR/lib/wbem/cimapi.jar ]   # If cimapi.jar does not exists
then
    getsadmdir
fi

USRLIB=${SADMDIR}/lib
WBSERLIB=${SADMDIR}/lib/wbem
WBSERBIN=${SADMDIR}/bin
WBSERMOF=${SADMDIR}/mof

JAVACMD="java"
#
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}:${WBSERMOF}:${WBSERBIN}

LD_LIBRARY_PATH=${WBSERLIB}:${LD_LIBRARY_PATH}
CLASSPATH=${WBSERLIB}:${WBSERLIB}/mofcomp.jar:${WBSERLIB}/cimapi.jar:${WBSERLIB}/cimom.jar:${WBSERLIB}/cimrepository.jar:${USRLIB}/xml.jar

export PATH
export LD_LIBRARY_PATH
export CLASSPATH

setJavaPath

# start MOF Compiler  
MOFCOMPILE=com.sun.wbem.compiler.mofc.CIM_Mofc
umask 066
${JAVACMD} $MOFCOMPILE "$@"
EXITVALUE=$?
if [ $EXITVALUE -ne 0 ]; then
	MSG=`gettext "Warning: MOF compiler failed"`
	echo "${MSG} $@ ; EXITVALUE=$EXITVALUE"
fi

exit 0
