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


#
#cdb_backout - remove the cdb entries from a cluster node.
#
function cdb_backout
{
#
# Gather the cluster name and CDB file location
# PatchNum variable is inherited from the backout patch script
#
# Should only be called on cluster nodes - not the admin w/s
#

DEF_CLS_NAME=/etc/opt/SUNWcluster/conf/default_clustername
CDB_LOC=/etc/opt/SUNWcluster/conf
 
if [ ! -f ${DEF_CLS_NAME} ]; then
        echo "${DEF_CLS_NAME} non-existent"
        exit 1
fi
 
read clust < $DEF_CLS_NAME
 
CDB_FL=$CDB_LOC/$clust.cdb
 
if [ ! -f ${CDB_FL} ]; then
        echo "${CDB_FL} non-existent"
        exit 1
fi

/usr/bin/cp ${CDB_FL} ${CDB_FL}.${PatchNum}

#
# handle the comment line first, check to see if this was 
# the first patch that put the comment in the file. If
# not put the comment back into the real file.
#

grep -v "^# RPC monitoring tunable parameters." ${CDB_FL}.${PatchNum} > ${CDB_FL}

oldcomment=`grep "^# RPC monitoring tunable parameters." /var/sadm/patch/${PatchNum}/cdb.${PatchNum}`

if [ "${oldcomment}" ]
	then
	echo ${oldcomment} >> ${CDB_FL}
fi

#
# now handle the actual values themselves.
#
# Build a file containing the key : value pairs which we can then parse
# individually.
#

/usr/bin/cp ${CDB_FL} ${CDB_FL}.${PatchNum}

grep -v "^rpcmon" ${CDB_FL}.${PatchNum} > ${CDB_FL}
grep "^rpcmon" ${CDB_FL}.${PatchNum} > ${CDB_FL}.rpcmon

params=`cat ${CDB_FL}.rpcmon | cut -f1`

#
# work through all the parameters in a loop.
#

for param in ${params}
do

	#
	# First, gather the value for this key.
	#

	value=`grep ${param} ${CDB_FL}.rpcmon | cut -d":" -f2 | cut -d" " -f2`

	#
	# Check the stored copy of the cdb file from the patch's application,
	# if the key : value pair existed in there then restore them into the
	# working cdb.
	#

	oldparam=`grep ${param} /var/sadm/patch/${PatchNum}/cdb.${PatchNum}`

	if [ "${oldparam}" ]
		then
		echo "${oldparam}" >> ${CDB_FL}
	fi

	#
	# Check the parameter's value against the default value - if it
	# changed the store a copy under /var/opt/SUNWcluster for reference
	#

	case ${param} in
		rpcmon.action)
			defvalue="abort"
			;;
		rpcmon.retries)
			defvalue="1"
			;;
		rpcmon.ival)
			defvalue="30"
			;;
		rpcmon.noresponse)
			defvalue="5"
			;;
		rpcmon.loops)
			defvalue="0"
			;;
		rpcmon.sleep)
			defvalue="2"
			;;
		esac

	if [ "${value}" != "${defvalue}" ]
		then
		echo "${param} : ${value}" >> /var/opt/SUNWcluster/cdb.paramlog
	fi

	#
	# and back round the loop for the next parameter
	#

done

#
# Remove our temporary working files and exit the function
#

if [ -r ${CDB_FL}.${PatchNum} ]
	then
	rm ${CDB_FL}.${PatchNum}
fi

if [ -r ${CDB_FL}.rpcmon ]
	then
	rm ${CDB_FL}.rpcmon
fi
}


#
# main program
#

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

#
# PatchNum & ROOTDIR are 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

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

TOP_rpc=$ROOTDIR/opt/SUNWcluster/etc/reconf/conf.d
SYMLINKS_rpc='rc12.d/90_rpcbindmon rcA.d/90_rpcbindmon rcK.d/90_rpcbindmon rcR.d/90_rpcbindmon'

for i in `echo $SYMLINKS_rpc`; do
        if [ -h $TOP_rpc/$i -a ! -a $TOP_rpc/$i ]
        then
           rm -f $TOP_rpc/$i
           removef SUNWsc $TOP_rpc/$i
           removef -f SUNWsc
          fi
        
done


if [ -f /etc/opt/SUNWcluster/conf/default_clustername ]; then
	cdb_backout
fi


exit 0
