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

MV=/usr/bin/mv


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

# 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 ]
    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 source file was restored above, the inode number 
# has changes.  Therefore, we must reestablish the hard links 
# and update the contents database 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
}

restore_file
restore_links

exit 0
