#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG                                                    
#  This is an automatically generated prolog.                                  
#                                                                              
#  Copyright (C) Altran ACT S.A.S. 2017,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/domino/sbin/cl_lotus_domino_config.sh 1.2 
#  
# 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/domino/sbin/cl_lotus_domino_config.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
######################################################################################################################
#
# NAME:
#       cl_lotus_domino_config
#
# PURPOSE:
#       create PowerHA SystemMirror configuration for Lotus domino server. This script configures Lotus domino server
#       Resource Group with Application server and Domino IP address as service IP address.
#
# ARGUMENTS:
#       -a Application name
#       -n Participating Nodes for Lotus domino server
#       -p Domino servers node and data directory
#       -d Domino cluster between the domino servers
#
# RETURNS
#  0 on success
#  1 on Failure
######################################################################################################################
typeset version="1.0"
[[ "$VERBOSE_LOGGING" == "high" ]] && set -x
. /usr/es/sbin/cluster/sa/domino/sbin/cl_lotusdominoGlobals
. /usr/es/sbin/cluster/sa/domino/sbin/cl_lotusdominoUtilities

KLIB_HACMPLOG_ENTRY=$KLIB_LOTUS_DOMINO_HACMPLOG_ENTRY

typeset APPLICATION_ID       # Smart Assist Application Name of the Lotus domino server.
typeset NODES 		     # Participating nodes in the cluster.
typeset DATA_DIR             # Domino server node and data directory
typeset DOMINO_CLSTR	     # Domino cluster between the domino servers
typeset MODIFY="N"           # Modify the domino server configuration.

typeset SMARTASSIST_ID="DOMINO" # Domino smart assist ID
typeset COMPONENT_ID="DOMINO_SERVER" # component ID of smart assist for Lotus domino server
typeset SERVER_USERS		# all domino users
typeset SERVER_GROUPS		# all domino groups
typeset NODES_RG		# Nodes for a particular RG
typeset DOMINO_PROG_DIRECTORY   # Domino program directory

#----------------------------------------------------------------------------
# Function:
#   clean_Up
#
# Purpose:
#   To remove the configured Serive IP, RG, application and monitor
#   in case of Error.
#
# Arguments:
#    (1) rgname
#    (2) serviceip 
#    (3) appname
#    (4) appmonname
#
# Returns:
#   N/A
#----------------------------------------------------------------------------

function clean_Up {
    typeset rgname=$1
    typeset serviceip=$2
    typeset appname=$3
    typeset appmonname=$4
    KLIB_SA_logmsg INFO 1 66 lotusdominosa.cat "Rolling back the already created configuration.\n"
    if [[ -n $rgname ]]
    then
        clmgr delete resource_group $rgname
    fi
    if [[ -n $serviceip ]]
    then
        clmgr delete service_ip $serviceip
    fi
    if [[ -n $appmonname ]]
    then
        clmgr delete application_monitor $appmonname
    fi
    if [[ -n $appname ]]
    then
        clmgr delete application $appname
    fi
}


#################################################################################
# Function: addserverInstance
#
# Purpose: To create PowerHA SystemMirror resources and Resource group for Managing
#          Lotus domino server.
#
# Arguments: None.
#
# Returns: 0 for Sucess
#          1 for Failure
################################################################################

function addserverInstance      {

        [[ "$VERBOSE_LOGGING" == "high" ]] && set -x
	local_node=$(get_local_nodename)
	i=1
	echo "$DATA_DIR"|grep ";" > /dev/null 2>&1
	[[ "$?" != "0" ]] && {
		DATA_DIR=$(echo $DATA_DIR";") 
	}
        if [[ -n $DOMINO_PROG_DIR ]];then
                DOMINO_PROG_DIRECTORY=$DOMINO_PROG_DIR
        else
                KLIB_SA_logmsg ERROR 1 51 lotusdominosa.cat "Lotus domino pogram directory DOMINO_PROG_DIR is not exported\n"
                dspmsg -s 1 lotusdominosa.cat 51 "Lotus domino pogram directory DOMINO_PROG_DIR is not exported\n"
                exit 1
        fi

	NODE_DATA=$(echo "$DATA_DIR"|/usr/bin/cut -d ';' -f $i)
	while [[ -n $NODE_DATA ]]
	do
		SERVER_NODE=$(echo $NODE_DATA|/usr/bin/cut -d ',' -f 1)
		SERVER_DATA_DIR=$(echo $NODE_DATA|/usr/bin/cut -d ',' -f 2)
		if [[ $SERVER_NODE == $local_node ]];then	
			SERVER_USER=$(ls -l $SERVER_DATA_DIR/notes.ini|awk '{print $3}')
			[[ "$?" == "1" ]] && {
               	                KLIB_SA_logmsg ERROR 1 18 lotusdominosa.cat "unable to read the domino server directory %1\$s on the node %2\$s\n" $SERVER_DATA_DIR $SERVER_NODE
                       	        exit 1
        		 }
			if [[ $DOMINO_CLSTR == "n" ]];then
				SERVER_GROUP=$(ls -l $SERVER_DATA_DIR/notes.ini|awk '{print $4}')
			fi

			SERVER_IP=$(grep TCPIP_TCPIPAddress $SERVER_DATA_DIR/notes.ini|awk -F= '{print $2}'|awk -F, '{print $2}')
			[[ "$?" == "1" ]] && {
                       	        KLIB_SA_logmsg ERROR 1 19 lotusdominosa.cat "unable to find the domino server directorys notes.ini file %1\$s on the node %2\$s\n" $SERVER_DATA_DIR $SERVER_NODE
                               	exit 1
                       	}
		else
			SERVER_USER=$(cl_rsh $SERVER_NODE "ls -l $SERVER_DATA_DIR/notes.ini"|awk '{print $3}')
			[[ "$?" == "1" ]] && {
                                KLIB_SA_logmsg ERROR 1 18 lotusdominosa.cat "unable to read the domino server directory %1\$s on the node %2\$s\n" $SERVER_DATA_DIR $SERVER_NODE
                                exit 1
                        }
			if [[ $DOMINO_CLSTR == "n" ]];then
				SERVER_GROUP=$(cl_rsh $SERVER_NODE "ls -l $SERVER_DATA_DIR/notes.ini"|awk '{print $4}')
			fi

                        SERVER_IP=$(cl_rsh $SERVER_NODE "grep TCPIP_TCPIPAddress $SERVER_DATA_DIR/notes.ini"|awk -F= '{print $2}'|awk -F, '{print $2}')
                        [[ "$?" == "1" ]] && {
                        KLIB_SA_logmsg ERROR 1 19 lotusdominosa.cat "unable to find the domino server directorys notes.ini file %1\$s on the node %2\$s\n" $SERVER_DATA_DIR $SERVER_NODE
                        exit 1
                        }
		fi
		if [[ $DOMINO_CLSTR == "n" ]];then
			if [[ -z $SERVER_USERS ]];then
				SERVER_USERS=$SERVER_USER
				SERVER_GROUPS=$SERVER_GROUP
			else
				SERVER_GROUPS=$(echo "$SERVER_GROUPS $SERVER_GROUP")
				SERVER_USERS=$(echo "$SERVER_USERS $SERVER_USER")
			fi
		fi

		typeset rgname="Domino_RG_$SERVER_NODE_$SERVER_USER"
	        typeset appname="Domino_APP_$SERVER_NODE_$SERVER_USER"
        	typeset appmonname="Domino_APP_MON_$SERVER_NODE_$SERVER_USER"
			
		KLIB_SA_logmsg INFO 1 20 lotusdominosa.cat "Adding Lotus domino server configuration %1\$s\n" $APPLICATION_ID

		rgname=$(getUnusedName $rgname "resource_group")
	       	[[ "$?" == "1" ]] && {
        		KLIB_SA_logmsg ERROR 1 21 lotusdominosa.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 lotusdominosa.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 lotusdominosa.cat "Unable to get the unused name for %1\$s=%2\$s\n" "application_monitor" $appmonname
        		exit 1
        	}
		SERVER_IP=$(/usr/bin/host $SERVER_IP|awk '{print $1}'|awk -F"." '{print $1}')
		[[ "$?" == "1" ]] && {
        		KLIB_SA_logmsg ERROR 1 22 Tsmadminsa.cat "Service IP label %1\$s can not be resolved\n" $SERVER_IP
			exit 1
		}
        	isServiceIPLabelAlreadyDefined $SERVER_IP
        	[[ "$?" == "0" ]] && {
        		KLIB_SA_logmsg ERROR 1 23 lotusdominosa.cat "service label %1\$s already defined in the PowerHA SystemMirror configuration\n" $SERVER_IP
        		exit 1
        	}
			
		#creating Resource Group for Lotus domino server configuration
        	KLIB_SA_logmsg INFO 1 24 lotusdominosa.cat "Adding Lotus domino server Resource Group %1\$s to PowerHA SystemMirror configuration\n" $rgname
		if [[ $DOMINO_CLSTR == "y" ]];then
                        claddgrp -g "$rgname" -n "$SERVER_NODE" \
                                -S "OHN" -O "FNPN" -B "FBHPN" \
                                -K $APPLICATION_ID

                        (( $? != 0 )) && {
                                        KLIB_SA_logmsg ERROR 1 25 lotusdominosa.cat "Failed while adding Lotus domino server Resource Group %1\$s\n" $rgname
                                        exit 1
                                    }
			NODES_RG=$(echo "$SERVER_NODE")
		else
			PRIMNODES=$SERVER_NODE
                        TAKENODES=""
                        for g in $NODES
                        do
                                [[ $g == $SERVER_NODE ]] || {
                                        if [[ -z $TAKENODES ]]; then
                                                TAKENODES=$g
                                        else
                                                TAKENODES=$(echo "$TAKENODES $g")
                                        fi
                                }
                        done
                        claddgrp -g "$rgname" -n "$SERVER_NODE $TAKENODES" \
                                -S "OHN" -O "FNPN" -B "FBHPN" \
                                -K $APPLICATION_ID

			(( $? != 0 )) && {
                        	 KLIB_SA_logmsg ERROR 1 25 lotusdominosa.cat "Failed while adding Lotus domino server Resource Group %1\$s\n" $rgname
                                 exit 1
                                }
			NODES_RG=$(echo "$NODES")
		fi

                #Adding service IP label
                addServiceIPLabel
               (( $? != 0 )) && {
                   clean_Up $rgname 
                   exit 1
           }

		#creating Lotus domino server Application
 	        startscript="$LOTUS_DOMINO_HA_SBIN/cl_dominostart -d $SERVER_DATA_DIR -p $DOMINO_PROG_DIRECTORY" 
        	stopscript="$LOTUS_DOMINO_HA_SBIN/cl_dominostop -d $SERVER_DATA_DIR -p $DOMINO_PROG_DIRECTORY"
       		monitorscript="$LOTUS_DOMINO_HA_SBIN/cl_dominomonitor -d $SERVER_DATA_DIR -p $DOMINO_PROG_DIRECTORY"

		KLIB_SA_logmsg INFO 1 26 lotusdominosa.cat "Adding Lotus domino server Application %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 27 lotusdominosa.cat "Failed while adding Lotus domino server Application %1\$s\n" $appname
                                  clean_Up $rgname $SERVER_IP   
                                  exit 1
                                 }

	        KLIB_SA_logmsg INFO 1 28 lotusdominosa.cat "Adding Lotus domino server 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='120' \
       			HUNG_MONITOR_SIGNAL='9' \
       			STABILIZATION_INTERVAL='180' \
       			RESTART_COUNT='3' \
       			RESTART_INTERVAL='900' >/dev/null 2>&1 || {
                                 KLIB_SA_logmsg ERROR 1 29 lotusdominosa.cat "Failed while adding Lotus domino server Application monitor %1\$s\n" $appmonname
                                 clean_Up $rgname $SERVER_IP $appname 
                                 exit 1
                                }

 	        # Add service IP, Application to Resource Group
       		KLIB_SA_logmsg INFO 1 30 lotusdominosa.cat "Adding Lotus domino serveice IP to Resource Group %1\$s\n" $rgname
		if [[ $DOMINO_CLSTR == "y" ]];then
       			clvt modify resource_group "$rgname" \
       				SERVICE_LABEL="$SERVER_IP" \
        			APPLICATIONS="$appname" >/dev/null 2>&1 || {
                                          KLIB_SA_logmsg ERROR 1 31 lotusdominosa.cat "Failed while adding Lotus domino service IP to Resource Group %1\$s\n" $rgname
                                          clean_Up $rgname $SERVER_IP $appname $appmonname
                                          exit 1
                                  }
		else
			VG=$(dir_fs_vg $SERVER_DATA_DIR 0)
			clvt modify resource_group "$rgname" \
                                SERVICE_LABEL="$SERVER_IP" \
				FILESYSTEM="ALL" \
				VOLUME_GROUP="$VG" \
                                APPLICATIONS="$appname" >/dev/null 2>&1 || {
                                          KLIB_SA_logmsg ERROR 1 31 lotusdominosa.cat "Failed while adding Lotus domino service IP to Resource Group %1\$s\n" $rgname
                                          clean_Up $rgname $SERVER_IP $appname $appmonname
                                          exit 1
                                  }
		fi

		# Add the Resource Group and Application entry to HACMPsa_metadata ODM
	        KLIB_SA_logmsg INFO 1 33 lotusdominosa.cat "Adding Lotus domino Resource Configuration to ODM\n"

       		claddsaapp -a $APPLICATION_ID \
        		RESOURCE_GROUP_$i=$rgname \
			RG_PRI_NODE_$i=$SERVER_NODE \
			RG_DOMINO_DIR_$i=$SERVER_DATA_DIR >/dev/null 2>&1 || {
               			KLIB_SA_logmsg ERROR 1 34 lotusdominosa.cat "Failed while Adding Lotus domino server configuration to ODM\n"   
               			clean_Up $rgname $SERVER_IP $appname $appmonname
                                       exit 1
               	    	 }
		i=$(expr $i + 1)
		NODE_DATA=$(echo "$DATA_DIR"|/usr/bin/cut -d ';' -f $i)
	done				

	i=$(expr $i - 1)
	claddsaapp -a $APPLICATION_ID \
		APP_NODES=$NODES \
		SMARTASSIST_ID="$SMARTASSIST_ID" \
		COMPONENT_ID="$COMPONENT_ID" \
		APP_DOMINO_CLUSTER=$DOMINO_CLSTR \
		RESOURCE_GROUP_COUNT="$i" \
		APPLICATION_NAME="$APPLICATION_ID" >/dev/null 2>&1 || {
			      KLIB_SA_logmsg ERROR 1 34 lotusdominosa.cat "Failed while Adding Lotus domino server configuration to ODM\n"
                             clean_Up $rgname $SERVER_IP $appname $appmonname
                             exit 1
                                 }


	# generate verify scripts for verification
	if [[ $DOMINO_CLSTR == "n" ]];then
	        dspmsg -s 1 lotusdominosa.cat 36 "Generating verify script for Lotus domino server configuration %1\$s\n" $APPLICATION_ID
		generateVerifyScript $COMPONENT_ID $SMARTASSIST_ID 
        	[[ "$?" == 1 ]] && {
                          KLIB_SA_logmsg ERROR 1 37 lotusdominosa.cat "Failed while Generating verify script for Lotus domino server configuration %1\$s\n" $APPLICATION_ID
                          clean_Up $rgname $SERVER_IP $appname $appmonname
                          exit 1
                     }
	fi
	return 0
}

#################################################################################
# Function: modifyserverInstance
#
# Purpose: To modify PowerHA SystemMirror resources and Resource group for Managing
#          Lotus domino server
#
# 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
#          Lotus domino server
#
# Arguments: None.
#
# Returns: 0 for Sucess
#          1 for Failure
#
################################################################################

function removeHAresources  {

        [[ "$VERBOSE_LOGGING" == "high" ]] && set -x
	KLIB_SA_logmsg INFO 1 38 lotusdominosa.cat "Removing Lotus domino server %1\$s from PowerHA SystemMirror configuration\n" $APPLICATION_ID
	RG_cnt=$(clodmget -q "name=RESOURCE_GROUP_COUNT and application_id=$APPLICATION_ID" -f value -n HACMPsa_metadata)
	[[ $? != 0 ]] && {
                                KLIB_SA_logmsg ERROR 1 39 lotusdominosa.cat "Resource Group count %1\$s not found in ODM\n" $rg
                                exit 1
                         }
	while (( $RG_cnt > 0 ))
	do
        	rg=$(clodmget -q "name=RESOURCE_GROUP_$RG_cnt and application_id=$APPLICATION_ID" -f value -n HACMPsa_metadata)
        	[[ $? != 0 ]] && {
                                KLIB_SA_logmsg ERROR 1 40 lotusdominosa.cat "Resource Group %1\$s not found in ODM\n" $rg
                                exit 1
                         }
		RG_cnt=$(expr $RG_cnt - 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 41 lotusdominosa.cat "Failed while deleting Application monitor %1\$s for changing Lotus domino server 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 42 lotusdominosa.cat "Failed while deleting Application for changing Lotus domino server configuration%1\$s\n" $app
                                        exit 1
                                }
        	done

 	        clvt delete resource_group $rg >/dev/null 2>&1
        	(( $? != 0 )) && {
                                KLIB_SA_logmsg ERROR 1 43 lotusdominosa.cat "Failed while deleting Resource Group %1\$s for changing Lotus domino server configuration\n" $rg
                                exit 1
                         }
	done
        # Remove the metadata entries
        clrmsaapp -a $APPLICATION_ID >/dev/null 2>&1
        (( $? != 0 )) && {
                                KLIB_SA_logmsg ERROR 1 44 lotusdominosa.cat "Failed while deleting for changing Lotus domino server ODM configuration\n"
                                exit 1
                         }

        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

    #
    # Can the service IP label be resolved?
    #
    clvt query interface | grep $SERVER_IP > /dev/null 2>&1
    if [[ $? == "0" ]];then
        clvt delete interface $SERVER_IP
    fi

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

        nodes=$(echo "$NODES_RG")

        addr=$(/usr/bin/host $SERVER_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
        [[ -z $network ]] && {
            KLIB_SA_logmsg ERROR 1 46 lotusdominosa.cat "No suitable PowerHA SystemMirror network is defined to create service IP label\n"
            return 1
        }

        /usr/es/sbin/cluster/utilities/claddnode -T service -B "$SERVER_IP" -P "$NETMASK" -w "$network" > /dev/null 2>&1
        (( $? != 0 )) && {
            KLIB_SA_logmsg ERROR 1 47 lotusdominosa.cat "Error while adding service IP label %1\$s to network %2\$s\n" $SERVER_IP $network
            return 1
        }
    }

    return 0
}
#----------------------------------------------------------------------------
# Function:
#   getVerifyHeader
#
# Purpose:
#   Generate the verification parameterized script header for Lotus domino 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 48 lotusdominosa.cat "Generating verify Header for Lotus domino server Application name %1$s\n" $sa_id
    echo "#\n# PowerHA SystemMirror / Lotus domino server"
    echo "#Lotus domino server 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=DOMINO:A=$sa_id\""
    return 0

}

#----------------------------------------------------------------------------
# Function:
#   generateDominoVerifyScript
#
# Purpose:
#   Generates the parameterized verification script for
#   Lotus domino server smart assist.
#
# Arguments:
#  n/a
#
# Returns:
#   0 on success
#   1 on failure
#----------------------------------------------------------------------------
function generateDominoVerifyScript {
    [[ "$VERBOSE_LOGGING" == "high" ]] && set -x
    # Generates a parameterised verification script to
    # Obtain Lotus domino server instance user and group, validate that the user and group exists
    # on all nodes in the cluster. 
    c=0
    KLIB_SA_logmsg INFO 1 49 lotusdominosa.cat "Generating verify Script for Lotus domino server %1\$s\n" $APPLICATION_ID
    echo "\n#\n# Perform validation for user: $APPLICATION_ID\n#"

    echo "HAVerify.User.severity = \"ERROR\""
    for i in $SERVER_USERS
    do
    	echo "HAVerify.User.exists[$c].name=\"$i\""
	c=$(expr $c + 1)
    done

    c=0
    echo "HAVerify.Group.exists.severity = \"ERROR\""
    for i in $SERVER_GROUPS
    do
	echo "HAVerify.Group.exists[$c].name =\"$i\""
	c=$(expr $c + 1)
    done
    return 0
}

#----------------------------------------------------------------------------
# Function:
#   generateVerifyScript
#
# Purpose:
#   Generates the cluster verification parameterized script for
#   Lotus domino server smart assist, 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=$NODES
    typeset localnode=$(get_local_nodename)

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

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

    {
        getVerifyHeader $cid $sa_id
        generateDominoVerifyScript

    } > $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 50 lotusdominosa.cat "Pushing the verification scripts to other node %1$s is failed\n" $node
                                return 1
                             }
        }
    done

    file_coll_name=$(echo $cid"_"$APPLICATION_ID)

    clvt delete file_collection $file_coll_name >/dev/null 2>&1
    clvt add file_collection $file_coll_name \
        FILES="$TEST_ROOT/$VERIFICATION_FILENAME" \
        ISPROPOGATEDFILEDURINGSYNC="true" \
        ISPROPOGATEAUTOWHENDETECTED="true" >/dev/null 2>&1

    claddsaapp -a $APPLICATION_ID FILE_COLLECTION=$file_coll_name
    return 0
}

###########################################
# main
###########################################

while getopts a:n:p:d:m opt
do
	case $opt in
		m) # modify Lotus domino 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 lotusdominosa.cat "Validating Smart Assist for Lotus domino application id\n"
                	validateAppName "$APPLICATION_ID"
	                [[ "$?" == "1" ]] && {
        	        KLIB_SA_logmsg ERROR 1 11 lotusdominosa.cat "smart assist for Lotus domino 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 lotusdominosa.cat "completion of validating smart Assist for Lotus domino applicaton id=%1\$s\n" "$APPLICATION_ID"
        	        ;;
		n) # Participating nodes in the cluster
			NODES=$OPTARG
			;;
		p) # Domino server and data directory
			DATA_DIR=$OPTARG
			;;
		d) # Domino cluster between the domino servers
			DOMINO_CLSTR=$OPTARG
	esac
done

#Validate the node names, if there are any Name duplication for Participating nodes
#
KLIB_SA_logmsg INFO 1 13 lotusdominosa.cat "Validating Lotus domino server nodes\n"
validateNodes "$NODES"
(( $? != 0 )) && {
	KLIB_SA_logmsg ERROR 1 14 lotusdominosa.cat "smart assist for Lotus domino server has nodes=%1\$s as repetitive\n" "$NODES"
	exit 1
}
if [[ "$MODIFY" == "Y" ]]
then
        KLIB_SA_logmsg INFO 1 15 lotusdominosa.cat "Modify the Lotus domino server configuration %1\$s\n" $APPLICATION_ID
        dspmsg -s 1 lotusdominosa.cat 15 " Modifying Lotus domino server configuration %1\$s\n" $APPLICATION_ID
        modifyserverInstance
        exit $?
else
        #check If the Domino server is already configured to PowerHA SystemMirror.

        KLIB_SA_logmsg INFO 1 16 lotusdominosa.cat "Checking if the domino server is already configured.\n"
        app_id=$(clodmget -q "name=APPLICATION_NAME" -f application_id -n HACMPsa_metadata)

        [[ $app_id == $APPLICATION_ID ]] &&  {
                        KLIB_SA_logmsg ERROR 1 17 lotusdominosa.cat "Lotus domino server with application is %1\$s already configured\n" $APPLICATION_ID
                        exit 1
             }
        addserverInstance
        exit $?
fi
