#!/bin/ksh93
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/lib/ksh93/hacmp/KLIB_HACMP_delete_dependency.sh 1.3 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1990,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 
# @(#)01	1.3  src/43haes/lib/ksh93/hacmp/KLIB_HACMP_delete_dependency.sh, hacmp.assist, 61haes_r714 6/10/12 15:11:24

# Start of POD-formatted documentation. Viewing suggestions:
#      perldoc <FILENAME>
#      pod2text -c <FILENAME>
#      pod2text -c --code <FILENAME>
#      pod2html <FILENAME>
function devDoc {
    : <<'=cut' >/dev/null 2>&1

=head1 NAME

 KLIB_HACMP_delete_dependency

=head1 VERSION

 Version Number:  1.3
 Last Extracted:  1/31/14 04:41:46
 Last Changed:    6/10/12 15:11:24

 Path, Component, Release(, Level):
 src/43haes/lib/ksh93/hacmp/KLIB_HACMP_delete_dependency.sh, hacmp.assist, 61haes_r714

=head1 SYNOPSIS

 clmgr delete dependency {<dependency> | ALL} \
             [ TYPE={PARENT_CHILD|STOP_AFTER|START_AFTER|\
                     SAME_NODE|SAME_SITE|DIFFERENT_NODES} ]

=head1 DESCRIPTION

Deletes the specified dependency from the cluster configuration.

=head1 ARGUMENTS

 1. dependency [REQUIRED] [string]
    The dependency that is to be deleted.

 2. explicit_type [OPTIONAL] [string]
    The type of dependency that is being deleted. This is strictly a
    performance optimization, to eliminate having to divine the type
    from the dependency string itself. From the following set:

        PARENT_CHILD
        STOP_AFTER
        START_AFTER
        SAME_NODE or NODECOLLOCATION
        SAME_SITE or SITECOLLOCATION,
        DIFFERENT_NODES or ANTICOLLOCATION

 3. rg [OPTIONAL] [string]
    A specific resource group to target for dependency removal.
    If this value is specified, every dependency that involves
    the specified resource group will be removed.

=head1 RETURN

 0: no errors were detected; the operation appears to have been successful
 1: a general error has occurred
 2: a specified resource does not exist, or could not be found
 3: some required input was missing
 4: some detected input was incorrect in some way
 5: a required dependency does not exist
 6: a specified search failed to match any data

=head1 COPYRIGHT

COPYRIGHT International Business Machines Corp. 2005,2010
All Rights Reserved

=cut
} # End of POD-formatted documentation.


function KLIB_HACMP_delete_dependency {
    LINENO=2 . $HALIBROOT/log_entry "$0()" "$CL"
    : version=1.3, src/43haes/lib/ksh93/hacmp/KLIB_HACMP_delete_dependency.sh, hacmp.assist, 61haes_r714
    : INPUTS: $*

    typeset DEPENDENCIES=${1//\"/}   # Required
    typeset explicit_type=${2//\"/}  # Optional
    typeset rg=${3//\"/}             # Optional

    [[ $CLMGR_LOGGING == 'med' ]] && set +x  # Only trace param values

    #===================================
    : Declare and initialize variables
    #===================================
    typeset -i rc=$RC_UNKNOWN
    typeset -i i=0
    typeset dependency= implicit_type=
    typeset -u type=

    #================================================================
    : Check for a defined cluster. No need to continue without one.
    #================================================================
    CL=$LINENO isClusterDefined
    (( $? != RC_SUCCESS )) && rc=$RC_MISSING_DEPENDENCY

    #================================================================
    : Assuming an object was specified, see if it is a known object
    #================================================================
    if (( $rc == RC_UNKNOWN )); then
        if [[ $DEPENDENCIES == *([[:space:]]) && -z $rg ]]; then
            /usr/bin/dspmsg -s $CLVT_SET $CLVT_MSGS 100 "\nERROR: a name/label must be provided.\n\n" 1>&2
            rc=$RC_MISSING_INPUT

        elif [[ -n $DEPENDENCIES && -n $rg ]]; then
            /usr/bin/dspmsg -s $CLVT_SET $CLVT_MSGS 36 "\nERROR: conflicting options were provided,\n\       \"%1\$s\" versus \"%2\$s\".\n\n" "$DEPENDENCIES" RESOURCE_GROUP 1>&2
            rc=$RC_INCORRECT_INPUT

        else
            typeset -u uctext=$DEPENDENCIES
            if [[ $uctext == "ALL" || -n $rg ]]; then
                DEPENDENCIES=
                typeset available
                CL=$LINENO KLIB_HACMP_list_dependencies available
                for (( i=0; i<${#available[*]}; i++ )); do
                    dep="${available[$i]}"
                    [[ $dep == *@(SERIAL|PARALLEL)* ]] && continue
                    dep=${dep// /}
                    DEPENDENCIES="$DEPENDENCIES $dep"
                done

                if [[ $DEPENDENCIES == *([[:space:]]) ]]; then
                    /usr/bin/dspmsg -s $CLVT_SET $CLVT_MSGS 322 "Warning: there are no resource group dependencies to delete.\n"
                    rc=$RC_SUCCESS
                fi
            fi
        fi
    fi

    #==============================================================
    : Delete the dependency if no input errors have been detected
    #==============================================================
    if (( $rc == RC_UNKNOWN )); then
        for dependency in $DEPENDENCIES; do

            if [[ $dependency == *\(*\)* ]]; then
                implicit_type=${dependency##*\(}
                implicit_type=${implicit_type%%\)*}
                type=$implicit_type
                dependency=${dependency/\(*\)}
            fi
            if [[ -n $explicit_type ]]; then
                if [[ $explicit_type == *\(*\)* ]]; then
                    explicit_type=${explicit_type##*\(}
                    explicit_type=${explicit_type%%\)*}
                fi
                type=$explicit_type
            fi

            #=================
            : Validate input
            #=================
            if (( $rc == RC_UNKNOWN )); then
                if [[ -n $implicit_type && -n $explicit_type && \
                      $implicit_type != $explicit_type ]]
                then
                    /usr/bin/dspmsg -s $CLVT_SET $CLVT_MSGS 314 "\nERROR: the specified implicit dependency type, \"%1\$s\", does\n       not match the specified explicit dependency type, \"%2\$s\".\n\n" "$implicit_type" "$explicit_type" 1>&2
                    rc=$RC_INCORRECT_INPUT
                fi
            fi

            if [[ -z $type ]]; then
                CL=$LINENO KLIB_HACMP_is_known_dependency "$dependency"
                : "DEPTYPE" gets set by KLIB_HACMP_is_known_dependency
                if [[ $DEPTYPE == *[[:space:]]* ]]; then
                    /usr/bin/dspmsg -s $CLVT_SET $CLVT_MSGS 315 "\nERROR: the specified dependency, \"%1\$s\", is ambiguous\n       and could be referring to any of the following dependency types:\n           %2\$s\n       Please specify which dependency type you intended to delete by\n       using the \"TYPE\" attribute.\n" "$dependency" "$DEPTYPE" 1>&2
                    rc=$RC_INCORRECT_INPUT
                else
                    type=$DEPTYPE
                fi
            fi

            if [[ $type == @(ACQ|REL)* ]]; then
                typeset ERRMSG="\
ERROR: the specified dependency cannot be deleted:

           %1\$s

       It is a resource group processing order dependency (its type is
       \"%2\$s\"), so it can only be modified, not removed.
       If your intention was to remove a different dependency, of a
       different type, then you will need to specify the type using
       the \"TYPE\" option. This can happen when a dependency uses the
       same nodes as one of the resource group processing orders.\n
"
                CL=$LINENO cl_dspmsg -s $CLVT_SET $CLVT_MSGS 352 "$ERRMSG" "$dependency" "$type" 1>&2
                rc=$RC_INCORRECT_INPUT
                break
            fi

            CL=$LINENO KLIB_HACMP_is_known_dependency "$dependency" "$type"
            typeset -i is_known=$?
            if (( $is_known != RC_SUCCESS )); then
                if [[ -n $type ]]; then
                    /usr/bin/dspmsg -s $CLVT_SET $CLVT_MSGS 312 "\nERROR: no \"%1\$s\" dependencies involving \"%2\$s\"\n       could be found.\n\n" "$type" "$dependency" 1>&2
                else
                    /usr/bin/dspmsg -s $CLVT_SET $CLVT_MSGS 313 "\nERROR: no dependencies involving \"%1\$s\" could be found.\n\n" "$dependency" 1>&2
                fi

                /usr/bin/dspmsg -s $CLVT_SET $CLVT_MSGS 142 "Available Dependencies:\n\n" 1>&2
                typeset available
                CL=$LINENO KLIB_HACMP_list_dependencies available
                for (( i=0; i<${#available[*]}; i++ )); do
                    if [[ ${available[$i]} != *([[:space:]]) ]]; then
                        print -u2 "\t${available[$i]}"
                    fi
                done
                print -u2 ""
                rc=$RC_NOT_FOUND
            else
                #=========================================================
                : If a specified resource group was provide, only delete
                : the dependency if it contains that resource group.
                #=========================================================
                if [[ -n $rg ]]; then
                    typeset split=${dependency//\+/ }
                    split=${split//,/ }
                    [[ " $split " != *\ $rg\ * ]] && continue
                fi

                case $type in
                    "PARENT_CHILD")
                        current_parent=${dependency%%\+*}
                        current_parent=${current_parent//,/ }
                        current_children=${dependency##*\+}
                        current_children=${current_children//,/ }

                        for child in $current_children; do
                            print -- "$0()[$LINENO]($SECONDS): $HAUTILS/clrgdependency -t \"$type\" -d -p \"$current_parent\" -c \"$child\"" >>$CLMGR_TMPLOG  # Always log commands
                            $HAUTILS/clrgdependency -t "$type" -d \
                                                    -p "$current_parent" \
                                                    -c "$child"
                            (( $rc == RC_UNKNOWN || $rc == RC_SUCCESS )) && rc=$?
                        done
                    ;;
                    "STOP_AFTER")
                        current_after=${dependency%%\+*}
                        current_after=${current_after//,/ }
                        current_stop=${dependency##*\+}
                        current_stop=${current_stop//,/ }

                        for child in $current_stop; do
                            print -- "$0()[$LINENO]($SECONDS): $HAUTILS/clrgdependency -t \"$type\" -d -c \"$current_after\" -p \"$child\"" >>$CLMGR_TMPLOG  # Always log commands
                            $HAUTILS/clrgdependency -t "$type" -d \
                                                    -c "$current_after" \
                                                    -p "$child"
                            (( $rc == RC_UNKNOWN || $rc == RC_SUCCESS )) && rc=$?
                        done
                    ;;
                    "START_AFTER")
                        current_after=${dependency%%\+*}
                        current_after=${current_after//,/ }
                        current_start=${dependency##*\+}
                        current_start=${current_start//,/ }

                        for child in $current_start; do
                            print -- "$0()[$LINENO]($SECONDS): $HAUTILS/clrgdependency -t \"$type\" -d -p \"$current_after\" -c \"$child\"" >>$CLMGR_TMPLOG  # Always log commands
                            $HAUTILS/clrgdependency -t "$type" -d \
                                                    -p "$current_after" \
                                                    -c "$child"
                            (( $rc == RC_UNKNOWN || $rc == RC_SUCCESS )) && rc=$?
                        done
                    ;;
                    @(SAME_NODE|NODECOLLOCATION))
                        print -- "$0()[$LINENO]($SECONDS): $HAUTILS/clrgdependency -t \"NODECOLLOCATION\" -d -l\"${dependency//,/ }\"" >>$CLMGR_TMPLOG  # Always log commands
                        $HAUTILS/clrgdependency -t "NODECOLLOCATION" -d \
                                                -l"${dependency//,/ }"
                        rc=$?
                    ;;
                    @(SAME_SITE|SITECOLLOCATION))
                        print -- "$0()[$LINENO]($SECONDS): $HAUTILS/clrgdependency -t \"SITECOLLOCATION\" -d -l\"${dependency//,/ }\"" >>$CLMGR_TMPLOG  # Always log commands
                        $HAUTILS/clrgdependency -t "SITECOLLOCATION" -d \
                                                -l"${dependency//,/ }"
                        rc=$?
                    ;;
                    @(DIFFERENT_NODES|ANTICOLLOCATION))  # Only one of these
                        print -- "$0()[$LINENO]($SECONDS): $HAUTILS/clrgdependency -t \"ANTICOLLOCATION\" -u -hp \"\" -ip \"\" -lp \"\"" >>$CLMGR_TMPLOG  # Always log commands
                        $HAUTILS/clrgdependency -t "ANTICOLLOCATION" -u \
                                                -hp "" \
                                                -ip "" \
                                                -lp ""
                        rc=$?
                    ;;
                    *)
                        rc=$RC_ERROR
                    ;;
                esac
                print "clrgdependency RC: $rc" >>$CLMGR_TMPLOG  # Always log command result
                (( $rc != RC_SUCCESS && $rc == RC_UNKNOWN )) && rc=$RC_ERROR
            fi
        done
    fi

    #=======================================================================
    : If a user input error was detected, provide some helpful suggestions
    #=======================================================================
    if (( $rc == RC_MISSING_INPUT || $rc == RC_INCORRECT_INPUT )) && \
       [[ $CLVT_GUI == *([[:space:]]) ]]
    then
        CL=$LINENO cl_dspmsg -s $CLVT_SET $CLVT_MSGS 104 "For more information about available options and syntax, try\n\"$HAUTILS/clmgr %1\$s\". As an\nalternative, if the PowerHA SystemMirror man pages have been installed, invoke\n\"$HAUTILS/clmgr -hv\" (or \"/usr/bin/man clmgr\"),\nsearching for \"%2\$s\" in the displayed text.\n\n" \
        "delete dependency -h" "DEPENDENCY:" "$CLVT_PROGNAME" 1>&2
    fi

    (( $rc == RC_UNKNOWN )) && rc=$RC_SUCCESS

    log_return_msg "$rc" "$0()" "$LINENO"
    return $?
} # End of "KLIB_HACMP_delete_dependency()"
