#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2002,2019 
# 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 
# sccsid = "@(#)39   1.6   src/rsct/cfg_access/uncfgrpd.sh, cfg.access, rsct_rady, rady2035a 11/12/15 16:44:07"
######################################################################
# name: uncfgrpd: this will stoprpnode and cleanup all rpd definitions
######################################################################


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

RSCTBIN=/opt/rsct/bin

#stoprpnode if the node is in peer domain
if [[ -f ${RSCTBIN}/ct_clusterinfo ]]; then
	set -A clinfo $(${RSCTBIN}/ct_clusterinfo -r -c)
	REALM=${clinfo[0]}
	CLUSTER_NAME=${clinfo[1]}
fi
if [[ -z ${CLUSTER_NAME} ]]; then
	CLUSTER_NAME=IW       # assume IW mode
fi
if [[ -z ${REALM} ]]; then
        REALM=CLUSTER
fi

if [[ -n ${CLUSTER_NAME} && ${CLUSTER_NAME} != "IW" && ${REALM} != "CAA" && -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
		CLUSTER_NAME=$(${RSCTBIN}/ct_clusterinfo -c 2> /dev/null)
		if [[ -z $CLUSTER_NAME || $CLUSTER_NAME = "IW" ]]; then
			break
		else
			sleep 2
			(( i = $i - 2 ))
		fi
	done
    fi
fi


# walk-through each cluster definition and remove rpd definitions
#
if [[ ${REALM} != "CAA" ]]
then
	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
fi

# 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
