#! /bin/sh
# This script reestablishes hard links that were broken after 
# the removal and reinstallation of the initd file. 
#

MV=/usr/bin/mv
PKGCOND=/usr/bin/pkgcond
RM="/usr/bin/rm -f"


# 
# Call removef with the proper options to handle rerooted
# installations. 

if [ "$ROOTDIR" != "" ]; then
        REMOVEF="removef -R $ROOTDIR"
else
        REMOVEF="removef"
fi


CheckZones()
{
        if [ -x /usr/bin/zonename ]; then
                ZONENAME=`/usr/bin/zonename`
                if [ "$ZONENAME" = "global" ]; then
                        GLOBAL_ZONE=true
                else
                        GLOBAL_ZONE=false
                fi
        else
                # Unable to determine zone
                GLOBAL_ZONE=true
        fi
}


# When the version of the file that is listed below (and is delivered by this 
# patch) was placed on the system, the inode number changed from its original 
# value.  Therefore, the hard link must be reestablished and the contents 
# database updated with this infomation.

remove_links () {

pkginfo -R $ROOTDIR -q  SUNWwbcor
if [ $? -eq 0 ]
then
        $REMOVEF SUNWwbcor /etc/rc0.d/K36wbem > /dev/null 2>&1
        $RM ${ROOTDIR}/etc/rc0.d/K36wbem > /dev/null 2>&1 
        $REMOVEF SUNWwbcor /etc/rc1.d/K36wbem > /dev/null 2>&1
        $RM ${ROOTDIR}/etc/rc1.d/K36wbem > /dev/null 2>&1
        $REMOVEF SUNWwbcor /etc/rc2.d/S90wbem > /dev/null 2>&1
        $RM ${ROOTDIR}/etc/rc2.d/S90wbem > /dev/null 2>&1
        $REMOVEF SUNWwbcor /etc/rcS.d/K36wbem > /dev/null 2>&1
        $RM ${ROOTDIR}/etc/rcS.d/K36wbem > /dev/null 2>&1
        $REMOVEF -f SUNWwbcor || exit 2
fi
}

register_smc () {

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

    if [ ! -d "${ROOTDIR}/var/sadm/smc" ] ; then
         return 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
            cp ${ROOTDIR}/usr/sadm/lib/volmgr/SVMServices.jar ${ROOTDIR}/var/sadm/smc/codebase/services/com.sun.admin.volmgr.server.SVMServices.jar
            cp ${ROOTDIR}/usr/sadm/lib/volmgr/VVolMgr.jar ${ROOTDIR}/var/sadm/smc/codebase/tools/com.sun.admin.volmgr.client.VVolMgr.jar
    fi
}

save_old_smc_config_files()
{
# After the WBEM server was made an SMF service, the creation of the /etc/smc/smcserver.config 
# file is no longer required.  Any file that is present on the system was created by the 
# by the WBEM server before the service was made as an SMF service.
#
# In order to resolve the issue that is reported in CR 6600869 (SMC will not work as a user if 
# the cmask is set to 077 in /etc/default/init file with JASS), the contents of the /etc/smc 
# directory needs to be removed.  SMC will work without any problem and the contents of the 
# /etc/smc directory will never be created or used again.

if [ -f ${ROOTDIR}/etc/smc/smcserver.config ]; then
        ${MV} ${ROOTDIR}/etc/smc/smcserver.config ${ROOTDIR}/etc/smc/smcserver.config_${PatchNum}
fi
}

ExecuteALLCmds()
{
  remove_links
  register_smc
  save_old_smc_config_files 
  return 0
}

determine_pkgcond () {

  if $PKGCOND is_whole_root_nonglobal_zone ; then
      # Execute non-global whole root zone commands.
      # Should be same action as the default action.

      ExecuteALLCmds() 
      return 0
  fi

  if $PKGCOND is_sparse_root_nonglobal_zone ; then
      # Execute non-global zone commands. Should be no action here
      return 0
  fi

  if $PKGCOND is_netinstall_image ; then
      # Execute commands applicable to patching the mini-root.
      # There are usually no actions to take here since your patching
      # the mini-root on an install server.
      return 0
  fi

  if $PKGCOND is_mounted_miniroot ; then
      # Execute commands specific to the mini-root
      return 0
  fi

  if $PKGCOND is_diskless_client ; then
      # Execute commands specific to diskless client
      return 0
  fi

  if $PKGCOND is_alternative_root ; then
      # Execute commands specific to an alternate root
      ExecuteALLCmds
      return 0
  fi

  if $PKGCOND is_global_zone ; then
      # In a global zone.
      # Execute all commands.
      ExecuteALLCmds
      return 0
  fi

}

# Main

ZONENAME=global

[ -x /sbin/zonename ] && ZONENAME=`/sbin/zonename`

if [ -x $PKGCOND ] ; then
  determine_pkgcond && exit 0 || exit 1
else
  CheckZones
  if [ "$GLOBAL_ZONE" = "true" ]; then
       ExecuteALLCmds
  fi
fi

exit 0

