#!/bin/sh

# For RTI 4711800
# Remove symlink only on Solaris 9 and we've reverted to a version of
# this patch which no longer installed the symlink.


#------------------------------------
# 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_link <MY_PKGNAME> <SYMLINK>
#------------------------------------
remove_link()
{
   MY_PKGNAME=$1
   RM_LINK=$2

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

   # If the link is present, remove it.
   if [ -h ${FS_LINK} ]
   then
     /usr/sbin/removef -R $ROOTDIR $MY_PKGNAME $RM_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 ${RM_LINK}`

   # We can remove the directory because if removef says it's ok.
   /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
}

# For RTI 4823783
# After the patch is removed, we need to ...
#  1) Remove the directory the man page was in if it exists and
#     is empty.
#  2) Remove the directory the man page was in from the 
#     /var/sadm/install/contents file.
#  

#------------------------------------
# This function will remove the indicated directory which 
# belongs to the indicated package from the system and 
# the contents file.
#
#    remove_directory <MY_PKGNAME> <DIRECTORY>
#------------------------------------
remove_directory()
{
   MY_PKGNAME=$1
   RM_DIR=$2

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

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

   # See if we can remove the directory
   if [ ! -d $TRUE_DIR ]; then
      return 0
   fi

   # We can remove the directory because it's empty
   /usr/sbin/removef -R $ROOTDIR $MY_PKGNAME $TRUE_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
}

#
# See what revision of a patch is installed for a package
# Or what revision of the patch is obsoleted by another patch.
# Arguments: package patch
# Returns revision of specified patch
# Returns 0 if no patch or package not installed
#
# Assumptions: pkginfo is in /var/sadm/pkg
# Package's patches are listed in pkginfo in the PATCHLIST line
# Obsoleted patches are listed in a PATH_INFO line
#
get_patchrev()
{
          pkginfo="/var/sadm/pkg/$1/pkginfo"
          if [ ! -f $pkginfo ]; then
                  # Package not installed
                  return 0
          fi

          # See if the patch is installed
          x=`/bin/grep "PATCHLIST" $pkginfo | \
                  /bin/grep $2 | /bin/sed "s/.*$2-//" | /bin/sed 's/ .*//'`
          if [ "$x" -ne 0 ]; then
                  return $x
          fi

          # See if the patch has been obsoleted by another patch
          maxrev=0
          for x in `/bin/grep "^PATCH_INFO" $pkginfo | /bin/grep Obsoletes: | \
                  /bin/sed 's/.*Obsoletes://' |  /bin/sed 's/Requires.*//' | \
                  /bin/grep $2 | /bin/sed "s/.*$2-//" | /bin/sed 's/ .*//'`
          do
                  if [ $maxrev -lt $x ]; then
                          maxrev=$x
                  fi
          done
          if [ "$maxrev" -ne 0 ]; then
                  return $maxrev
          fi

          # Patch is not installed or obsoleted
          return 0
}

###########################################
#
# Cleanup any temporary files on exit/interrupt
#
###########################################
cleanup()
{
	exit_code=$1

	if [ "${TMPFILES}" ]; then
		rm -f ${TMPFILES}
	fi

	exit ${exit_code}
}

##############################
# MAIN
##############################
trap 'echo "${PROG}: caught signal";  cleanup 3' 1 2 3 15

# Name of package
PKGINST=SUNWscorx
OS=`/bin/uname -r`

# Don't execute any of the patch backout instructions if
# a patch is still installed which depends on these actions.
# That patch will backout the changes when (if) it is removed.
get_patchrev $PKGINST 110651
patchrev=$?
if [ $patchrev -gt 10 -o "$OS" != "5.9" ]; then
   echo ""
else
   # Don't execute if the package is not installed
   if [ -d /var/sadm/pkg/${PKGINST} ]; then
      #-----------------------------
      # Remove the symlink that got added by the patch install.
      #-----------------------------
      remove_link $PKGINST /usr/cluster/lib/sparcv9/libC.so.5
      if [ $? -ne 0 ]; then
         exit 1
      fi 
   fi
fi


# Name of package
PKGINST=SUNWscor

# Don't execute if
# a patch is still installed which depends on these actions.
# That patch will backout the changes when (if) it is removed.
get_patchrev $PKGINST 110651
patchrev=$?
if [ $patchrev -gt 11 ]; then
   exit 0
fi

# Don't execute if the package is not installed
# or a dependent patch is installed.
if [ -d /var/sadm/pkg/${PKGINST} ]; then
   #-----------------------------
   # Remove the directories that got added by the patch install.
   #-----------------------------
   errs=0
   remove_directory ${PKGINST} ${PKGINST}/oracle_listener/man/man5 || errs=`expr ${errs} + 1`
   remove_directory ${PKGINST} ${PKGINST}/oracle_listener/man      || errs=`expr ${errs} + 1`
   remove_directory ${PKGINST} ${PKGINST}/oracle_server/man/man5   || errs=`expr ${errs} + 1`
   remove_directory ${PKGINST} ${PKGINST}/oracle_server/man        || errs=`expr ${errs} + 1`
   if [ ${errs} -ne 0 ]; then
        cleanup 1
   else
        cleanup 0
   fi
fi

exit 0
