#!/bin/ksh
#
# Copyright 2002-2003 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#pragma	ident	"@(#)rac_setup.ksh	1.9	03/09/10 SMI"
#
#############################################################
# 
# RAC Framework resource group configuration program
# 
# This program is invoked by system management tools for 
# configuring RAC Framework resource group.
# 
#############################################################

export PATH="/bin:/usr/bin:/usr/cluster/bin:/usr/cluster/lib/sc:/usr/cluster/lib/ucmm"

#############################################################
# Include ucmm_reconf.common library
# This library has functions to get RAC resource names
# and RAC RG
#############################################################

. /usr/cluster/lib/ucmm/ucmm_reconf.common

typeset -r DEFAULT_RAC_RG="rac-framework-rg"
typeset -r DEFAULT_RAC_FRAMEWORK_RS="rac_framework"
typeset -r DEFAULT_RAC_UDLM_RS="rac_udlm"
typeset -r DEFAULT_RAC_CVM_RS="rac_cvm"
typeset -r DEFAULT_RAC_HWRAID_RS="rac_hwraid"
typeset -r DEFAULT_RAC_SVM_RS="rac_svm"

FRAMEWORK_RT=SUNW.rac_framework
UDLM_RT=SUNW.rac_udlm
CVM_RT=SUNW.rac_cvm
HWRAID_RT=SUNW.rac_hwraid
SVM_RT=SUNW.rac_svm

NODELIST=""
GLOBAL_RESOURCE_NAME=""
GLOBAL_RG_NAME=""
VERBOSE_FLAG=""
LOG_FILE=""

typeset -r SCRGADM=scrgadm
typeset -r SCSWITCH=scswitch
typeset -r CFGMATCH=/usr/cluster/lib/ucmm/cfgmatch

set -mufp

COMPONENT_FRAMEWORK="rac_framework"
COMPONENT_UDLM="rac_udlm"
COMPONENT_CVM="rac_cvm"
COMPONENT_HWRAID="rac_hwraid"
COMPONENT_SVM="rac_svm"


#############################################################
#  print_err <formattext> <parameters>
#
#  This functions writes output to stderr
# 
#############################################################
print_err()
{
	typeset msg_format=${1:-""}
	typeset msg

	shift
	msg=$(/usr/bin/printf "${msg_format}" ${@:-} )

	print -r -u2 -- "$msg"
}

#############################################################
#  print_warn <formattext> <parameters>
#
#  This functions writes output to stderr
# 
#############################################################
print_warn()
{
	typeset msg_format=${1:-""}
	typeset msg

	shift
	msg=$(/usr/bin/printf "${msg_format}" ${@:-} )

	print -r -u2 -- "$msg"
}


#############################################################
# run_command
#############################################################
run_command()
{

	typeset rc=0

	typeset command_line="$@"

        if [ "$VERBOSE_FLAG" == "YES" ]; then
		print "${command_line}"
		print ""
	fi

	[ -z "${command_line}" ] && return 0

	${command_line}
	rc=$?

	if [ -n "${LOG_FILE}" ]; then
		print "${command_line}" >> "${LOG_FILE}"
	fi

	return $rc

}
#############################################################
# register_rac_rg <rg>
#
#  This function registers resource group for RAC resources
#  
#
#############################################################
register_rac_rg()
{
	typeset rg_name=${1:-""}

	# Check if RG exists
	rg_exists "$rg_name"

	if  [ $? -eq 0 ]; then
		return 0
	fi
	
	if [ -z "$rg_name" ]; then
		return 1
	fi

	# Read nodelist

	if [ -z "$NODELIST" ]; then

		new_nodelist=""
		NODELIST="$(scha_cluster_get -O ALL_NODENAMES)"
		for node in $NODELIST; do
			if [ -n "$new_nodelist" ]; then 
		  		new_nodelist="$new_nodelist,$node"
		  	else
		  		new_nodelist="$node"
		  	fi
		done
	else
		new_nodelist="$NODELIST"
	fi

#	[ "$VERBOSE_FLAG" == "YES" ] && print "Node list is: $new_nodelist";

	typeset -i nodecount
	nodecount=$(print "$new_nodelist" | sed 's/,/ /g' | wc -w)

#	[ "$VERBOSE_FLAG" == "YES" ] && print "Node count is: $nodecount";

	# Add RG
	#set -x
	run_command "$SCRGADM -a -g $rg_name  -y maximum_primaries=$nodecount \
-y desired_primaries=$nodecount -y nodelist=$new_nodelist -y RG_mode=Scalable"

}

#############################################################
#
# register_rt <name>
#   
#   Argument 1: resource type name
#
#   If resource type is not registered, then scrgadm command
#   is invoked to register the resource type
#
#   Return code: 
#	0 - success
#    <> 0 - failure. Will return return code of scrgadm
#
#############################################################
register_rt()
{
	typeset pattern="${1}:*"

	rt_registered "${pattern}"

	if  [ $? -eq 0 ]; then
		return 0
	fi
 
	run_command "$SCRGADM -a -t "${1}""

	return $?
}

#############################################################
#
# unregister_rt <name>
#
#   Argument 1: resource type name
#
#   If resource type is  registered, then scrgadm command
#   is invoked to unregister the resource type
#
#   Return code: 
#	0 - success
#    <> 0 - failure. Will return return code of scrgadm
#
#
#############################################################

unregister_rt()
{
	typeset pattern="${1}:*"

	rt_registered "${pattern}"

	if  [ $? -ne 0 ]; then
		# RT not registered
		return 0
	fi
 
	run_command "$SCRGADM -r -t ${1}"

	return $?
}

#############################################################
#
# Verify_rac_rg
#
#     This function verifies that the resource group specified
#     in argument can be configured as RAC resource group and
#     the RAC resources don't exist in any other resource group 
#
#############################################################
verify_rac_rg()
{

	typeset rg_name="${1:-}"
	typeset rs_name=""
	typeset rs_group=""
	
        rs_name=$(get_framework_resource)
        if [ -n "$rs_name" ]; then
		rs_group=$($SCHA_RS_GET -O GROUP -R "${rs_name}")

		if [ "$rs_group" != "$rg_name" ]; then
			print_err "$(gettext "Oracle RAC framework resource \
is configured in the resource group %s." )" "$rs_group"
		return 1

		fi
	fi

	return 0
}

#############################################################
#
# create_resource <resource_type> <default_name>
# 
# uses global variables
#   GLOBAL_RESOURCE_NAME
#   GLOBAL_RG_NAME
#
#############################################################

create_resource()
{

  typeset rc=0
  typeset rt_name="$1"
  typeset resource_name="$2"
  typeset rg_name="$GLOBAL_RG_NAME"
  shift
  shift
  typeset option="${@:-}"


  [ -z "${resource_name}" ] && return 1
  [ -z "${rg_name}" ] && return 1

  register_rt ${rt_name}
  rc=$?

  if [ $rc -eq 0 ]; then
  	run_command "$SCRGADM -a -j ${resource_name} -g ${rg_name} -t ${rt_name} ${option}"

	# 'scrgadm' can return  non zero, in case of warnings, such as 
	# WARNING: operation succeeded, but some of the nodes on which VALIDATE would 
	# have run are currently down
	# In case of warnings, return code will be >= 201
	# (as described in scha_priv.h)
	#

	rc=$?

	if [ $rc -eq 0 ]; then 
		return 0
	fi

	
	if [ $rc -ge 201 ]; then
		#
		# Warning during scrgadm
		# Verify that resource really exists
		# If resource was created inspite warning, 
		# return success
		#

		resource_configured ${resource_name}
		rc=$?
	fi

  fi

  return $rc
}

#############################################################
# create_framework_rs <resource name>
#
#  Create RAC framework resource based on SUNW.rac_framework
#  resource type
#
#
#############################################################
create_framework_rs()
{
	create_resource ${FRAMEWORK_RT} ${1:-DEFAULT_RAC_FRAMEWORK_RS}
}

#############################################################
# create_udlm_rs <resource name>
#
#  Create RAC udlm resource based on SUNW.rac_udlm
#  resource type
#
#
#############################################################
create_udlm_rs()
{
	typeset option="-y resource_dependencies=$(get_framework_resource)"
	typeset value=""
	typeset conf_file="/opt/SUNWudlm/etc/udlm.conf"

	#
	# Look at udlm.conf file and create command line to set
	# extension properties
	#
	# This is to ensure that if RAC framework RG is being added
	# on top of existing framework and udlmm.conf file was
	# modified by customer
	#
	#

	value=$($CFGMATCH udlm.start_timeout ${conf_file})
	[ -n "${value}" ] && option="${option} -x udlm_start_step_timeout=${value}"

	value=$($CFGMATCH udlm.abort_timeout ${conf_file})
	[ -n "${value}" ] && option="${option} -x udlm_abort_step_timeout=${value}"
	
	value=$($CFGMATCH udlm.step1_timeout ${conf_file})
	[ -n "${value}" ] && option="${option} -x udlm_step1_timeout=${value}"
	
	value=$($CFGMATCH udlm.step2_timeout ${conf_file})
	[ -n "${value}" ] && option="${option} -x udlm_step2_timeout=${value}"
	
	value=$($CFGMATCH udlm.step3_timeout ${conf_file})
	[ -n "${value}" ] && option="${option} -x udlm_step3_timeout=${value}"
	
	value=$($CFGMATCH udlm.step4_timeout ${conf_file})
	[ -n "${value}" ] && option="${option} -x udlm_step4_timeout=${value}"
	
	value=$($CFGMATCH udlm.step5_timeout ${conf_file})
	[ -n "${value}" ] && option="${option} -x udlm_step5_timeout=${value}"

	value=$($CFGMATCH udlm.oracle.config.file ${conf_file})
	[ -n "${value}" ] && option="${option} -x oracle_config_file=${value}"

	value=$($CFGMATCH udlm.port ${conf_file})
	[ -n "${value}" ] && option="${option} -x port=${value}"

	value=$($CFGMATCH udlm.num_ports ${conf_file})
	[ -n "${value}" ] && option="${option} -x num_ports=${value}"

	create_resource ${UDLM_RT} ${1:-DEFAULT_RAC_UDLM_RS} ${option}
}

#############################################################
# create_cvm_rs <resource name>
#
#  Create RAC cvm resource based on SUNW.rac_cvm
#  resource type
#
#
#############################################################
create_cvm_rs()
{
	typeset option="-y resource_dependencies=$(get_framework_resource)"

	typeset value=""
	typeset conf_file="/opt/SUNWcvm/etc/cvm.conf"

	#
	# Look at cvm.conf file and create command line to set
	# extension properties
	# This is to ensure that if RAC framework RG is being added
	# on top of existing framework and cvm.conf file was
	# modified by customer
	#
	#

	value=$($CFGMATCH cvm.start_timeout ${conf_file})
	[ -n "${value}" ] && option="${option} -x cvm_start_step_timeout=${value}"

	value=$($CFGMATCH cvm.stop_timeout ${conf_file})
	[ -n "${value}" ] && option="${option} -x cvm_stop_step_timeout=${value}"

	value=$($CFGMATCH cvm.abort_timeout ${conf_file})
	[ -n "${value}" ] && option="${option} -x cvm_abort_step_timeout=${value}"
	
	value=$($CFGMATCH cvm.return_timeout ${conf_file})
	[ -n "${value}" ] && option="${option} -x cvm_return_step_timeout=${value}"
	
	value=$($CFGMATCH cvm.step1_timeout ${conf_file})
	[ -n "${value}" ] && option="${option} -x cvm_step1_timeout=${value}"
	
	value=$($CFGMATCH cvm.step2_timeout ${conf_file})
	[ -n "${value}" ] && option="${option} -x cvm_step2_timeout=${value}"
	
	value=$($CFGMATCH cvm.step3_timeout ${conf_file})
	[ -n "${value}" ] && option="${option} -x cvm_step3_timeout=${value}"
	
	value=$($CFGMATCH cvm.step4_timeout ${conf_file})
	[ -n "${value}" ] && option="${option} -x cvm_step4_timeout=${value}"
	
	value=$($CFGMATCH cvm.port.vxclust ${conf_file})
	[ -n "${value}" ] && option="${option} -x vxclust_port=${value}"
	
	value=$($CFGMATCH cvm.port.vxclust_nports ${conf_file})
	[ -n "${value}" ] && option="${option} -x vxclust_num_ports=${value}"
	
	value=$($CFGMATCH cvm.port.vxkmsgd ${conf_file})
	[ -n "${value}" ] && option="${option} -x vxkmsgd_port=${value}"
	
	value=$($CFGMATCH cvm.port.vxconfigd ${conf_file})
	[ -n "${value}" ] && option="${option} -x vxconfigd_port=${value}"
	
	create_resource ${CVM_RT} ${1:-DEFAULT_RAC_CVM_RS} ${option}
}

#############################################################
# create_hwraid_rs <resource name>
#
#  Create RAC HWRAID resource based on SUNW.rac_chwraid
#  resource type
#
#
#############################################################
create_hwraid_rs()
{
	typeset option="-y resource_dependencies=$(get_framework_resource)"
	create_resource ${HWRAID_RT} ${1:-DEFAULT_RAC_HWRAID_RS} ${option}
}

#############################################################
# create_svm_rs <resource name>
#
#  Create RAC SVM resource based on SUNW.rac_svm
#  resource type. (Solaris Volume Manager)
#
#
#############################################################
create_svm_rs()
{
	typeset option="-y resource_dependencies=$(get_framework_resource)"
	create_resource ${SVM_RT} ${1:-DEFAULT_RAC_SVM_RS} ${option}
}


#############################################################
#
# pkg_added <name>
# returns 
# 	0 - if added
#       1 - if package not added
#
#############################################################

pkg_added()
{
	typeset pkgname=${1:-""}

	if  [ -n "${pkgname}" ]; then
		pkginfo -q $pkgname
		return $?
	fi

	return 1
}

#############################################################
# print_rac_components [ <verbose flag> ]
#
#  print RAC component list depending on the packages installed
#  on the node
# 
#   
#############################################################
print_rac_components()
{
	typeset verbose="${1:-}"

	pkg_added SUNWscucm 
	if [ $? -eq 0 ]; then
		print_name "${COMPONENT_FRAMEWORK}" $verbose
	fi

	pkg_added SUNWudlmr
	if [ $? -eq 0 ]; then
		print_name "${COMPONENT_UDLM}" $verbose
	fi

	pkg_added SUNWcvmr
	if [ $? -eq 0 ]; then
		print_name "${COMPONENT_CVM}" $verbose
	fi

	pkg_added SUNWschwr
	if [ $? -eq 0 ]; then
		print_name "${COMPONENT_HWRAID}" $verbose
	fi

	pkg_added SUNWscmd
	if [ $? -eq 0 ]; then
		print_name "${COMPONENT_SVM}" $verbose
	fi

	return 0
}

#############################################################
# print_name <component name> [ <verbose flag> ]
#
#  If verbose flag is specified, output is of the form
#   <component name> <space> <resource name> <space> <description>
#
#############################################################
print_name()
{
	typeset a_name=${1:-""}
	typeset verbose=${2:-""}

	printf "$a_name"

	case  $a_name in
		${COMPONENT_FRAMEWORK})
			[ "$verbose" == "YES" ] &&  \
				printf " ${DEFAULT_RAC_FRAMEWORK_RS} \"RAC Framework resource\""
			;;
		${COMPONENT_UDLM})
			[ "$verbose" == "YES" ] &&  \
				printf " ${DEFAULT_RAC_UDLM_RS} \"RAC UDLM resource\""
			;;
		${COMPONENT_CVM})
			[ "$verbose" == "YES" ] &&  \
				printf " ${DEFAULT_RAC_CVM_RS} \"RAC CVM resource\""
			;;
		${COMPONENT_HWRAID})
			[ "$verbose" == "YES" ] &&  \
				printf " ${DEFAULT_RAC_HWRAID_RS} \"RAC HWRAID resource\""
			;;
		${COMPONENT_SVM})
			[ "$verbose" == "YES" ] &&  \
				printf " ${DEFAULT_RAC_SVM_RS} \"RAC SVM resource\""
			;;
		*)
			;;
	    esac

	printf "\n"
}

#################################################################
# print_components 
#  Argument 1:
#	INSTALLED | NOT_INSTALLED
#
# Argument 2:
#	Verbose flag, default ""
# 
#
#################################################################

print_components()
{
	typeset list_type="${1:-}"
	typeset verbose="${2:-}"
	typeset rs_name=""
	typeset component_name=""

	print_rac_components | while read component_name; do

	   	rs_name="" 
		case  ${component_name} in
			${COMPONENT_FRAMEWORK})
				rs_name=$(get_framework_resource) 
				;;
			${COMPONENT_UDLM})
				rs_name=$(get_udlm_resource)
				;;
			${COMPONENT_CVM})
				rs_name=$(get_cvm_resource)
				;;
			${COMPONENT_HWRAID})
				rs_name=$(get_hwraid_resource)
				;;
			${COMPONENT_SVM})
				rs_name=$(get_svm_resource)
				;;
			*)
				continue
				;;
		esac

		if [ "${list_type}" == "INSTALLED" ]; then
			[ -z "${rs_name}" ] && continue
		elif [ "${list_type}" == "NOT_INSTALLED" ]; then
			[ -n "${rs_name}" ] && continue
		else
			continue
		fi

		print_name ${component_name} ${verbose}

	done
}
#################################################################
#
# Print name of RAC framework resoure group
#
#################################################################
print_rac_framework_rg()
{
				
	rac_rg=$(get_rac_rg)
	[ -z "${rac_rg}" ] && rac_rg="${DEFAULT_RAC_RG}"

	print "${rac_rg}"

	return 0
}

#################################################################
# 
# add_rac_component
# 
#    Add RAC components
#
#	return codes:
#		0 - Operation successful
#		1 - Error in resource creation
#		254 - Incorrect options or invalid names
#		255 - RAC framework resource exists and operation is
#		    requested to create all resources
#
#################################################################

add_rac_component()
{
	typeset component="${1:-}"
	typeset framework_rs=""
	typeset framework_rg=""

	framework_rs=$(get_framework_resource)

	# If global RG name is specified, verify that it 
	# matches that of RAC framework rs if configured

	if [ -n "$GLOBAL_RG_NAME" ]; then

		if [ -n "${framework_rs}" ]; then
			verify_rac_rg "$GLOBAL_RG_NAME"
			if [ $? -ne 0 ]; then

				framework_rg=$(get_rac_rg)
				print_err "$(gettext "Resource group name specified does not match RAC framework resource group name %s")"  "$framework_rg"
				return 254

			fi
		fi
	fi


	if [ "$component" = ALL ]; then 

		# Check if resources have been already configured
		# If yes then don't proceed, return 2

		if [ -n "${framework_rs}" ]; then
			print_warn "$(gettext \
"RAC framework resource group has been already configured in the system." )"


			return 255
		fi
		component_list=$(print_rac_components)

	else 
		component_list="$component"
	fi


	if [ -z "${component_list}" ]; then
		print_warn "$(gettext "RAC component not specified with add option")"
		return 254
	fi

	#
	# Configure RAC framework resource first
	# if it is present in the component list
	# 
	for item in $component_list; do

        	name="${item#*=}"
        	component="${item%%=*}"
	
		case  $component in
		    ${COMPONENT_FRAMEWORK})
			#
			# Verify that RAC Framework resource is not configured
			#
	
			if [ -n "${framework_rs}" ]; then
				if [ "${framework_rs}" = "${name}" ]; then
					print_warn "$(gettext \
"RAC framework resource has been already configured in the system." )"
					continue
				else
					print_err \
"$(gettext "RAC framework resource %s has been already configured \
in the system. Cannot configure  another RAC framework resource.")" \
"${framework_rs}"
					return 1

				fi

			fi

			#
			# Now register RAC RG
			#
			if [ -z "$GLOBAL_RG_NAME" ]; then
			 	GLOBAL_RG_NAME=${DEFAULT_RAC_RG}
			fi

        		register_rac_rg "$GLOBAL_RG_NAME"

			#
			# Now register RAC RG
			#
			create_framework_rs "$name"
			if [ $? -ne 0 ]; then
				return 1
			fi
			;;
			
		    *)
			# Don't process any other component in this loop
			;;

		esac
	done

	framework_rs=$(get_framework_resource)

	if [ -z "$framework_rs" ]; then
		print_err "$(gettext "RAC framework resource must be \
configured before other components")"

			return 1
	fi

	#
	# Now RAC framework resource is configured in the system
	#
	#
	if [ -z "$GLOBAL_RG_NAME" ]; then
	 	GLOBAL_RG_NAME=$(get_rac_rg)
		if [ -z "$GLOBAL_RG_NAME" ]; then
			print_err "$(gettext "Unable to get RAC resource \
group name")"
			return 1
		fi
	fi
	
	
		
	for item in $component_list; do

        	name="${item#*=}"
        	component="${item%%=*}"
	
		case  $component in
			${COMPONENT_UDLM})
				create_udlm_rs "$name"
				if [ $? -ne 0 ]; then
					# Failed to create resource
					# do not proceed further
					return 1
				fi

				;;

			${COMPONENT_CVM})
				create_cvm_rs "$name"
				if [ $? -ne 0 ]; then
					# Failed to create resource
					# do not proceed further
					return 1
				fi

				;;
			${COMPONENT_HWRAID})
				create_hwraid_rs "$name"
				if [ $? -ne 0 ]; then
					# Failed to create resource
					# do not proceed further
					return 1
				fi

				;;
			${COMPONENT_SVM})
				create_svm_rs "$name"
				if [ $? -ne 0 ]; then
					# Failed to create resource
					# do not proceed further
					return 1
				fi

				;;
			${COMPONENT_FRAMEWORK})
				;;
			*)
				print_err "$(gettext \
				    "Unknown component name %s")" "$component"
				return 1
				;;
		esac
	done

	return 0
}

#################################################################
#
# remove_cvm_rs
#
#################################################################

remove_cvm_rs()
{
	RAC_RS=$(get_cvm_resource)

	if [ -n "$RAC_RS" ]; then
		run_command "$SCSWITCH -n -j $RAC_RS"
		run_command "$SCRGADM -r -j $RAC_RS"
	fi

	unregister_rt $CVM_RT

	return $?
}

#################################################################
#
#################################################################

remove_udlm_rs()
{
	RAC_RS=$(get_udlm_resource)

	if [ -n "$RAC_RS" ]; then
		run_command "$SCSWITCH -n -j $RAC_RS"
		run_command "$SCRGADM -r -j $RAC_RS"
	fi

	unregister_rt $UDLM_RT

	return $?
}

#################################################################
#
#################################################################

remove_hwraid_rs()
{
	RAC_RS=$(get_hwraid_resource)

	if [ -n "$RAC_RS" ]; then
		run_command "$SCSWITCH -n -j $RAC_RS"
		run_command "$SCRGADM -r -j $RAC_RS"
	fi

	unregister_rt $HWRAID_RT

	return $?
}

#################################################################
#
#################################################################

remove_svm_rs()
{
	RAC_RS=$(get_svm_resource)

	if [ -n "$RAC_RS" ]; then
		run_command "$SCSWITCH -n -j $RAC_RS"
		run_command "$SCRGADM -r -j $RAC_RS"
	fi

	unregister_rt $SVM_RT

	return $?
}


#################################################################
#
#################################################################

remove_framework_rs()
{
	RAC_RS=$(get_framework_resource)

	if [ -n "$RAC_RS" ]; then
		run_command "$SCSWITCH -n -j $RAC_RS"
		run_command "$SCRGADM -r -j $RAC_RS"
	fi

	unregister_rt $FRAMEWORK_RT

	return $?
}

#################################################################
#
#################################################################

remove_framework_rg() 
{

	rg_name="${1:-}"


	[ -z "$rg_name" ] && return 0

	rg_exists $rg_name && run_command "$SCRGADM -r -g $rg_name"

	return $?
}

#################################################################
#
#################################################################

remove_rac_component()
{
	typeset rac_rg="$GLOBAL_RG_NAME"

	[ -z "${rac_rg}" ] && rac_rg=$(get_rac_rg)
	[ -z "${rac_rg}" ] && rac_rg="${DEFAULT_RAC_RG}"


	typeset component="$1"

	    case  $component in
		${COMPONENT_FRAMEWORK})
			remove_framework_rs
			;;
		${COMPONENT_UDLM})
			remove_udlm_rs
			;;
		${COMPONENT_CVM})
			remove_cvm_rs
			;;
		${COMPONENT_HWRAID})
			remove_hwraid_rs
			;;
		${COMPONENT_SVM})
			remove_svm_rs
			;;
		ALL)
			remove_cvm_rs || return 1
			remove_svm_rs || return 1
			remove_hwraid_rs || return 1
			remove_udlm_rs || return 1
			remove_framework_rs || return 1
			remove_framework_rg $rac_rg || return 1
			;;	
		*)
			;;
	    esac
	
	return $?
}


#################################################################
#
#################################################################

change_nodelist()
{

	if [ -z "$GLOBAL_RG_NAME" ]; then
		GLOBAL_RG_NAME=$(get_rac_rg)
	fi

	if [ -z "$GLOBAL_RG_NAME" ]; then
		GLOBAL_RG_NAME="${DEFAULT_RAC_RG}"
	fi

	if [ -z "$GLOBAL_RG_NAME" ]; then
		print_err "$(gettext "Resource group name not specified")"
		return 1
	fi

	if [ -z "$NODELIST" ]; then
		print_err "$(gettext "Nodelist not specified")"
		return 1
	fi


        typeset -L nodecount=$(print "$NODELIST" | sed 's/,/ /g' | wc -w)

        # Change nodelist
	run_command "$SCRGADM -c -g $GLOBAL_RG_NAME -y maximum_primaries=$nodecount \
 -y desired_primaries=$nodecount -y nodelist=$NODELIST"

	return $?

}

#################################################################
#
# If RAC framework daemons are running on this node, then bring
# RAC framework resource group to managed state
#
#################################################################

enable_rac_framework_rg()
{

	typeset UCMMD_NAME=ucmmd
	typeset pidlist

	pidlist=$(/usr/bin/pgrep -u 0 ${UCMMD_NAME})
	if [ $? -ne 0 ] ; then
        	return 0
        fi

	run_command "$SCSWITCH -Z -g $GLOBAL_RG_NAME"
}

#################################################################
#
#################################################################

usage()
{
print "$(gettext "RAC RG creation tool
Syntax: 
	$0  -h 
		help

	$0  -l 
		print RAC components

	$0  -p 
		print installed RAC components

	$0  -P 
		print RAC components not installed

	$0  -G 
		Print RAC framework RG name

	$0  -r <component>
		Remove RAC component

	$0  -f <log_file> 
		Log commands to the log_file

	$0  -R
		Remove ALL component

	$0  -v
		verbose

	$0 -a [ -j <component>[=<name>] ]..  [ -g <groupname> ] [ -N <nodelist> ] [ -v ]
		Add RAC component
		  Specify nodelist with rac_framework component
	e.g.
		$0 -a -j rac_framework -g MYRG -N pluck1,pluck2
		$0 -a -j ${COMPONENT_UDLM}=UDLM

	$0 -c -N <nodelist> [ -v ]
		Change nodelist
")"

}

#################################################################
#
#################################################################

main()
{
#typeset -ft $(typeset +f)

        typeset rc=0
        typeset add_list=""
	typeset option=""
	typeset verbose=""
	typeset print_option=""

	#
	# l - print RAC components based on packages
	# p - print components that are installed
	# P - print components that are not installed
	# G - print RAC Framework resource group name
	# a <component> - add component
	# N <nodelist> - add component
	#
	# 

        while getopts acvhlpPGRN:j:g:f: opt
        do
        case $opt in
                a)     option=ADD
			;;
                c)     option=CHANGE
			;;
                v)     verbose="YES"
			;;
		h) 	usage 	
			return
			;;
                N)      NODELIST="$OPTARG"
			;;
                j)     
			if [  -z "${GLOBAL_RESOURCE_NAME}" ]; then
				GLOBAL_RESOURCE_NAME="$OPTARG"
			else 
				GLOBAL_RESOURCE_NAME="${GLOBAL_RESOURCE_NAME} $OPTARG"
			fi
			;;
                f)      LOG_FILE="$OPTARG"
			;;
                g)      GLOBAL_RG_NAME="$OPTARG"
			;;
		l|p|P|G)	print_option="$opt"
			rc=0
			;;
                r)      option=REMOVE
			;;
                R)      GLOBAL_RESOURCE_NAME="ALL"
			option=REMOVE
			;;
                *)      rc=1
			print_err "Ignoring invalid option %s" "$opt"
                        ;;
        esac
        done

	if [ -n "$print_option" ]; then

            case ${print_option} in
		l)	print_rac_components "$verbose"
			rc=0
			;;
		p)	print_components INSTALLED "$verbose"
			rc=0
			;;
		P)	print_components NOT_INSTALLED "$verbose"
			rc=0
			;;
		G)	print_rac_framework_rg "$verbose"
			rc=0
			;;
		*)
			;;	
	    esac
	    return
	fi

	if [ -z "$option" ]; then
		print_err "$(gettext "Valid option not specified")"
		return 1
	fi
			
	VERBOSE_FLAG="$verbose"
	case $option in

		ADD)
			add_rac_component "$GLOBAL_RESOURCE_NAME"
			rc=$?
			if [ $rc -eq 0 ]; then
				# If RAC framework is already running
				# bring the RG to managed state
				#
				enable_rac_framework_rg
				rc=$?

			elif [ $rc -eq 255 ]; then
				# RAC framework resource is already configured
				# Skip configuration
				rc=0
			elif [ $rc -eq 1 ]; then
				print_err "$(gettext "Error in RAC framework resource group configuration.")"
				print_err "$(gettext "Backing out the changes.")"

				# Remove RAC framework resource group and components

				GLOBAL_RESOURCE_NAME="ALL"
				remove_rac_component "$GLOBAL_RESOURCE_NAME"
				rc=$?
			else
				# configuration error
				rc=1
			fi

			;;	
		CHANGE)
			change_nodelist "$GLOBAL_RG_NAME" "$NODELIST"
			rc=$?
			;;	
		REMOVE)
			remove_rac_component "$GLOBAL_RESOURCE_NAME"
			rc=$?
			;;
		*)
			;;	
	esac
	return $rc
	
}

main ${@:-""}

