# @(#)31	1.1  src/43haes/usr/sbin/cluster/sa/domino/sbin/cl_lotusdominoUtilities.sh, hacmp.assist, 61haes_r714 11/28/11 15:14:03
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/sa/domino/sbin/cl_lotusdominoUtilities.sh 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 
########################################################################################
# Function:
#   isServiceIPLabelAlreadyDefined
#
# Purpose:
#   Validate that the service IP label isn't already defined in the PowerHA SystemMirror
#   cluster.  If it is already defined, report an error and exit this script
#
# Arguments:
#   (1) Service IP Label
#
# Return:
#   1 if service IP label is not defined
#   0 if service IP label is already defined
######################################################################################

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

    typeset serviceIP=$1
    clvt query resource_group >/dev/null 2>&1
    if [[ $? == "0" ]];then
        RGS=$(clvt query resource_group)
    	for rg in $RGS; do
        	ips=$(clvt query resource_group $rg | grep \
              		SERVICE_LABEL | awk -F= '{ print $2 }')
        	ips=${ips//\"/}
        	for ip in $ips; do
            		[[ "$ip" == "$serviceIP" ]] && {
                	return 0
            		}
       		 done
    	done
    fi 
    return 1
}

#############################################################################
# Function:
#   getServiceNetwork
#
# Purpose:
#   Determine the appropriate network to place the service IP label
#   on based on adapter count per network per node
#
# Arguments:
#    (1) network type in PowerHA SystemMirror
#   (2) list of nodes to find interfaces for
#
# Output:
#   network name to place service IP label on
#
# Returns:
#   0 on success
#   1 on failure
############################################################################

function getServiceNetwork {
    [[ "$VERBOSE_LOGGING" == "high" ]] && set -x
    serv_addr_type=$1
    NODES=$2
    validv6Networks=""
    validv4Networks=""
    typeset NETWORKS=$(clvt query network | sort -u)
    typeset validNetworks
    typeset -A netXnodeCount

    for network in $NETWORKS
    do
        fam=$(clvt query network $network|
        awk -F= '$1 ~ /NET_FAMILY/ { print $2 }' | sed -e "s/\"//g")
        if [[ "$fam" == "2" ]]
        then
            validv6Networks=$(echo "$network $validv6Networks")
        else
            validv4Networks=$(echo "$network $validv4Networks")
        fi
    done

    INTERFACES=$(clvt query interface | sort -u)
    for interface in $INTERFACES; do
        node=$(clvt query interface $interface |
            awk -F= '$1 ~ /NODE/ {print $2 }' | sed -e "s/\"//g")
        net=$(clvt query interface $interface |
            awk -F= '$1 ~ /NETWORK/ {print $2 }' | sed -e "s/\"//g")
        [[ -n $node && -n $net ]] && {
            typeset -i count=${netXnodeCount[${net}_${node}]}
            (( count++ ))
            netXnodeCount[${net}_${node}]=$count
        }
    done

    typeset -i invalid=0

    if [[ "$serv_addr_type" == "2" || -z $validv4Networks ]]
    then
        for network in $validv6Networks; do
            invalid=0
            for node in $NODES; do
                [[ ${netXnodeCount[${network}_${node}]} == 0 ]] && {
                    invalid=1
                }
            done

            [[ $invalid == 0 ]] && {
            echo $network
            return 0
            }
       done
    fi

    for network in $validv4Networks; do
        invalid=0
        for node in $NODES; do
            [[ ${netXnodeCount[${network}_${node}]} == 0 ]] && {
                invalid=1
            }
         done

         [[ $invalid == 0 ]] && {
         echo $network
         return 0
         }
    done

    return 1
}

#######################################################################################################
#
# Function : validateNodes
#
# Purpose : Validate if the Primary and takeover nodes are not repetetive.
#
# Arguments : PRIMNODE
#             TAKEOVNODES
#
# Returns: 0 for Sucess
#          1 for Failure
#
######################################################################################################

function validateNodes {

        for node in $2
        do
                [[ $1 == $node ]] && return 1
        done

        return 0
}

#############################################################################################
# Function:
#   getUnusedName
#
# Purpose:
#   Obtain an un-used name for an PowerHA SystemMirror component (application, monitor,
#   resource group, etc)
#
# Arguments:
#   (1) by reference - name
#   (2) type - (as used in clvt command
#           resource_group |
#           application |
#           application_monitor, etc.)
#
# Output:
#   output contains the available name
#
# Returns:
#   0 on success
#   1 on failure
############################################################################################

function getUnusedName {
    [[ "$VERBOSE_LOGGING" == "high" ]] && set -x
    typeset name=$1
    typeset type=$2

    typeset newname=$name
    typeset ret
    typeset -i count=1

    clvt query $type $newname >/dev/null 2>&1
    ret=$?
    while [[ $ret == "0" ]]; do

        (( count++ ))
        # Give up after 9 attempts
        if (( $count > 9 )); then
            echo $name
            return 1
        fi
        newname=${name:0:31}
        newname=${newname}${count}
        clvt query $type $newname >/dev/null 2>&1
	ret=$?
    done
    echo $newname
    return 0
}

#######################################################################################################
#
# Function : validateAppName
#
# Purpose : Validate if the Application name entered has valid charactes. charecters can be
#           [a-z][A-z][0-9]_
#
# Arguments :APPLICATION_ID
#
# Returns: 0 for Sucess
#          1 for Failure
#
######################################################################################################

function validateAppName {

   appname=$1

   [[ -n ${appname//[a-zA-Z0-9_]/} ]] &&  return 1

    return 0

}

#####################################################################################################
#
# Function : isipv6addr
#
# Purpose : Finds the address is IPv4 or IPV6
#
# Args:
#       <ipv6/ipv4 address>
#
# Returns :
#   0 for success
#   1 for failure (when the address is not IPv6)
#
#######################################################################################################

function isipv6addr
{
        typeset v6addr=${1:-}
        abc=$(echo $v6addr|awk '
                        { n=split($1, A, ":")
                          if(n==1)
                          {
                                print n
                                exit
                          }
                            for(i=1; i<=n; i++)
                            {
                                if(length(A[i]))
                                {
                                    if(i == n)
                                        printf "%s", A[i]
                                    else
                                        printf "%s:", A[i]
                                }
                                else
                                {
                                        if(i==1)
                                            s=1
                                        else
                                            s = 9-n

                                        for(j=1; j<=s; j++)
                                            printf "%s:", "0"
                                }
                            }
                        }')

        if [[ $abc == 1 ]]
        then
        return 1
        else
        return 0
        fi

}

#####################################################################################################
#
# Function : dir_fs_vg
#
# Purpose : If the directory is given it finds File system or VG.
#           dir_fs_vg $dir 0/1
#       second argument 0 -- it return the VG
#                       1 -- it returns file system
#
# Args:
#     1. directory
#     2. 0/1
#
# Returns :
#   0 for success
#   1 for failure
#
#######################################################################################################
function dir_fs_vg {
        [[ "$VERBOSE_LOGGING" == "high" ]] && set -x
        dir=$1
        fs=$dir
        i=0
        count=$(echo $dir|awk '{ n=split($1, A, "/");print n }')
        count=$(expr $count - 1)
        while [[ $i < $count ]]
        do
                i=$i
                m_fs=$(lsfs -c|grep $fs |grep -v grep)
                [[ $? == 0 ]] && {
                                        [[ $2 == 1 ]] && {
                                                                fs_d=$(echo $m_fs|awk -F: '{print $1}')
                                                                echo $fs_d
                                                                return 0
                                                         }
                                        lv=$(echo $m_fs|awk -F: '{print $2}'|awk -F/ '{print $3}')
                                        vg=$(clodmget -q "name=$lv" -f parent -n CuDv)
                                        echo $vg
                                        return 0
                                }
                fs=$(echo $fs|cut -d "/" -f -`expr $count - $i`)
                i=$(expr $i + 1)
        done
        return 1
}

#####################################################################################################
#
# Function : getUnusedappname
#
# Purpose : obtain an unused application name for the Lotus domino configuration
#
# Args:
#     app_name
#
# Returns :
#   0 for success
#   1 for failure
#
#######################################################################################################
function getUnusedappname {
        [[ "$VERBOSE_LOGGING" == "high" ]] && set -x
        app_name=$1
        app=$(/usr/es/sbin/cluster/utilities/clodmget -q "name=APPLICATION_NAME AND value=$app_name" -f value HACMPsa_metadata 2>/dev/null)
        if [[ -z $app ]];then
                echo $app_name
                return 0
        else
                c=1
                while (( $c < 32 ))
                do
                        new_app=$( echo $app_name"_"$c)
                        app=$(/usr/es/sbin/cluster/utilities/clodmget -q "name=APPLICATION_NAME AND value=$new_app" -f value HACMPsa_metadata 2>/dev/null)
                        [[ -z $app ]] && {
                                echo $new_app
                                return 0
                        }
                        c=$(expr $c + 1)
                done
        fi
        return 1
}
