#!/bin/ksh93
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/lib/ksh93/hacmp/KLIB_HACMP_modify_method.sh 1.7 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2009,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 
# @(#)37	1.7  src/43haes/lib/ksh93/hacmp/KLIB_HACMP_modify_method.sh, hacmp.assist, 61haes_r714, 1415A_hacmp714 1/13/14 16:46:13

#================================================
# The following, commented line enforces coding
# standards when this file is edited via vim.
#================================================
# vim:tabstop=4:shiftwidth=4:expandtab:smarttab
#================================================

# 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_modify_method

=head1 VERSION

 Version Number:  1.7
 Last Extracted:  4/18/14 01:31:41
 Last Changed:    1/13/14 16:46:13

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

=head1 SYNOPSIS

 clmgr modify method <method_label> \
             TYPE={snapshot|verify} \
             [ NAME=<new_method_label> ] \
             [ DESCRIPTION=<new_description> ] \
             [ FILE=<new_executable_file> ]

 clmgr modify method <method_label> \
             TYPE=notify \
             [ NAME=<new_method_label> ] \
             [ DESCRIPTION=<description> ] \
             [ FILE=<message_file> ] \
             [ CONTACT=<number_to_dial_or_email_address> ] \
             [ EVENT=<cluster_event_label> ] \
             [ NODES=<node>[,<node#2>,<node#n>,...] ] \
             [ RETRY=<retry_count> ] \
             [ TIMEOUT=<timeout> ]

 NOTE: the alias for "method" is "me".

=head1 DESCRIPTION

Attempts to modify the specified custom method
to conform to the provided specifications.

=head1 ARGUMENTS

 1. properties [REQUIRED] [hash ref]
    An associative array within which data about the
    modified object can be returned to the caller.

 2. name [REQUIRED] [string]
    The label of the method that is to be modified.

 3. type [REQUIRED] [string]
    The type of method to be created.

    Valid values: {notify|snapshot|verify}

 4. new_name[OPTIONAL] [string]
    A new label for the method.

 5. new_desc [OPTIONAL] [string]
    A new description for the method.

 6. new_file [OPTIONAL] [string]
    A new executable file for the method.

 7. contact [REQUIRED FOR NOTIFY] [string]
    The contact information. Might be an email address, pager number, etc...

 8. events [REQUIRED FOR NOTIFY] [string]
    The event(s) to provide notification about (i.e. when that event occurs).

 9. nodes [OPTIONAL FOR NOTIFY] [string]
    The nodes that the notification will be sent from, in priority order.

 10. retry [OPTIONAL FOR NOTIFY] [string]
    The number of times to attempt to send the notification before giving up.

 11. timeout [OPTIONAL FOR NOTIFY] [string]
    The maximum number of seconds to attempt each notification before giving up.

=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_modify_method {
    LINENO=2 . $HALIBROOT/log_entry "$0()" "$CL"
    : version=1.7, src/43haes/lib/ksh93/hacmp/KLIB_HACMP_modify_method.sh, hacmp.assist, 61haes_r714, 1415A_hacmp714
    : INPUTS: $*

    typeset -n properties=$1
    typeset name=${2//\"/}
    shift; shift

    typeset -l type=${1//\"/}
    typeset new_name=${2//\"/}
    typeset new_desc=${3//\"/}
    typeset new_file=${4//\"/}
    typeset contact=${5//\"/}  # Notify methods only
    typeset events=${6//\"/}   # Notify methods only
    typeset nodes=${7//\"/}    # Notify methods only
    typeset retry=${8//\"/}    # Notify methods only
    typeset timeout=${9//\"/}  # Notify methods only

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

    #================================================================
    : Check for a defined cluster. No need to continue without one.
    #================================================================
    CL=$LINENO isClusterDefined
    if (( $? != RC_SUCCESS )); then
        log_return_msg "$RC_MISSING_DEPENDENCY" "$0()" "$LINENO"
        return $?
    fi

    #===================================
    : Declare and initialize variables
    #===================================
    typeset -i rc=$RC_UNKNOWN
    typeset dopt= Nopt=
    typeset -i i=0

    #================================================================
    : Assuming an object was specified, see if it is a known object
    #================================================================
    if [[ $name != *([[:space:]]) ]]; then
        CL=$LINENO KLIB_HACMP_is_known_method "$name"
        (( $? != RC_SUCCESS )) && rc=$RC_NOT_FOUND
    fi

    #=================
    : Validate input
    #=================
    if [[ -z $name ]]; then
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 100 "\nERROR: a name/label must be provided.\n\n" 1>&2
        rc=$RC_MISSING_INPUT

    elif (( $rc == RC_NOT_FOUND )); then
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 102 "\nERROR: \"%1\$s\" does not appear to exist!\n\n" "$name" 1>&2
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 149 "Available Methods:\n\n" 1>&2

        typeset available
        CL=$LINENO KLIB_HACMP_list_methods available
        for (( i=0; i<${#available[*]}; i++ )); do
            if [[ ${available[$i]} != *([[:space:]]) ]]; then
                print -u2 "\t${available[$i]}"
            fi
        done
        print -u2 ""
    fi

    if [[ -n $new_name && -n "${new_name//[a-zA-Z0-9_]/}" ]]; then
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 105 "\nERROR: one or more invalid characters were detected in \"%1\$s\" (\"%2\$s\").\n\nValid characters include letters, numbers, and underscores only.\n\n" "$new_name" "${new_name//[a-zA-Z0-9_]/}" 1>&2
        rc=$RC_INCORRECT_INPUT
    fi

    if [[ -n $type && $type != @(no|sn|ve)* ]]; then
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 110 "\nERROR: invalid value specified for \"%1\$s\":  \"%2\$s\".\n" TYPE "$type" 1>&2
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 3 "Valid values: %1\$s\n\n" "notify, snapshot, verify" 1>&2
        rc=$RC_INCORRECT_INPUT
    fi

    if [[ -n $new_file && $new_file == *([[:space:]]) ]]; then
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 101 "\nERROR: this operation requires the \"%1\$s\" attribute.\n\n" FILE 1>&2
        rc=$RC_MISSING_INPUT
    fi

    if [[ -n $new_file && ! -e $new_file ]]; then
        CL=$LINENO cl_dspmsg -s $CLMGR_SET $CLMGR_MSGS 107 '\nERROR: the specified path/file does not appear to exist on "%2$s": %1$s\n\n' "$new_file" "$LOCAL_NODE" 1>&2
        rc=$RC_NOT_FOUND
    fi

   #
   : If nodes were specified, verify that they are known nodes.
   #
   if [[ $nodes != *([[:space:]]) ]]; then
        typeset checked=
        typeset -i all_nodes_found=1
        for node in ${nodes//,/ }; do
            [[ " $checked " == *\ $node\ * ]] && continue

            checked="$checked $node"
            CL=$LINENO KLIB_HACMP_is_known_node "$node" >/dev/null 2>&1
            if (( $? != RC_SUCCESS )); then
                rc=$RC_INCORRECT_INPUT
                all_nodes_found=0
                /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 102 "\nERROR: \"%1\$s\" does not appear to exist!\n\n" "$node" 1>&2
            fi
        done

        if (( ! all_nodes_found )); then
            /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 151 "Available Nodes:\n\n" 1>&2
            typeset available
            CL=$LINENO KLIB_HACMP_list_nodes available
            for (( i=0; i<${#available[*]}; i++ )); do
                if [[ ${available[$i]} != *([[:space:]]) ]]; then
                    print -u2 "\t${available[$i]}"
                fi
            done
            print -u2 ""
        fi
    fi

    if [[ -z "$new_name$new_desc$new_file$contact$events$nodes$retry$timeout" ]]; then
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 401 "\nERROR: no valid modifications were specified for \"%1\$s\".\n\n" "$name" 1>&2
        rc=$RC_MISSING_INPUT
    fi

    #==========================================================
    : Modify the method if no input errors have been detected
    #==========================================================
    if (( $rc == RC_UNKNOWN )); then

        if [[ -n $type ]]; then
            CL=$LINENO KLIB_HACMP_is_known_method "$name"
        else
            CL=$LINENO KLIB_HACMP_is_known_method "$name" type
        fi

        if [[ $type == *,* ]]; then
            /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 34 "\nERROR: \"%1\$s\", is ambiguous, and could match any of the following:\n       %2\$s\n\n" "$method" "$type" 1>&2
            rc=$RC_INCORRECT_INPUT

        else
            typeset CMD=
            CL=$LINENO KLIB_HACMP_get_method_attributes "$name" properties TYPE=$type
            case "$type" in
                no*) [[ -z $new_desc ]] && new_desc="${properties[DESCRIPTION]}"
                     [[ -z $new_file ]] && new_file="${properties[FILE]}"
                     [[ -z $contact  ]] && contact="${properties[CONTACT]}"
                     if [[ -z $events ]]; then
                         events="${properties[EVENTS]}"
                     else
                         events=${events//,/ }
                     fi
                     [[ -z $nodes    ]] && nodes="${properties[NODES]}"
                     [[ -z $retry    ]] && retry="${properties[RETRY]}"
                     [[ -z $timeout  ]] && timeout="${properties[TIMEOUT]}"
                     CMD="clchpgr"
                     if [[ -n $new_name ]]; then
                         print "$0()[$LINENO]($SECONDS): $HAUTILS/clchpgr -m \"$name\" -c \"$new_name\" -d \"$new_desc\" -f \"$new_file\" -N \"$contact\" -e \"$events\" -n \"$nodes\" -r \"$retry\" -t \"$timeout\"" >>$CLMGR_TMPLOG  # Always log commands
                         $HAUTILS/clchpgr -m "$name" \
                                          -c "$new_name" \
                                          -d "$new_desc" \
                                          -f "$new_file" \
                                          -N "$contact" \
                                          -e "$events" \
                                          -n "$nodes" \
                                          -r "$retry" \
                                          -t "$timeout"
                     else
                         print "$0()[$LINENO]($SECONDS): $HAUTILS/clchpgr -m \"$name\" -d \"$new_desc\" -f \"$new_file\" -N \"$contact\" -e \"$events\" -n \"$nodes\" -r \"$retry\" -t \"$timeout\"" >>$CLMGR_TMPLOG  # Always log commands
                         $HAUTILS/clchpgr -m "$name" \
                                          -d "$new_desc" \
                                          -f "$new_file" \
                                          -N "$contact" \
                                          -e "$events" \
                                          -n "$nodes" \
                                          -r "$retry" \
                                          -t "$timeout"
                     fi
                ;;
                sn*) [[ -z $new_desc ]] && new_desc="${properties[DESCRIPTION]}"
                     [[ -z $new_file ]] && new_file="${properties[FILE]}"
                     CMD="clchcustom"
                     if [[ -n $new_name ]]; then
                         print "$0()[$LINENO]($SECONDS): $HAUTILS/clchcustom -t snapshot -n\"$name\" -N\"$new_name\" -I\"$new_desc\" -v\"$new_file\"" >>$CLMGR_TMPLOG  # Always log commands
                         $HAUTILS/clchcustom -t snapshot   \
                                             -n"$name"     \
                                             -N"$new_name" \
                                             -I"$new_desc" \
                                             -v"$new_file"
                     else
                         print "$0()[$LINENO]($SECONDS): $HAUTILS/clchcustom -t snapshot -n\"$name\" -I\"$new_desc\" -v\"$new_file\"" >>$CLMGR_TMPLOG  # Always log commands
                         $HAUTILS/clchcustom -t snapshot   \
                                             -n"$name"     \
                                             -I"$new_desc" \
                                             -v"$new_file"
                     fi
                ;;
                ve*) [[ -z $new_desc ]] && new_desc="${properties[DESCRIPTION]}"
                     [[ -z $new_file ]] && new_file="${properties[FILE]}"
                     CMD="clchcustom"
                     if [[ -n $new_name ]]; then
                         print "$0()[$LINENO]($SECONDS): $HAUTILS/clchcustom -t verify -n\"$name\" -N\"$new_name\" -I\"$new_desc\" -v\"$new_file\"" >>$CLMGR_TMPLOG  # Always log commands
                         $HAUTILS/clchcustom -t verify     \
                                             -n"$name"     \
                                             -N"$new_name" \
                                             -I"$new_desc" \
                                             -v"$new_file"
                     else
                         print "$0()[$LINENO]($SECONDS): $HAUTILS/clchcustom -t verify -n\"$name\" -I\"$new_desc\" -v\"$new_file\"" >>$CLMGR_TMPLOG  # Always log commands
                         $HAUTILS/clchcustom -t verify     \
                                             -n"$name"     \
                                             -I"$new_desc" \
                                             -v"$new_file"
                     fi
                ;;
            esac
            rc=$?
            print "$CMD RC: $rc" >>$CLMGR_TMPLOG  # Always log command result

            if (( $rc != RC_SUCCESS )); then
                /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 400 "\nERROR: failed to modify \"%1\$s\".\n\n" "$name" 1>&2
                rc=$RC_ERROR

            #===========================================================
            : If output from this operation was requested, retrieve it
            #===========================================================
            else
                [[ -n $new_name ]] && name=$new_name
                if (( CLMGR_VERBOSE )) || [[ -n $CLMGR_ATTRS ]]; then
                    CL=$LINENO KLIB_HACMP_get_method_attributes "$name" properties
                fi
            fi
        fi
    fi

    #=======================================================================
    : If a user input error was detected, provide some helpful suggestions
    #=======================================================================
    if (( $rc == RC_MISSING_INPUT || $rc == RC_INCORRECT_INPUT )) && \
       [[ $CLMGR_GUI == *([[:space:]]) ]]
    then
        CL=$LINENO cl_dspmsg -s $CLMGR_SET $CLMGR_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" \
        "modify method -h" "METHOD:" "$CLMGR_PROGNAME" 1>&2
    fi

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