#! /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


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

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

# If ROOTDIR = "/", we want to prevent the beginning of the absolute path 
# name from being assigned "// instead of "/" in the restore_file function. 

if [ "$ROOTDIR" = "/" ]; then
   rootdir=""
else
  rootdir=$ROOTDIR
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
}

LocalZones()
{
# Any local zone stuff here,
# typically this fuction is not used
        return 0
}

# Due to the i.initd class action script's saving of a copy of the original 
# init.wbem file, we now need to restore the original source file back onto 
# the target system.

restore_file () {

if [ -f $rootdir/etc/init.d/init.wbem ] && [ -f $rootdir/etc/init.d/init.wbem.$PatchNum ]
  then
       ${MV} $rootdir/etc/init.d/init.wbem.$PatchNum $rootdir/etc/init.d/init.wbem
  elif [ ! -f $rootdir/etc/init.d/init.wbem.$PatchNum ] && [ ! -f $rootdir/etc/init.d/init.wbem ]
    then
       # neither backup nor original are present. ignore
       exit
  elif [ ! -f $rootdir/etc/init.d/init.wbem.$PatchNum ] && [ -f $rootdir/etc/init.d/init.wbem ]
    then

       cat << ENDCAT

          Backup file $rootdir/etc/init.d/init.wbem.$PatchNum 
          appears to have been removed.  Unable to restore 
          original $rootdir/etc/init.d/init.wbem file.  

ENDCAT

       exit
fi

}

# When the files listed below were restored, each inode number
# changed.  Therefore, each hard link must be reestablished and
# the contents database updated with this information.

restore_links () {

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

#     According to the installf man page, links should be specified as  path1=path2.  path1 indicates
#     the destination and path2 indicates the source file.  Can use "../../etc ..." notation to avoid 
#     having to have to specify ROOTDIR.

        ${INSTALLF} SUNWwbcor /etc/rc0.d/K36wbem=../../etc/init.d/init.wbem l > /dev/null 2>&1
        ${INSTALLF} SUNWwbcor /etc/rc1.d/K36wbem=../../etc/init.d/init.wbem l > /dev/null 2>&1
        ${INSTALLF} SUNWwbcor /etc/rc2.d/S90wbem=../../etc/init.d/init.wbem l > /dev/null 2>&1
        ${INSTALLF} SUNWwbcor /etc/rcS.d/K36wbem=../../etc/init.d/init.wbem l > /dev/null 2>&1
        ${INSTALLF} -f SUNWwbcor || exit 2

fi
}

copy_jar_files () {

     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
}

restore_old_smc_config_files()
{
# 
# Restore any SMC config file that was present on the system prior to the installation of this patch.
# The configuration file was created by the by the WBEM server before the service is made as an SMF 
# service and needs to be restored upon patch removal.
#

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

  elif [ ! -f $rootdir/etc/smc/smcserver.config_${PatchNum} ]
    then
        cat << ENDCAT

         Backup file $rootdir/etc/smc/smcserver.config_${PatchNum}
         appears to have been removed.  Unable to restore
         original $rootdir/etc/smc/smcserver.config.

ENDCAT

        exit
fi

}

ExecuteALLCmds()
{
  restore_file
  restore_links
  copy_jar_files
  restore_old_smc_config_files
  return 0
}

determine_pkgcond () {

  if $PKGCOND  is_whole_root_nonglobal_zone > /dev/null 2>&1 ; then
      # Execute non-global whole root zone commands.
      # Should be same action as the default action.
      return 0
  fi

  if $PKGCOND  is_nonglobal_zone > /dev/null 2>&1 ; then
      # Execute non-global zone commands. Should be no action here
      return 0
  fi

  if $PKGCOND is_netinstall_image > /dev/null 2>&1 ; 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 > /dev/null 2>&1 ; then
      # Execute commands specific to the mini-root
      return 0
  fi

  if $PKGCOND is_diskless_client > /dev/null 2>&1 ; then
      # Execute commands specific to diskless client
      return 0
  fi

  if $PKGCOND is_alternative_root > /dev/null 2>&1 ; then
      # Execute commands specific to an alternate root
      ExecuteALLCmds
      return 0
  fi

  if $PKGCOND is_global_zone > /dev/null 2>&1 ; then
      # In a global zone and system is mounted on /.
      # 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
  else
   LocalZones
  fi
fi

exit 0
