#!/bin/ksh
#
# ident	"@(#)ipv6_check_ccr.sh	1.1	04/09/02 SMI"
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# Determine if IPv6 resource groups exist on this cluster by looking into
# the ccr files of all RGs.
#
# Returns 0 if no such RGs exist; non-0 otherwise.
#
# Used by pre-backout script during patch removal to decide if the removal
# can proceed.

PATH=/usr/bin:/usr/sbin
CCRDIR=/etc/cluster/ccr

BAD_PORT_LIST="udp6|tcp6"

# I18N
export TEXTDOMAINDIR=/usr/cluster/lib/locale
export TEXTDOMAIN=SUNW_SC_CMD
export LC_COLLATE=C


version=`/usr/cluster/bin/scinstall -p | cut -f1 -d'_'`
if [[ "$version" != 3.1 && "$version" != 3.1u1 && "$version" != 3.1u2 ]]; then
	#
	# We must be trying to downgrade to a 3.1u3 or later
	# version. Since IPv6 support started in 3.1u3, no more checking
	# is required. Note that patch install does not update the version.
	#
	exit 0
fi

#
# Look through each resource group ccr file
#
for rg_file in `ls $CCRDIR | grep "^rgm_rg_"`; do

	# Skip rg that are not actually in use
	grep "^${rg_file}$" $CCRDIR/directory >/dev/null 2>&1
	if [[ "$?" != 0 ]]; then
		continue
	fi

	#
	# Each resouce has its own line starting with "RS_".
	# Go through all resouces in this group one by one.
	#
	egrep -e "^RS_.*Port_list=" $CCRDIR/$rg_file | while read line
	do
		# Extract resource name
		rs_name=`echo "$line" | cut -f1 -d'	' | cut -f2 -d'_'`

		# Truncate line down to just the Port_list value
		line=`echo "$line" | sed 's/^.*Port_list=//' | cut -f1 -d';'`

		# Skip if the value does not contain "udp6" or "tcp6"
		echo "$line" | egrep -e "${BAD_PORT_LIST}" >/dev/null
		if [[ "$?" != 0 ]]; then
			continue
		fi

		# We have a "bad" resource. Add it to the list
		if [[ "$bad_rs_list" == "" ]]; then
			bad_rs_list="$rs_name"
		else
			bad_rs_list="${rs_name}, ${bad_rs_list}"
		fi
	done
done

if [[ "$bad_rs_list" == "" ]]; then
	exit 0
else
	printf "$(gettext '\
The following resource(s) are configured to work with IPv6\
and must be removed before proceeding:') \c"
	echo "$bad_rs_list"
	echo
	exit 1
fi
