#!/usr/bin/ksh93
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/sa/db2/sbin/cl_db2testtool.sh 1.5 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2005 
# 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 
# @(#)59      1.5 src/43haes/usr/sbin/cluster/sa/db2/sbin/cl_db2testtool.sh, hacmp.assist, 61haes_r714 4/7/05 21:37:00

# Read in the message catalog entries
. /usr/es/sbin/cluster/sa/db2/cat/cl_db2testtool

# Read in the DB2 defaults
. /usr/es/sbin/cluster/sa/db2/etc/db2_definitions

# Load the common functions, logmsg, dbgmsg, errmsg, abort
. /usr/es/lib/ksh93/common_functions.ksh93


function write_script
{
	typeset RESOURCE_GROUP=$1
	typeset FILENAME=$2

	KLIB=/usr/es/lib/ksh93
	FPATH=$KLIB/hacmp:$KLIB/sa:$KLIB/aix:$KLIB/aix/odm

	typeset NODES=$(KLIB_AIX_ODM_get_odm_fields "group=$RESOURCE_GROUP" "HACMPgroup" "nodes")
	typeset INSTANCE=$(KLIB_AIX_ODM_get_odm_fields "group=$RESOURCE_GROUP" "HACMPgroup" "sa_key")
	[[ -z $NODES ]] && abort 2 $RESOURCE_GROUP $RESOURCE_GROUP
	[[ -z $INSTANCE ]] && abort 3 $RESOURCE_GROUP

	typeset DB2_APPLICATION_SERVER=$(KLIB_HACMP_get_appserver $RESOURCE_GROUP)
	[[ -z $DB2_APPLICATION_SERVER ]] && abort 4 $RESOURCE_GROUP $INSTANCE

	typeset DB2_INSTANCE_START_SCRIPT=$(KLIB_AIX_ODM_get_odm_fields "name=$DB2_APPLICATION_SERVER" "HACMPserver" "start")
	[[ -z $DB2_INSTANCE_START_SCRIPT ]] && abort 5 $RESOURCE_GROUP $DB2_APPLICATION_SERVER

	typeset DB2HOMEVG=$(KLIB_SA_get_metadata "SA_DB2Instance" "$INSTANCE" "HOME_VOLUME_GROUP")
	[[ -z $DB2HOMEVG ]] && abort 6 $RESOURCE_GROUP $INSTANCE

	typeset DB2_SERVICE_IP_LABEL=$(KLIB_AIX_ODM_get_odm_fields "group=$RESOURCE_GROUP and name=SERVICE_LABEL" "HACMPresource" "value")
	[[ -z $DB2_SERVICE_IP_LABEL ]] && abort 7 $RESOURCE_GROUP $INSTANCE
	
	typeset PRIMARY_NODE=''
	typeset TAKEOVER_NODES=''

	for node in $NODES; do
		if [[ -z $PRIMARY_NODE ]]; then
			PRIMARY_NODE=$node
		else
			TAKEOVER_NODES="$node $TAKEOVER_NODES"
		fi
	done

	# TEST SECTION 1:
	## Test Fallover for each DB2 instance resource group takeover node
	###

	echo "NODE_DOWN_TAKEOVER,$PRIMARY_NODE,\"Stop cluster services with takeover on the primary node for DB2 instance resource group: $RESOURCE_GROUP\"" > $FILENAME
	echo "NODE_UP,$PRIMARY_NODE,\"Start cluster services on the primary node: $PRIMARY_NODE, for DB2 instance resource group: $RESOURCE_GROUP\"" >> $FILENAME
	for NODE in $TAKEOVER_NODES; do
		echo "NODE_DOWN_TAKEOVER,$NODE,\"Stop cluster services with takeover on the takeover node: $NODE for DB2 instance resource group: $RESOURCE_GROUP\""
		echo "NODE_UP,$NODE,\"Start cluster services for node: $NODE, for DB2 instance resource group: $RESOURCE_GROUP\""
	done >> $FILENAME
}

umask -S u=rw,g=,o=
KLIB=/usr/es/lib/ksh93
FPATH=$KLIB/hacmp:$KLIB/db2:$KLIB/aix/odm
PATH=$PATH:/usr/es/sbin/cluster/utilities:/usr/es/sbin/cluster:/usr/es/sbin/cluster/sa/db2/sbin/

SCRIPT_LOCATION=/usr/es/sbin/cluster/sa/db2/etc

if set -- $(getopt g:f: $* 2>&1); then

        while [ $# != 0 ]; do
                case "$1" in
			-g)
				# Store the resource group name
				RG=$2
				shift
			;;
			-f)
				OUTPUT_SCRIPT=$2
				shift
			;;
                        --)
                                shift
                                break
                        ;;
                esac
                shift
        done
fi

if [[ -z $RG ]]; then
	# Show the usage statement
	abort 1 $SCRIPT_NAME 
fi

if [[ -z $OUTPUT_SCRIPT ]]; then
	OUTPUT_SCRIPT="$SCRIPT_LOCATION/testscript_$RG"
fi

write_script $RG $OUTPUT_SCRIPT
instance=$(KLIB_AIX_ODM_get_odm_fields "group=$RG" "HACMPgroup" "sa_key")

echo "#instance:script"
echo "$instance:$OUTPUT_SCRIPT"
