#!/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_r720 src/43haes/usr/sbin/cluster/sa/tsmserver/sbin/cl_tsm_serv_config.sh 1.4.1.1 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2010 
# 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/tsmserver/sbin/cl_tsm_serv_config.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM

###############################################################################################
#
# NAME:
#	cl_tsm_serv_config
#
# PURPOSE:	
#       create PowerHA SystemMirror configuration for TSM server instance. This script configures TSM server 
#	instance Resource Group with Application server, Volume Groups, and service IP address
#
# ARGUMENTS:
#       -a Application name
#	-p Primary Node
#	-t Takeover Nodes(s)
#	-i instance name
#	-d instance Directory
#	-v VG_list
#	-s Service IP
#	-n Netmask/prefix Length
#	-m Modify TSM Smart server Instance 
#	-u TSM server user ID
# 
# RETURNS
#  0 on success
#  1 on Failure
#################################################################################################
typeset version="1.0"
[[ "$VERBOSE_LOGGING" == "high" ]] && set -x
. /usr/es/sbin/cluster/sa/tsmserver/sbin/cl_tsmserverGlobals
. /usr/es/sbin/cluster/sa/tsmserver/sbin/cl_tsmserverUtilities

KLIB_HACMPLOG_ENTRY=$KLIB_TSM_SERVER_HACMPLOG_ENTRY

typeset APPLICATION_ID     # Smart Assist Application Name of the TSM server instance. 
typeset PRIMNODE TAKEOVNODES # primary and takeover nodes.
typeset INSTANCE_ID	     # TSM server instance name.
typeset INSTANCE_DIR	     # TSM server instance Directory.
typeset SHARED_VGS	     # Shared VGs between the nodes in the cluster.
typeset SERVICE_IP	     # Service IP for the TSM instance.
typeset NETMASK		     # Netmask/ prefix Length of service IP address. 		
typeset MODIFY="N"	     # Modify the TSM server instance configuration.
typeset USER		     # TSM user id.
typeset PASSWORD	     # TSM Password.	
	
typeset SMARTASSIST_ID="TSM_SERV" # TSM server smart assist ID 
typeset COMPONENT_ID		      # component ID of TSM server smart assist	

#################################################################################
# Function: addServerInstance
#
# Purpose: To create PowerHA SystemMirror resources and Resource group for Managing 
#	   TSM server Instance.
#
# Arguments: None.
#
# Returns: 0 for Sucess
# 	   1 for Failure
################################################################################

function addServerInstance	{

	[[ "$VERBOSE_LOGGING" == "high" ]] && set -x

	typeset rgname="TSM_SERV_RG_"$INSTANCE_ID
	typeset appname="TSM_SERV_APP_"$INSTANCE_ID
	typeset appmonname="TSM_SERV_APP_MON_"$INSTANCE_ID
	
	COMPONENT_ID="TSM_SERVER"

	rgname=$(getUnusedName $rgname "resource_group")
	[[ "$?" == "1" ]] && {
	KLIB_SA_logmsg ERROR 1 21 tsmserversa.cat "Unable to get the unused name for %1\$s=%2\$s\n" "resource_group" $rgname	
	exit 1
	}

	appname=$(getUnusedName $appname "application")
	[[ "$?" == "1" ]] && {
        KLIB_SA_logmsg ERROR 1 21 tsmserversa.cat "Unable to get the unused name for %1\$s=%2\$s\n" "application" $appname
        exit 1
        }

	appmonname=$(getUnusedName $appmonname "application_monitor")
	[[ "$?" == "1" ]] && {
        KLIB_SA_logmsg ERROR 1 21 tsmserversa.cat "Unable to get the unused name for %1\$s=%2\$s\n" "application" $appmonname
        exit 1
        }

	isVGsAlreadyDefined "$SHARED_VGS"
	isServiceIPLabelAlreadyDefined $SERVICE_IP
	[[ "$?" == "0" ]] && {
        KLIB_SA_logmsg ERROR 1 25 tsmserversa.cat "service label %1\$s already defined in the PowerHA SystemMirror configuration\n" $SERVICE_IP
        exit 1
        }

	#Adding service IP label
	addServiceIPLabel 

	#creating Resource Group for TSM server instance
	dspmsg -s 1 tsmserversa.cat 34 "Adding TSM server instance Resource Group %1\$s to PowerHA SystemMirror configuration\n" $rgname		
	KLIB_SA_logmsg INFO 1 34 tsmserversa.cat "Adding TSM server instance Resource Group %1\$s to PowerHA SystemMirror configuration\n" $rgname
	claddgrp -g "$rgname" -n "$PRIMNODE $TAKEOVNODES" \
		-S "OHN" -O "FNPN" -B "FBHPN" \
		-K $APPLICATION_ID
		
		(( $? != 0 )) && {
					KLIB_SA_logmsg ERROR 1 35 tsmserversa.cat "Failed while adding TSM server instance Resource Group %1\$s\n" $rgname
					exit 1
				  }	

	#creating Application server

	startscript="$TSM_SERVER_HA_SBIN/cl_tsmserverstart -i $INSTANCE_ID -d $INSTANCE_DIR"
	stopscript="$TSM_SERVER_HA_SBIN/cl_tsmserverstop -i $INSTANCE_ID"
	monitorscript="$TSM_SERVER_HA_SBIN/cl_tsmservermonitor -i $INSTANCE_ID"
	
	KLIB_SA_logmsg INFO 1 36 tsmserversa.cat "Adding TSM server instance Application server %1\$s to PowerHA SystemMirror configuration\n" $appname

	clvt add application $appname \
		STARTSCRIPT="$startscript" \
		STOPSCRIPT="$stopscript" >/dev/null 2>&1 || {
 	                                       KLIB_SA_logmsg ERROR 1 37 tsmserversa.cat "Failed while adding TSM server instance Application server %1\$s\n" $appname
        	                               exit 1
                	                    }
	#creating custom application monitor
	
	KLIB_SA_logmsg INFO 1 38 tsmserversa.cat "Adding TSM server instance Application monitor %1\$s to PowerHA SystemMirror configuration\n" $appmonname
	claddappmon name=$appmonname \
        MONITOR_TYPE='user' \
       	RESOURCE_TO_MONITOR=$appname \
        INVOCATION='longrunning' \
        FAILURE_ACTION='fallover' \
        RESTART_METHOD="$startscript" \
        CLEANUP_METHOD="$stopscript" \
        MONITOR_METHOD="$monitorscript" \
        MONITOR_INTERVAL='180' \
        HUNG_MONITOR_SIGNAL='9' \
        STABILIZATION_INTERVAL='180' \
        RESTART_COUNT='3' \
        RESTART_INTERVAL='792' >/dev/null 2>&1 || {
	                                           KLIB_SA_logmsg ERROR 1 39 tsmserversa.cat "Failed while adding TSM server instance Application monitor %1\$s\n" $appmonname
        	                                   exit 1
                                               }

	# Add service IP, Application and VG to Resource Group
	KLIB_SA_logmsg INFO 1 56 tsmserversa.cat "Modifying TSM server Resource Group %1\$s\n" $rgname
	clvt modify resource_group "$rgname" \
        SERVICE_LABEL="$SERVICE_IP" \
        APPLICATIONS="$appname" \
	FILESYSTEM="ALL" \
	VG_AUTO_IMPORT='false' \
        VOLUME_GROUP="$SHARED_VGS" >/dev/null 2>&1 || {
                                          KLIB_SA_logmsg ERROR 1 40 tsmserversa.cat "Failed while modifying TSM server instance Resource Group %1\$s\n" $rgname
                                          exit 1
                                  }
	
	# Add the entry to HACMPsa_metadata ODM

	KLIB_SA_logmsg INFO 1 41 tsmserversa.cat "Adding TSM server instance configuration to ODM\n" 
	claddsaapp -a $APPLICATION_ID \
        RESOURCE_GROUP=$rgname \
        INSTANCE_NAME=$INSTANCE_ID \
        INSTANCE_DIRECTORY=$INSTANCE_DIR \
        SMARTASSIST_ID="$SMARTASSIST_ID" \
        COMPONENT_ID="$COMPONENT_ID" \
        APPLICATION_NAME="$APPLICATION_ID" \
        INSTANCE_USER="$USER" \
        INSTANCE_PASSWORD="$PASSWORD" >/dev/null 2>&1 || {
                           	               KLIB_SA_logmsg ERROR 1 42 tsmserversa.cat "Failed while Adding TSM server instance configuration to ODM\n"
                                	       exit 1
	                                }


	# generate verify scripts for verification
	dspmsg -s 1 tsmserversa.cat 43 "Generating verify script for TSM server instance %1\$s\n" $INSTANCE_ID
	nodes=$(clvt query node)
	KLIB_SA_logmsg INFO 1 43 tsmserversa.cat "Generating verify script for TSM server instance %1\$s\n" $INSTANCE_ID
	generateVerifyScript $COMPONENT_ID $APPLICATION_ID "$nodes" 
	[[ "$?" == 1 ]] && {
				KLIB_SA_logmsg ERROR 1 47 tsmserversa.cat "Failed while Generating verify script for TSM server instance %1\$s\n" $INSTANCE_ID
				exit 1
			   }	
	return 0
}

#################################################################################
# Function: modifyServerInstance
#
# Purpose: To modify PowerHA SystemMirror resources and Resource group for Managing
#          TSM server Instance.
#
# Arguments: None.
#
# Returns: 0 for Sucess
#          1 for Failure
#
################################################################################

function modifyServerInstance      {

	[[ "$VERBOSE_LOGGING" == "high" ]] && set -x
	removeHAresources
	addServerInstance
	return $?
	
}

#################################################################################
# Function: removeHAresources
#
# Purpose: To remove PowerHA SystemMirror resources and Resource group for Managing
#          TSM server Instance.
#
# Arguments: None.
#
# Returns: 0 for Sucess
#          1 for Failure
#
################################################################################

function removeHAresources      {

        [[ "$VERBOSE_LOGGING" == "high" ]] && set -x
	
	KLIB_SA_logmsg INFO 1 48 tsmserversa.cat "Removing TSM server instance %1\$s from PowerHA SystemMirror configuration\n" $INSTANCE_ID
	dspmsg -s 1 tsmserversa.cat 48 "Removing TSM server instance %1\$s from PowerHA SystemMirror configuration\n" $INSTANCE_ID
	rg=$(clodmget -q "name=RESOURCE_GROUP and application_id=$APPLICATION_ID" -f value -n HACMPsa_metadata)
	[[ $? != 0 ]] && {
				KLIB_SA_logmsg ERROR 1 49 tsmserversa.cat "Resource Group %1\$s not found in ODM\n" $rg
				exit 1
			 }	
	app_serv=$(clvt query resource_group $rg|grep APPLICATIONS|awk -F= '{ print $2 }' | sed "s/\"//g" )
	
	for ap_srv in $app_serv
	do
		app_mons=$(clvt query application $ap_srv|grep ASSOCIATEDMONITORS |awk -F= '{ print $2 }' | sed "s/\"//g" )
	
		#removing the application monitors
		for mon in $app_mons
		do
			clvt delete application_monitor $mon > /dev/null 2>&1
			[[ $? != 0 ]] && {
				KLIB_SA_logmsg ERROR 1 50 tsmserversa.cat "Failed while deleting Application monitor %1\$s for changing TSM server instance configuration\n" $app_mons
                                exit 1
			}
		done
	done

	for app in $app_serv;do
		clvt delete application $app > /dev/null 2>&1
		[[ $? != 0 ]] && {
					KLIB_SA_logmsg ERROR 1 51 tsmserversa.cat "Failed while deleting Application servers for changing TSM server instance configuration%1\$s\n" $app
                        	        exit 1
            		 	}
	done
	
	clvt delete resource_group $rg >/dev/null 2>&1
        (( $? != 0 )) && {
				KLIB_SA_logmsg ERROR 1 52 tsmserversa.cat "Failed while deleting Resource Group %1\$s for changing TSM server instance configuration\n" $rg
                                exit 1
                         }

	# Remove the metadata entries for this instance
    	clrmsaapp -a $APPLICATION_ID >/dev/null 2>&1
    	(( $? != 0 )) && {
				KLIB_SA_logmsg ERROR 1 53 tsmserversa.cat "Failed while deleting ODM for changing TSM server instance configuration\n"
                                exit 1
    			 }

	return 0
}

#----------------------------------------------------------------------------
# Function:
#   getVerifyHeader
#
# Purpose:
#   Generate the verification parameterized script header for TSM server.
#   This is called at verification time.
#
# Arguments:
#   (1) Component ID
#   (2) Application Name
#
# Output:
#   Script output to stdout
#
# Returns:
#   n/a
#----------------------------------------------------------------------------

function getVerifyHeader {
    [[ "$VERBOSE_LOGGING" == "high" ]] && set -x
    c_id=$1
    sa_id=$2
    KLIB_SA_logmsg INFO 1 44 tsmserversa.cat "Generating verify Header for TSM server instance Application name %1\$s\n" $sa_id
    echo "#\n# PowerHA SystemMirror / TSM Server"
    echo "# Application Verificiation script generated for: $sa_id"
    echo "# Date: $(date)\n#\n"

    print -n "Component.Name.DefaultName = \""
    print -n $c_id
    echo "\""
    echo "Component.Nodes = \"S=TSM_SERV:A=$sa_id\""

    return 0
}

#----------------------------------------------------------------------------
# Function:
#   generateTSMVerifyScript
#
# Purpose:
#   Generates the parameterized verification script for
#   TSM server smart instance.
#
# Arguments:
#  n/a 
#
# Returns:
#   0 on success
#   1 on failure
#----------------------------------------------------------------------------

function generateTSMVerifyScript {
	[[ "$VERBOSE_LOGGING" == "high" ]] && set -x

    # Generates a parameterised verification script to 
    # Obtain TSM server instance user and UID, validate that the user exists
    # on all nodes in the cluster with the same UID
    KLIB_SA_logmsg INFO 1 45 tsmserversa.cat "Generating verify Script for TSM server instance %1\$s\n" $INSTANCE_ID
    echo "\n#\n# Perform validation for user: $INSTANCE_ID\n#"

    echo "HAVerify.User.severity = \"ERROR\""
    echo "HAVerify.User.exists[0].name=\"$INSTANCE_ID\""

    uid=$(/usr/sbin/lsuser -f $INSTANCE_ID | grep id| awk ' FS="=" {print $2}')

    echo "HAVerify.User.exists[0].UID=$uid"

    # Generates a fileset parameterised verification script to
    # Obtain TSM server is installed or not.

    echo "\n#\n# Perform file set validation for TSM server\n#"

    echo "HAVerify.Fileset.severity = \"ERROR\""
    echo "HAVerify.Fileset.exists[0].name =\"tivoli.tsm.server\""
    
    return 0
}

#----------------------------------------------------------------------------
# Function:
#   generateVerifyScript
#
# Purpose:
#   Generates the cluster verification parameterized script for
#   TSM server smart instance, then pushes the file across to
#   other nodes in the cluster
#
# Arguments:
#   (1) Component Identifier
#   (2) Application Name
#   (3) nodes
#
# Returns:
#   0 on success
#   1 on failure
#----------------------------------------------------------------------------

function generateVerifyScript {
    [[ "$VERBOSE_LOGGING" == "high" ]] && set -x
    typeset cid=$1
    typeset sa_id=$2
    typeset nodes=$3
    typeset localnode=$(get_local_nodename)

    [[ -z $sa_id || -z $cid ]] && return 1
    [[ -z $nodes ]] && return 1

    typeset TEST_ROOT=/usr/es/sbin/cluster/etc/config/verify
    typeset VERIFICATION_FILENAME=${sa_id}"_"${cid}".ver"

    {
        getVerifyHeader $cid $sa_id
	generateTSMVerifyScript
	
    } > $TEST_ROOT/$VERIFICATION_FILENAME

    for node in $nodes; do
        [[ "$node" != "$localnode" ]] && {
            cl_rcp $TEST_ROOT/$VERIFICATION_FILENAME \
                $node:$TEST_ROOT/$VERIFICATION_FILENAME
	    [[ $? != 0 ]] && {
				KLIB_SA_logmsg ERROR 1 46 tsmserversa.cat "Pushing the verification scripts to other node %1\$s is failed\n" $node
				return 1
			     }		
        }
    done


    clvt delete file_collection $cid >/dev/null 2>&1
    clvt add file_collection $cid \
        FILES="$TEST_ROOT/$VERIFICATION_FILENAME" \
        ISPROPOGATEDFILEDURINGSYNC="true" \
        ISPROPOGATEAUTOWHENDETECTED="true" >/dev/null 2>&1
    claddsaapp -a $APPLICATION_ID FILE_COLLECTION=$cid
    return 0
}

#----------------------------------------------------------------------------
# Function:
#   isVGsAlreadyDefined
#
# Purpose:
#   Determine if a list of volume groups are already defined.  If a VG
#   is already define to PowerHA SystemMirror, then report an error and abort
#
# Arguments:
#   (1) by ref List of PowerHA SystemMirror volume groups
#
# Return:
#   0 on success
#   no return if failure (exit)
#----------------------------------------------------------------------------

function isVGsAlreadyDefined {
    [[ "$VERBOSE_LOGGING" == "high" ]] && set -x
    typeset vgs=$1
    typeset vg
    KLIB_SA_logmsg INFO 1 22 tsmserversa.cat "This function checks to see if the VG's are already defined to the PowerHA SystemMirror configuration\n"	
    for vg in $vgs; do
	KLIB_SA_logmsg DEBUG 1 23 tsmserversa.cat "Checking the Volume Group=%1\$s in  PowerHA SystemMirror configuration\n" $vg
        isVolumeGroupAlreadyDefined $vg
        (( $? != 1 )) && {
	    KLIB_SA_logmsg ERROR 1 24 tsmserversa.cat "Volume Group=%1\$s is already defined in PowerHA SystemMirror configuration\n" $vg
            exit 1
        }
    done
    return 0
}

#----------------------------------------------------------------------------
# Function:
#   addServiceIPLabel
#
# Purpose:
#   Adds Service IP label to the PowerHA SystemMirror configuration if
#   it doesn't already exist.
#
# Arguments:
#   n/a
#
# Returns:
#   0 on success
#   1 on failure
#----------------------------------------------------------------------------

function addServiceIPLabel {
    [[ "$VERBOSE_LOGGING" == "high" ]] && set -x
    typeset results

    #
    # Can the service IP label be resolved?
    #
    /usr/bin/host $SERVICE_IP >/dev/null 2>&1
    (( $? != 0 )) && {
        # The service IP label cannot be resolved
	KLIB_SA_logmsg ERROR 1 30 Tsmserversa.cat "Service IP label %1\$s can not be resolved\n" $SERVICE_IP
        exit 1
    }

    result=$(clvt query interface | grep $SERVICE_IP)
    [[ -n $result ]] &&
        clvt delete interface $SERVICE_IP

    clvt query service_ip $SERVICE_IP > /dev/null 2>&1
    [[ $? != "0" ]] && { # Create the service IP label
	KLIB_SA_logmsg INFO 1 31 tsmserversa.cat "creating Service IP label %1\$s\n" $SERVICE_IP

        nodes=$(echo "$PRIMNODE $TAKEOVNODES")

	addr=$(/usr/bin/host $SERVICE_IP|awk '{print $3}'| sed s/,//g)
	isipv6addr $addr
        if [[ "$?" == "0" ]]
	then
		Net_type="2" # Net_type will be specified in HACMPadapter ODM for IPv6=2, IPv4=1
	else
		Net_type="1"
	fi
        network=$(getServiceNetwork $Net_type "$nodes")

        # There are no suitable networks defined to create
        # the service IP label on, abort
        [[ -z $network ]] && {
	    KLIB_SA_logmsg ERROR 1 32 Tsmserversa.cat "No suitable PowerHA SystemMirror network is defined to create service IP label\n" 
            exit 1
        }

	/usr/es/sbin/cluster/utilities/claddnode -T service -B "$SERVICE_IP" -P "$NETMASK" -w "$network" > /dev/null 2>&1
        (( $? != 0 )) && {
	    KLIB_SA_logmsg ERROR 1 33 Tsmserversa.cat "Error while adding service IP label %1\$s to netowk %2\$s\n" $SERVICE_IP $network
	    exit 1
        }
    } # otherwise the service IP label is already defined

    return 0
}

###########################################
# main 
###########################################
export PATH="$(/usr/es/sbin/cluster/utilities/cl_get_path all)"
while getopts a:p:t:i:d:v:s:n:u:m opt
do
	case $opt in
	m) # modifying TSM server
		MODIFY="Y"
		;;
	a) # Application Name 
		APPLICATION_ID=$OPTARG
		APPLICATION_ID=$(echo $APPLICATION_ID|sed s/*$//) #Remove the trailing whitespaces if any at the end of the string
		KLIB_SA_logmsg INFO 1 10 tsmserversa.cat "Validating TSM server Smart Assist application id\n"
		validateAppName "$APPLICATION_ID"
		[[ "$?" == "1" ]] && {
		KLIB_SA_logmsg ERROR 1 11 tsmserversa.cat "TSM server smart assist Application id=%1\$s is not in the range [a-z][A-z][0-9]_\n" "$APPLICATION_ID"
		exit 1
		}
		
		KLIB_SA_logmsg DEBUG 1 12 tsmserversa.cat "completion of validating TSM server Smart Assist for applicaton id=%1\$s\n" "$APPLICATION_ID" 
		;;
	p) # Primary node 
		PRIMNODE=$OPTARG
		;;
	t) # Takeover nodes
		TAKEOVNODES=$OPTARG
		;;
	i) # TSM server instance Name
		INSTANCE_ID=$OPTARG
		;;
	d) # TSM server instance directory
		INSTANCE_DIR=$OPTARG
		;;
	v) # Shared VGs between the nodes
		SHARED_VGS=$OPTARG
		;;
	s) # Service IP of the TSM server
		SERVICE_IP=$OPTARG
		;;
	n) # Netmask/prefix length of the service IP
		NETMASK=$OPTARG
		;;
	u) # TSM server instance user ID
		USER=$OPTARG
		;;
	esac
done	

#
#Validate the node names, if there are any Name duplication for Primary and Takeover nodes
#
KLIB_SA_logmsg INFO 1 13 tsmserversa.cat "Validating TSM server Nodes \n"
validateNodes $PRIMNODE "$TAKEOVNODES"
(( $? != 0 )) && {
KLIB_SA_logmsg ERROR 1 14 tsmserversa.cat "TSM server smart assist has primary=%1\$s and takeover nodes=%2\$s as repetitive\n" $PRIMNODE "$TAKEOVNODES"
exit 1
}
# TSM server password encryption for the specific user
password=$(clmaskpasswd)
checkRC=$?
if (( $checkRC != RC_SUCCESS )); then
    KLIB_SA_logmsg INFO 1 67 tsmserversa.cat "Failed to set the TSM server password for user %1\$s\n" $USER
    dspmsg -s 1 tsmserversa.cat 67 "Failed to set the TSM server password for user %1\$s\n" $USER
    rc=$RC_ERROR
elif [[ -z $password || $password == *([[:space:]]) ]]; then
    KLIB_SA_logmsg INFO 1 68 tsmserversa.cat "Invalid password, failed to set the TSM server password for user %1\$s\n" $USER
    dspmsg -s 1 tsmserversa.cat 68 "Invalid password, failed to set the TSM server password for user %1\$s\n" $USER
    rc=$RC_INCORRECT_INPUT
else
    PASSWORD=$(print $password | clencodearg)
fi
KLIB_SA_logmsg DEBUG 1 15 tsmserversa.cat "completion of validating TSM server Smart Assist nodes primary=%1\$s amd takeover nodes=%2\$s\n" $PRIMNODE "$TAKEOVNODES"
if [[ "$MODIFY" == "Y" ]]
then
	KLIB_SA_logmsg INFO 1 16 tsmserversa.cat "Modify the TSM server instace=%1\$s \n" $INSTANCE_ID
	dspmsg -s 1 tsmserversa.cat 16 " Modifying TSM server instance=%1\$s \n" $INSTANCE_ID
        modifyServerInstance
        exit $?
else

	#check If the TSM server instance is already configured to PowerHA SystemMirror.

	KLIB_SA_logmsg INFO 1 17 tsmserversa.cat "Checking if the TSM server instance is already configured.\n"
	app_id=$(clodmget -q "name=INSTANCE_NAME and value=$INSTANCE_ID" -f application_id -n HACMPsa_metadata)

	[[ -n $app_id ]] &&  {
			KLIB_SA_logmsg ERROR 1 18 tsmserversa.cat "TSM server instance=%1\$s is already configured\n" $INSTANCE_ID
			exit 1 
	     }	
	KLIB_SA_logmsg DEBUG 1 19 tsmserversa.cat "TSM server instance=%1\$s is not already configured\n" $INSTANCE_ID

	KLIB_SA_logmsg INFO 1 20 tsmserversa.cat "Adding TSM server instance=%1\$s \n" $INSTANCE_ID
	dspmsg -s 1 tsmserversa.cat 20 "Adding TSM server instance=%1\$s \n" $INSTANCE_ID
	addServerInstance
	exit $?
fi
