#!/usr/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG
#  This is an automatically generated prolog.                                  
#                                                                              
#  Copyright (C) Altran ACT S.A.S. 2019,2021.  All rights reserved.  
#                                                                              
#  ALTRAN_PROLOG_END_TAG
#                                                                              
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/sa/db2/sbin/cl_db2mt_import.sh 1.9 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2005,2011 
# 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 
#  @(#)  7d4c34b 43haes/usr/sbin/cluster/sa/db2/sbin/cl_db2mt_import.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
##
## NAME:	cl_db2mt_import
##
## DESCRIPTION:	Runs cl_db2import twice for mutual takeover.
##		Accepts the same set of arguments as cl_db2import, but
##		accepts a second set of arguments for the second instance
##
## ARGUMENTS:
##	 -A Application Name
##
##	 -a Add/change all necessary HACMP resources for the specified DB2
##	    instance.  If the resources already exist, they will be removed
##		and then added.  This is the change/modify mode.
##
##	 -r Remove all HACMP resources for the specified DB2 instance.
##		
##	 -N DB2 UDB first node.
##
##	 -n DB2 UDB second node(s). 
##
##	 -I and -i DB2 UDB instance name to be made highly available or removed.
##	    This is the only option required when removing (-r) the resources.
##
##	 -D and	-d DB2 UDB database name.  This database is used by the Custom
##		SQL Monitor.
##
##	 -I and	-l The TCP/IP service IP label used by DB2 to communicate with
##		other application tiers and/or clients.
##
##	 -M Delete the pre-existing configuration (change/show mode)
##
## RETURN:
##	0       Success (import completed without error)
##	1       Warning (import completed with warning(s))
##	2       Failure (import failed due to pre-verification failure)
##	3       Failure (import failed due to failure in adding resource)
##
##---------------------------------------------------------------------------

. /usr/es/lib/ksh93/func_include
version='1.8'

#----------------------------------------------------------------------------
# Global Definitions:
#----------------------------------------------------------------------------

# Set the FPATH for all DB2 / HACMP functions
FLIB=/usr/es/lib/ksh93
FPATH=$FLIB/db2:$FLIB/hacmp:$FLIB/util:$FLIB/aix:$FLIB/util/list
PATH=$PATH:/usr/es/sbin/cluster/sa/db2/sbin/:/usr/es/sbin/cluster/utilities/:/usr/es/sbin/cluster/:/usr/es/sbin/cluster/sa/sbin/

DB2SA_BIN_DIR=/usr/es/sbin/cluster/sa/db2/sbin
DB2_ETC_PATH=/usr/es/sbin/cluster/sa/db2/etc
DISCOVERY_FILE=$DB2_ETC_PATH/db2.disc
FLUSH_DISCOVERY_FILE=false

# Signal that this is a mutual takeover configuration
FLAGS="-C DB2_NON_DPF_MUTUAL"
#----------------------------------------------------------------------------
# Functions:
#----------------------------------------------------------------------------

function setMetaDataPrefix {
	typeset prefix=$1
	typeset -A list

	# Change all of the HACMPsa_metadata entries to contain the prefix "FIRST"
	KLIB_UTIL_parse_arguments list $(clquerysaapp -a $APPLICATION_NAME) >/dev/null
	for name in ${!list[*]}; do
		[[ 	"$name" != "COMPONENT_ID" && "$name" != "RESOURCE_GROUP" &&
			"$name"	!= "SMARTASSIST_ID" && "$name" != "APPLICATION_NAME" &&
			"${name:0:6}" != "FIRST_" && "${name:0:7}" != "SECOND_" ]] && {
			clrmsaapp -a $APPLICATION_NAME "$name"
			clquerysaapp -a $APPLICATION_NAME $name
			claddsaapp -a $APPLICATION_NAME $prefix"_"$name="${list[$name]}"
		}
	done
}
	

#----------------------------------------------------------------------------
# Main:
#----------------------------------------------------------------------------

# Read in the init functions, abort, require, errmsg, etc.
. /usr/es/lib/ksh93/common_functions.ksh93

# Read in the preimport message catalog, which is where we'll
# store our messages
. /usr/es/sbin/cluster/sa/db2/cat/cl_db2preimport

umask -S u=rw,g=,o=

if set -- $(getopt A:arMo:n:i:d:l:N:I:D:L: $* 2>&1); then

	while [ $# != 0 ]; do
    	case "$1" in
		-A)
			APPLICATION_NAME=$2
			FLAGS="$FLAGS -A $APPLICATION_NAME"
			shift
			;;
		-a|-r)
			FLAGS="$FLAGS $1"
			;;
		-N)
			# Primary node for instance A
			node=$2
			A_FLAGS="$A_FLAGS -o $node"
			B_FLAGS="$B_FLAGS -n $node"
			shift
			;;
		-n)
			# Primary node for instance B
			node=$2
			A_FLAGS="$A_FLAGS -n $node"
			B_FLAGS="$B_FLAGS -o $node"
			shift
			;;
		-I)
			INSTANCE_A=$2
			A_FLAGS="$A_FLAGS -i $2"
			shift
			;;
		-i)
			B_FLAGS="$B_FLAGS $1 $2"
			INSTANCE_B=$2
			shift
			;;
		-l)
			B_FLAGS="$B_FLAGS $1 $2"
			SERVICE_B=$2
			shift
			;;
		-d)
			DB_TO_MONITOR_B=$2
			B_FLAGS="$B_FLAGS $1 $2"
			shift
			;;
		-D)
			DB_TO_MONITOR_A=$2
			A_FLAGS="$A_FLAGS -d $2"
			shift
			;;
		-L)
			A_FLAGS="$A_FLAGS -l $2"
			SERVICE_A=$2
			shift
			;;
		-M)
			FLAGS="$FLAGS -M"
			;;
        	--)
			shift
            break
            ;;
		esac
    	shift
	done
fi

clsapre

# Has the user improperly choosen the same instance twice?
if [[ "$INSTANCE_A" == "$INSTANCE_B" ]]; then
	abort 25 $INSTANCE_A
fi

# Has the user entered the same service IP label twice?
if [[ "$SERVICE_A" == "$SERVICE_B" ]]; then
	abort 24 $SERVICE_A $INSTANCE_A $INSTANCE_B
fi

#
# -F is used for signalling First Resource Group
#
A_FLAGS="$A_FLAGS -F"
#
# Construct the first instance
#
cl_db2preimport $FLAGS $A_FLAGS 
(( $? != 0 )) && exit $?
setMetaDataPrefix "FIRST"

claddsaapp -a $APPLICATION_NAME FIRST_RESOURCE_GROUP=${INSTANCE_A}"_ResourceGroup"

#
# -S is used for signalling Second Resource Group
#
B_FLAGS="$B_FLAGS -S"
#
# Construct the second instance
#
cl_db2preimport $FLAGS $B_FLAGS
(( $? != 0 )) && exit $?
setMetaDataPrefix "SECOND"
claddsaapp -a $APPLICATION_NAME SECOND_RESOURCE_GROUP=${INSTANCE_B}"_ResourceGroup"

clsapost -v
exit 0
