#!/usr/bin/ksh

#
# See what revision of a patch is installed for a package
# Or what revision of the patch is obsoleted by another patch, for the 
# given package
# 
# Arguments: package patch
#
# Returns revision of specified patch, or 0 if no patch or package installed
#
# Assumptions:	pkginfo is in /var/sadm/pkg/<package> directory
#		Package's patches are listed in pkginfo file's PATCHLIST line
#		Obsoleted patches are listed in PATCH_INFO line(s)
#
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
}


#
# Undo ccd.database file(s) permissions back to 0644
#
undo_ccd_permissions()
{
	E=/etc/opt/SUNWcluster/conf

	[ -f ${E}/ccd.database.init ] && \
	    /bin/chmod -f 0644 ${E}/ccd.database.init

	[ -f ${E}/ccd.database ] && \
	    /bin/chmod -f 0644 ${E}/ccd.database

	[ -f ${E}/ccd.database.shared ] && \
	    /bin/chmod -f 0644 ${E}/ccd.database.shared

	[ -f ${E}/ccd.database.shadow ] && \
	    /bin/chmod -f 0644 ${E}/ccd.database.shadow

	[ -f ${E}/ccd.database.orig ] && \
	    /bin/chmod -f 0644 ${E}/ccd.database.orig
}


#
# main program
#

#
# Move ccd.database file permissions back to 0644, if we drop below
# 109210-11 patch level.
#
get_patchrev SUNWsc 109210
patchrev=$?
if [ $patchrev -lt 11 ]; then
	undo_ccd_permissions
fi


#
# ROOTDIR is inherited from patch scripts
#
#
# Remove symbolic links in SUNWsclb, SUNWsclbx and SUNWsc
# introduced by the patch.
#
# This is a workaround for a patchrm bug where symlinks do not
# get removed on backout and also handles not to remove them if
# this patch is installed on top of a previous version or
# accumulated version of this patch.  After files have been
# restored on backout, check to see if there is an existing source
# pointed to by the symlink, if so, assume a previous patch
# installed the file.  If no source exists, meaning sym link 
# points to nothing, remove the sym links. 

[ "$ROOTDIR" = / ] && ROOTDIR=""
 
TOP32=$ROOTDIR/opt/SUNWcluster/lib
TOP64=$ROOTDIR/opt/SUNWcluster/lib/sparcv9
LINK_lib=libscutil.so
BIN=/etc/init.d/initpmf

for SYMLINK in $TOP32/$LINK_lib $TOP64/$LINK_lib 
do
         if [ -L $SYMLINK -a ! -a $SYMLINK ]
         then    
            echo "Removing symlink $SYMLINK"
            rm -f $SYMLINK

            if echo $SYMLINK | egrep -s sparcv9
            then
                removef SUNWsclbx $SYMLINK
                removef -f SUNWsclbx
            else
                removef SUNWsclb $SYMLINK
                removef -f SUNWsclb
             fi
        fi
done
exit 0
