#!/bin/sh

#
# Copyright 2002-2003 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#

# From SUNWudlm/postremove
#

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

UDLMCTL=${FS_ROOTDIR}/opt/SUNWudlm/bin/udlmctl

if [ -r $UDLMCTL ]
then
	rm $UDLMCTL
fi


# 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 -R $ROOTDIR $MY_PKGNAME $TRUE_LINK | \
      while read pathname
      do
        echo "Removing symlink $pathname"
        /bin/rm -f $pathname
      done
     /usr/sbin/removef -R $ROOTDIR -f $MY_PKGNAME
   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
}

#------------------------------------
# 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 -R $ROOTDIR $MY_PKGNAME $LINK_DIR d 0755 root bin
   /usr/sbin/installf -f -R $ROOTDIR $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 -R $ROOTDIR $MY_PKGNAME ${TRUE_LINK}=${ADD_LINK_TARGET} s
     /usr/sbin/installf -f -R $ROOTDIR $MY_PKGNAME
     if [ $? -ne 0 ]; then
        return 1
     fi
   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=`grep "PATCHLIST" $pkginfo | \
                  grep $2 | sed "s/.*$2-//" | 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 `grep "^PATCH_INFO" $pkginfo | grep Obsoletes: | \
                  sed 's/.*Obsoletes://' |  sed 's/Requires.*//' | \
                  grep $2 | sed "s/.*$2-//" | 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
}

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

# Determine which patch rev is being backedout.
# PatchNum is inherited from backoutpatch

patch_rev=`echo ${PatchNum} | nawk -F- '{print $2}'`

#The following table shows when a rev of the patch is 
#installed what link it will add and what link it will
#remove.
#
# Patch Rev      Symlinks Added           Symlinks Removed
#---------------------------------------------------------
#  01 - 04
#
#  05              rc2.d/15_cvm             rc2.d/05_cvm


# Do the right thing based on which patch revision is being
# backedout and what patch revision remains installed.

# See what version of the patch remains installed after this one
# was backedout.

get_patchrev SUNWcvmr 115062
patchrev_remain=$?

if [ $patch_rev -gt 04 ]; then
   if [ $patchrev_remain -lt 05 ]; then
   #undo the symlink actions performed in rev 07
   echo "Removing symlink usr/cluster/lib/ucmm/reconf.d/rc2.d/15_cvm"
   remove_it $PKGINST usr/cluster/lib/ucmm/reconf.d/rc2.d/15_cvm
   echo "Adding symlink  usr/cluster/lib/ucmm/reconf.d/rc2.d/05_cvm"
   add_link $PKGINST usr/cluster/lib/ucmm/reconf.d/rc2.d/05_cvm ../../../../../../opt/SUNWcvm/bin/cvmreconfig
      if [ $? -ne 0 ]; then
         exit 1
      fi
   fi
fi

exit 0
