#!/bin/sh

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

#------------------------------------
# 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 [ $ROOTDIR = "/" ]; 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
}

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

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

	exit ${exit_code}
}

###########################################
#
# remove_SUNWscucm_edits <filename>
#
#	Backout all lined from the given ascii <filename>
#	indicated by the SUNWscucm comment tag lines.
#
###########################################
remove_SUNWscucm_edits()
{
	update_file=$1

	# If the file does not exist, return without error
	if [ ! -f "${update_file}" ]; then
		return 0
	fi

	while :
	do
		grep "${STARTTAG}" ${update_file} >/dev/null 2>&1
		case $? in
		2)	# error reading file
			echo "${PROG}:  error reading ${update_file}"
			return 1
			;;

		0)	# grep found the string, so get rid of the entries
			ed -s ${update_file} << EOF >/dev/null 2>&1
/${STARTTAG}/,/${ENDTAG}/d
w
q
EOF
			if [ $? -ne 0 ]; then
				echo "${PROG}: problem updating ${update_file}"
				return 1
			fi
			;;

		*)
			break
			;;
		esac
	done

	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
}

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

# 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 SUNWudlmr 111857
patchrev=$?
if [ $patchrev -gt 01 ]; then
   exit 0
fi

get_patchrev SUNWudlm 111857
patchrev=$?
if [ $patchrev -gt 01 ]; then
   exit 0
fi

get_patchrev SUNWscucm 111857
patchrev=$?
if [ $patchrev -gt 01 ]; then
   exit 0
fi

###########################################################################
#
# postremove for SUNWudlmr
#
###########################################################################

# Name of package
PKGINST=SUNWudlmr

# Don't execute if the package is not installed
# or a dependent patch is installed.
if [ -d /var/sadm/pkg/${PKGINST} ]; then
   #-----------------------------
   # Remove the symlinks that got added by the patch install.
   #-----------------------------
   remove_it $PKGINST usr/cluster/lib/ucmm/reconf.d/rc7.d/05_udlm
   if [ $? -ne 0 ]; then
      exit 1
   fi 
      
   #-----------------------------
   # Add back the symlinks that were removed by the patch install.
   #-----------------------------
   echo "The following symlinks are being restored."
   add_link $PKGINST usr/cluster/lib/ucmm/reconf.d/rc3.d/05_udlm ../../../../../../opt/SUNWudlm/bin/udlmreconfig
   if [ $? -ne 0 ]; then
      exit 1
   fi 
   
fi

###########################################################################
#
# postremove for SUNWscucm
#
#	* Remove our changes to /etc/inetd.conf
#	* Remove our changes to /etc/rpc
#
###########################################################################

PATH=/usr/bin:/usr/sbin
export PATH

PKGNAME=SUNWscucm
STARTTAG="Start of lines added by ${PKGNAME}"
  ENDTAG="End   of lines added by ${PKGNAME}"
PROG=${PKGNAME}.postinstall

TMPFILES=

errs=0

if [ -d /var/sadm/pkg/${PKGNAME} ]; then

   # catch common signals
   trap 'echo "${PROG}: caught signal";  cleanup 3' 1 2 3 15
   remove_SUNWscucm_edits /etc/inetd.conf	|| errs=`expr ${errs} + 1`
   remove_SUNWscucm_edits /etc/rpc		|| errs=`expr ${errs} + 1`

   if [ ${errs} -ne 0 ]; then
   	cleanup 1
   else
   	cleanup 0
   fi
fi

