#!/bin/sh

# Copyright (c) 1996-2003 Sun Microsystems, Inc.  All Rights Reserved. Sun
# considers its source code as an unpublished, proprietary trade secret, and
# it is available only under strict license provisions.  This copyright
# notice is placed here only to protect Sun in the event the source is
# deemed a published work.  Dissassembly, decompilation, or other means of
# reducing the object code to human readable form is prohibited by the
# license agreement under which this code is provided to the user or company
# in possession of this copy.
#
# RESTRICTED RIGHTS LEGEND: Use, duplication, or disclosure by the Government
# is subject to restrictions as set forth in subparagraph (c)(1)(ii) of the
# Rights in Technical Data and Computer Software clause at DFARS 52.227-7013
# and in similar clauses in the FAR and NASA FAR Supplement.
#

SMCREG=${ROOTDIR}/usr/sadm/bin/smcregister
SMCDIR=${ROOTDIR}/usr/sadm/smc
SMCDIR2=${ROOTDIR}/usr/sadm/lib/smc
PRELOAD_DIR=${SMCDIR2}/lib/preload
SMCCOMPILE=${ROOTDIR}/usr/sadm/bin/smccompile
ADMIN_LIBDIR=${ROOTDIR}/usr/sadm/lib
TEMPFILE=/var/tmp/smcclassfile.$$

find_path_to_jh_jar_file() {

# Check JavaHelp jar file, we need to resolve it's full path
# in the face of symbolic links.  The reason for this is because
# the reference to the jar file may be a symbolic link to the
# the default JavaHelp jar file installed on the system, and we
# must have the real full path to the file so we can add it to
# the console's security policy file.
#
jh_jar=${PRELOAD_DIR}/jh.jar
if [ -h "${jh_jar}" ]; then

#   Need path to where link target is installed, but relative to
#   where it is located on the client.
    if [ "${ROOTDIR}" != "/" ]; then
        jh_jar=`echo ${jh_jar} | sed -e "s@^${PKG_INSTALL_ROOT}@@"`
    else

      # Must cd to where the symbolic link is located as a starting point
      # to follow the link.
      cd `dirname ${jh_jar}`

      # Resolve link to conclusion
      while [ -h "${jh_jar}" ]
      do
          jh_jar=`ls -al ${jh_jar} | awk '{print $NF}'`
      done

      # Resolve to full path, in case it's relative.
      cd `dirname ${jh_jar}`
      jh_jar=`pwd`/`basename ${jh_jar}`
    fi

fi
}

add_jh_jar_to_policy_file() {

    # Now add the jar to the console's security policy file.
    #
    CONSOLE_POLICY=${SMCDIR2}/policy/smcconsole.policy
    chmod 644 ${CONSOLE_POLICY}
    echo "" >> ${CONSOLE_POLICY}
    echo "grant codeBase \"file:${jh_jar}\" {" >> ${CONSOLE_POLICY}
    echo "\tpermission java.security.AllPermission;" >> ${CONSOLE_POLICY}
    echo "};" >> ${CONSOLE_POLICY}
    chmod 444 ${CONSOLE_POLICY}

}

# MAIN

if [ ! -d "${ROOTDIR}/var/sadm/smc" ] ; then
     exit 0
fi

if [ -d "${ROOTDIR}/var/sadm/smc/codebase" ]; then
        cp ${ROOTDIR}/usr/sadm/lib/wbem/cimapi.jar ${ROOTDIR}/var/sadm/smc/codebase/ALL@cimapi.jar
        cp ${ROOTDIR}/usr/sadm/lib/wbem.jar ${ROOTDIR}/var/sadm/smc/codebase/ALL@wbem.jar
        cp ${ROOTDIR}/usr/sadm/lib/wbem/providerutility.jar ${ROOTDIR}/var/sadm/smc/codebase/ALL@providerutility.jar
        cp ${ROOTDIR}/usr/sadm/lib/wbem/sunwbem.jar ${ROOTDIR}/var/sadm/smc/codebase/ALL@sunwbem.jar
fi

# Register tools and services
if [ -f ${SMCREG} ] && [ -d ${SMCDIR} ]; then
     ${SMCREG} tool -n com.sun.admin.patchmgr.client.VPatchMgr.jar \
         ${ADMIN_LIBDIR}/patchmgr/VPatchMgr.jar \
         ${ADMIN_LIBDIR}/patchmgr/VPatchMgr_classlist.txt \
         ${ADMIN_LIBDIR}/patchmgr/VPatchMgrInfo.xml

     ${SMCREG} tool -n com.sun.admin.patchmgr.cli.PatchMgrCli.jar \
         ${ADMIN_LIBDIR}/patchmgr/PatchMgrCli.jar \
         ${ADMIN_LIBDIR}/patchmgr/PatchMgrCli_classlist.txt \
         ${ADMIN_LIBDIR}/patchmgr/PatchMgrCliInfo.xml

         if [ -f ${SMCCOMPILE} ] && [ -f ${SMCREG} ]; then
                 ${SMCCOMPILE} -j library ALL /usr/sadm/lib/wbem/providerutility.jar \
                         > ${TEMPFILE}
                 ${SMCREG} library /usr/sadm/lib/wbem/providerutility.jar ${TEMPFILE} ALL
         fi

     rm -rf ${TEMPFILE}

     # CIL proposed mod for 550596 resolution
     # Unregister the old VSysInfo tool and register the new one
     ${SMCREG} tool -u com.sun.admin.sysinfo.client.VSysInfo.jar
     ${SMCREG} tool -n com.sun.admin.sysinfo.client.VSysInfo.jar \
	 ${ADMIN_LIBDIR}/sysinfo/VSysInfo.jar \
	 ${ADMIN_LIBDIR}/sysinfo/VSysInfo_classlist.txt \
	 ${ADMIN_LIBDIR}/sysinfo/VSysInfoInfo.xml
fi

find_path_to_jh_jar_file
add_jh_jar_to_policy_file

exit 0
