#! /bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2000,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 
#*===========================================================================*/
#*                                                                           */
#* Module Name:  cfgct                                                       */
#*                                                                           */
#* Description:                                                              */
#*      Script to unconfigure the RSCT subsystems at de-installation         */
#*                                                                           */
#*===========================================================================*/
#*   @(#)11   1.12   src/rsct/rmc/mcdaemon/uncfgct.sh, mcdaemon, rsct_relgh, relghs001a 11/28/06 14:58:46

# Set path to known value
PATH=/usr/sbin/rsct/install/bin:/usr/sbin/rsct/bin:/usr/bin:/bin:/usr/sbin
export PATH

CTROOTDIR=/var/ct		# root directory for all RSCT run-time data
SUBSYSGRP=rsct			# group name for RMC daemon and security daemon
RMGRP=rsct_rm			# group name for all RSCT resource managers
HAGSGRP=cthags			# group name for all HAGS daemons
HATSGRP=cthats			# group name for all HATS daemons

CTHAGSCTRL=/usr/sbin/rsct/bin/cthagsctrl
CTHATSCTRL=/usr/sbin/rsct/bin/cthatsctrl
CFGRMCSNMP=/usr/sbin/rsct/install/bin/cfgrmcsnmp

# Get option arguments; -n means clean up the node id file

cleannodeid=0

while getopts :n opt
do
	case $opt in 
		n) cleannodeid=1;;
	esac
done

#============================================================================
#
# Step 1: stop all RSCT subsystems, unless INUCLIENTS is set
# or INUBOSTYPE is set
#
#============================================================================

WAITLIMIT=40		# number of seconds to wait for subsystems to stop
PIDLIST=""

if [[ -z "$INUCLIENTS" && -z "$INUBOSTYPE" ]]
then
	# get process IDs of all RMC related daemons

	PROCLIST=$(LC_ALL=C lssrc -g ${SUBSYSGRP} 2> /dev/null | grep "active" | \
		sed -n -e"s/^ *[^ ]* *[^ ]* *\([0-9]*\) *active/\1/p" | tr '\n' ',')

	# get process IDs of all resource managers

	list=$(LC_ALL=C lssrc -g ${RMGRP} 2> /dev/null | grep "active" | \
		sed -n -e"s/^ *[^ ]* *[^ ]* *\([0-9]*\) *active/\1/p" | tr '\n' ',')

	if [[ -n "${list}" ]]
	then
		PROCLIST=${PROCLIST}${list}
	fi

	# get process IDs of all HAGS daemons

	list=$(LC_ALL=C lssrc -g ${HAGSGRP} 2> /dev/null | grep "active" | \
		sed -n -e"s/^ *[^ ]* *[^ ]* *\([0-9]*\) *active/\1/p" | tr '\n' ',')

	if [[ -n "${list}" ]]
	then
		PROCLIST=${PROCLIST}${list}
	fi

	# get process IDs of all HATS daemons

	list=$(LC_ALL=C lssrc -g ${HATSGRP} 2> /dev/null | grep "active" | \
		sed -n -e"s/^ *[^ ]* *[^ ]* *\([0-9]*\) *active/\1/p" | tr '\n' ',')

	if [[ -n "${list}" ]]
	then
		PROCLIST=${PROCLIST}${list}
	fi

	# if any daemons or RMs are running

	if [ -n "${PROCLIST}" ]
	then
		PROCLIST=${PROCLIST%,}
		stopsrc -c -g ${SUBSYSGRP} > /dev/null 2>&1
		stopsrc -c -g ${RMGRP} > /dev/null 2>&1
		stopsrc -c -g ${HAGSGRP} > /dev/null 2>&1
		stopsrc -c -g ${HATSGRP} > /dev/null 2>&1
		(( i = ${WAITLIMIT} ))
		while (( $i > 0 ))
		do
			/bin/ps -p ${PROCLIST} > /dev/null 2>&1
			if [[ $? -ne 0 ]]
			then
				break
			else
				sleep 2
				(( i = $i - 2 ))
			fi
		done
		/bin/ps -p ${PROCLIST} > /dev/null 2>&1
		if [[ $? -eq 0 ]]
		then
			print -n "Cannot stop all subsystems in groups $SUBSYSGRP, "
			print    "$RMGRP, $HAGSGRP or $HATSGRP"
			exit 1
		fi
	fi
fi


#============================================================================
#
# Step 2: delete the HAGS subsystem(s), if installed
#
#============================================================================

if [[ -x ${CTHAGSCTRL} ]]
then
	if ! ${CTHAGSCTRL} -d
	then
		exit 1
	fi
fi


#============================================================================
#
# Step 3: delete the HATS subsystem(s), if installed
#
#============================================================================

if [[ -x ${CTHATSCTRL} ]]
then
	if ! ${CTHATSCTRL} -d
	then
		exit 1
	fi
fi


#============================================================================
#
# Step 4: Unconfigure any "bridges" to/from RMC
#
#============================================================================

if [[ -x ${CFGRMCSNMP} ]]
then
	${CFGRMCSNMP} -u
fi

#============================================================================
#
# Step 5: delete the resource manager subsystems
#
#============================================================================

names=$(LC_ALL=C lssrc -g ${RMGRP} 2> /dev/null | sed -n -e"1d;s/^ *\([^ ]*\) .*$/\1/p" | tr '\n' ' ')
for ssname in $names
do
	rmssys -s $ssname >/dev/null	# take subsystem out of SRC
done


#============================================================================
#
# Step 6: delete the RMC subsystem
#
#============================================================================

if ! rmcctrl -d
then
	exit 1
fi

# remove the security subsystem

ctcasctrl -d

#============================================================================
#
# Step 7: remove all directories and files in /var/ct
#
#============================================================================

# get list of files and directories in $CTROOTDIR

for name in $(ls ${CTROOTDIR} 2> /dev/null)
do

	# Remove all files/directories except for cfg and lck

	if [[ $name != "cfg" && $name != "lck" ]]
	then
		rm -rf $CTROOTDIR/$name
	fi

done

#
# if the node id is not to be cleaned up, try to make sure the node id file is
# in /etc.
#
# if the node id is to be cleaned up, remove the node id files.
#

if [[ $cleannodeid -eq 0 ]]
then

	if [[ ! -f /etc/ct_node_id ]]
	then
		if [[ -f $CTROOTDIR/cfg/ct_node_id ]]
		then
			cp -p $CTROOTDIR/cfg/ct_node_id /etc/ct_node_id
		fi
	fi

else

	rm -f $CTROOTDIR/cfg/ct_node_id /etc/ct_node_id

fi

# remove cluster configuration files

rm -rf $CTROOTDIR/cfg/*
rm -rf $CTROOTDIR/lck/*

exit 0
