#!/bin/sh
#
#pragma ident   "@(#)postremove 1.3     02/04/09 SMI"
#
# Copyright (c) 1998-2001 by Sun Microsystems, Inc.
# All rights reserved.
#
#

# For RTI 4636694
# After the patch is removed, we need to ...
#  1) Remove the symlink added by the patch.
#  2) Remove the symlink from the /var/sadm/install/contents file.
#  3) Remove the directory the symlink was in if it exists and
#     is empty.
#  4) Remove the directory the symlink was in from the 
#     /var/sadm/install/contents file.
#  5) We need to restore the symlink removed by postpatch
#  6) We need to add the restored symlink to the 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 [ "x$ROOTDIR" = "x/" ]; 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 $MY_PKGNAME $TRUE_LINK | \
      while read pathname
      do
        echo "Removing symlink $pathname"
        /bin/rm -f $pathname
      done
     /usr/sbin/removef -f $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 ${FS_ROOTDIR}$LINK_DIR ]; then
      return 0
   fi

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

   return 0
}

#------------------------------------
# This function will add the indicated symlink which belongs to the
# indicated package to the system and the contents file.
# It will create the files' parent directory if necessary
# and add it to the contents file.
#
#    add_link <MY_PKGNAME> <SYMLINK> <LINK_TARGET>
#------------------------------------
add_link()
{
   MY_PKGNAME=$1
   ADD_LINK=$2
   ADD_LINK_TARGET=$3

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

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

   # See if we need to add the directory
   LINK_DIR=`/bin/dirname ${TRUE_LINK}`

   echo "Restoring directory ${FS_ROOTDIR}$LINK_DIR"
   /usr/sbin/installf $MY_PKGNAME $LINK_DIR d 0755 root bin
   /usr/sbin/installf -f $MY_PKGNAME
   if [ $? -ne 0 ]; then
      return 1
   fi

   if [ ! -d ${FS_ROOTDIR}$LINK_DIR ]; then
      mkdir ${FS_ROOTDIR}$LINK_DIR
      chown root ${FS_ROOTDIR}$LINK_DIR
      chgrp bin ${FS_ROOTDIR}$LINK_DIR
      chmod 0755 ${FS_ROOTDIR}$LINK_DIR
   fi

   # If the link is not present, add it.
   if [ ! -h ${FS_ROOTDIR}${TRUE_LINK} ]
   then
     echo "Restoring symlink ${FS_ROOTDIR}$TRUE_LINK"
     /usr/sbin/installf $MY_PKGNAME ${TRUE_LINK}=${ADD_LINK_TARGET} s
     /usr/sbin/installf -f $MY_PKGNAME
     if [ $? -ne 0 ]; then
        return 1
     fi
   fi
   
   return 0
}

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

# Name of package
PKGINST=SUNWudlmr


# Exit with a 0 if a previous rev of the patch is still installed
# which relies on these symlinks. That patch will perform the undo
# actions when it is uninstalled.
#echo "Checking if dependent patch is installed."
#if [ -d /var/sadm/patch/111857-02 ]; then
#   exit 0
#fi


#-----------------------------
# Remove the symlinks that got added by the pkg install.
#-----------------------------
remove_it $PKGINST usr/cluster/lib/ucmm/reconf.d/rc7.d/05_udlm
if [ $? -ne 0 ]; then
   exit 1
fi 
      
#-----------------------------
# Since this script only gets executed if the pkg is removed,
# Don't add back any files which are associated with the pkg.
# Let the postbackout patch script do that if this pkg was
# installed by a patch.
#-----------------------------

exit 0
