#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2002,2007 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)39   1.3         src/rsct/cfg_access/uncfgrpd.sh, cfg.access, rsct_relgh, relghs001a 10/11/02 19:53:08
######################################################################
# name: uncfgrpd: this will stoprpnode and cleanup all rpd definitions
######################################################################


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

RSCTBIN=/usr/sbin/rsct/bin

#stoprpnode if the node is in peer domain
if [[ -f ${RSCTBIN}/ct_clusterinfo ]]; then
	clname=`${RSCTBIN}/ct_clusterinfo -c 2> /dev/null`
fi
if [[ -z ${clname} ]]; then
	clname=IW       # assume IW mode
fi

if [[ -n ${clname} && ${clname} != "IW" && -x ${RSCTBIN}/stoprpnode ]]
then
    MYID=$(LC_ALL=C ${RSCTBIN}/lsnodeid)
    MYNAME=$(LC_ALL=C lsrsrc -p0 -t -x -s "NodeIDs={0x$MYID}" IBM.PeerNode Name | tr '\"' " ")
    if [[ -n $MYNAME ]]; then
	${RSCTBIN}/stoprpnode $MYNAME 2> /dev/null
	# wait till cluster name becomes "IW"
	(( i = 30 ))
	while (( $i > 0 ))
	do
		clname=$(${RSCTBIN}/ct_clusterinfo -c 2> /dev/null)
		if [[ -z $clname || $clname = "IW" ]]; then
			break
		else
			sleep 2
			(( i = $i - 2 ))
		fi
	done
    fi
fi


# walk-through each cluster definition and remove rpd definitions
#
tokens=$(LC_ALL=C lsclcfg -adx)

for token in $tokens
do
	names=${token%:}
	CNAME=${names%:*}
	CID=${names#*:}
	if [[ $CNAME != "IW" ]]; then
		rmrpdomain -f $CNAME 
	fi
done

# Disable ConfigRM's autostart as no peer domains are defined
if [[ -f /var/ct/cfg/ConfigRM.autostart ]]
then
	rm -f /var/ct/cfg/ConfigRM.autostart
fi

exit 0
