#!/bin/sh

# For RTI 4636694
# After the patch is installed, we need to ...
#  1) Remove the old symlink if it exists.
#  2) Remove the old symlink from the /var/sadm/install/contents file.
#  3) Remove the directory the old symlink was in if it exists and
#     is empty.
#  4) Remove the directory the old symlink was in from the 
#     /var/sadm/install/contents file.
#  

#------------------------------------
# This function will remove the indicated file which belongs to the
# indicated package from the system and the contents file.
# It will then remove the files' parent directory if it's empty
# and remove it from the contents file.
#
#    remove_it <MY_PKGNAME> <SYMLINK>
#------------------------------------
remove_it()
{
   MY_PKGNAME=$1
   RM_LINK=$2

   # Get My BASEDIR
   PKG_BASEDIR=`/usr/bin/pkgparam ${MY_PKGNAME} BASEDIR`
   if [ "$PKG_BASEDIR" != "/" ]; then
      TRUE_LINK=${PKG_BASEDIR}/${RM_LINK}
   else
      TRUE_LINK=${PKG_BASEDIR}${RM_LINK}
   fi

   if [ $ROOTDIR = "/" ]; then
      FS_ROOTDIR=""
   else 
      FS_ROOTDIR="${ROOTDIR}"
   fi

   # If the link is present, remove it.
   if [ -h ${FS_ROOTDIR}${TRUE_LINK} ]
   then
     /usr/sbin/removef -R $ROOTDIR $MY_PKGNAME $TRUE_LINK |
      while read pathname
      do
        echo "Removing symlink $pathname"
        /bin/rm -f $pathname
      done
     /usr/sbin/removef -f -R $ROOTDIR $MY_PKGNAME
     if [ $? -ne 0 ]; then
        return 1
     fi
   fi
   
   # See if we can remove the directory
   LINK_DIR=`/bin/dirname ${TRUE_LINK}`
   if [ ! -d $LINK_DIR ]; then
      return 0
   fi

   # We can remove the directory because it's empty
   /usr/sbin/removef -R $ROOTDIR $MY_PKGNAME $LINK_DIR |
     while read pathname
     do
          echo "Removing directory $pathname"
          /bin/rmdir $pathname
     done
   /usr/sbin/removef -R $ROOTDIR -f $MY_PKGNAME
   if [ $? -ne 0 ]; then
      return 1
   fi

   return 0
}

##############################
# MAIN
##############################

# Name of package
PKGINST=SUNWcvmr


# Exit with a 0 if the package is not installed.
if [ ! -d /var/sadm/pkg/${PKGINST} ]; then
   exit 0
fi


#-----------------------------
# For each symlink to the removed, remove it if it exists and
# remove it's parent directory if it's empty after removal of
# the symlink.
#-----------------------------
echo "The following files are no longer part of this package and are being removed."
remove_it $PKGINST usr/cluster/lib/ucmm/reconf.d/rc2.d/05_cvm
if [ $? -ne 0 ]; then
   exit 1
fi 
   
exit 0
