#!/bin/sh

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

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

# After the patch is removed, we need to ...
#  1) Remove the empty upgrade/5.2.1 directory.
#  

#------------------------------------
# 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_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
   DIR_CONTENTS=`/bin/ls ${FS_ROOTDIR}${TRUE_DIR}`
   if [ "x$DIR_CONTENTS" != "x" ]; then
      # Directory is not empty so don't remove it.
      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=`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=SUNWspsms


# 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 what upgrade directory was added
#by which patch rev.
#
# Patch Rev      directory Added
#-------------------------------
#  01            upgrade/5.2.1


# 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 SUNWspsms 122990
patchrev_remain=$?

if [ $patch_rev -eq 01 ]; then
   # No rev of the patch remains.
   echo "Removing server/lib/upgrade/5.2.1 directory"
   remove_it $PKGINST server/lib/upgrade/5.2.1
   remove_it $PKGINST server/lib/upgrade
fi

exit 0
