#!/bin/ksh93
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/lib/ksh93/hacmp/KLIB_HACMP_replace_mirror_group.sh 1.8 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2012,2013 
# 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 
# @(#)53	1.8  src/43haes/lib/ksh93/hacmp/KLIB_HACMP_replace_mirror_group.sh, hacmp.assist, 61haes_r714 5/15/13 17:23:58

#================================================
# 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>
#      pod2ht-ml <FILENAME>
function devDoc {
    : <<'=cut' >/dev/null 2>&1

=head1 NAME

 KLIB_HACMP_replace_mirror_group

=head1 VERSION

 Version Number:  1.8
 Last Extracted:  1/31/14 04:42:06
 Last Changed:    5/15/13 17:23:58

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

=head1 SYNOPSIS

 clmgr swap mirror_group <mirror_group_name>[,<mg#2>,...] \
             [ NODE=<node_name> ]
 clmgr swap mirror_group \
             NODES=<node_name>[,<node#2>,...] \
             [ SYSTEM_GROUPS={yes|no} ]
 clmgr swap mirror_group \
             SITE=<site_name>[,<site#2>] \
             [ SYSTEM_GROUPS={yes|no} ]
             [ REPOSITORY_GROUP={yes|no} ]

 NOTE: the "swap" action is only valid for DS-Series
       Inband (HyperSwap)
 NOTE: the alias for "mirror_group" is "mig".

=head1 DESCRIPTION

 Attempts to swap the specified HyperSwap mirror groups.

=head1 ARGUMENTS

 1. mirror_groups [OPTIONAL] [string]
    One or more HyperSwap mirror groups to swap.

 2. nodes [OPTIONAL] [string]
    The label of one or more cluster nodes.

 3. sites [OPTIONAL] [string]
    The label of one or more cluster sites.

 4. system_groups [OPTIONAL] [boolean]
    Indicates whether or not to include system mirror groups
    in the swap. Defaults to "yes".

 5. repository_group [OPTIONAL] [boolean]
    Indicates whether or not to include the repository mirror
    group in the swap. Defaults to "yes".

=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.


##############################################################################
#
# Name:        display_active_path_info
#
# Description: Formats the output from the "activepath" operation to
#              make it resembel the default clgr output format.
#
# Inputs:      FILE   A file containing the active path output
#
# Outputs:     The formatted output is displayed on STDOUT.
#
# Returns:     n/a
#
##############################################################################
function display_active_path_info {
    typeset FILE=$1

    cat $FILE |\
    while read LINE; do
        if (( ++COUNT % 2 )); then
            ATTRS=${LINE#*:}
            ATTRS="NODE:${ATTRS# }"
        else
            for VALUE in ${LINE//:/ }; do
                ATTRS=${ATTRS/:/=$VALUE$NL}
            done
            ATTRS="${ATTRS}=$VALUE"
            (( COUNT > 2 )) && print
            print -- "$ATTRS"
        fi
    done

    /usr/bin/rm -f $FILE
} # End of "display_active_path_info()"


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

    typeset mirror_groups=${1//\"/}
            mirror_groups=${mirror_groups//,/ }
    typeset nodes=${2//\"/}
            nodes=${nodes//,/ }
    typeset sites=${3//\"/}
            sites=${sites//,/ }
    typeset -l system_groups=${4//\"/}
    typeset -l repository_group=${5//\"/}

    typeset OPERATION="$OPERATION"
    [[ -z $OPERATION ]] && OPERATION="swap"

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

    #===================================
    : Declare and initialize variables
    #===================================
    typeset -i rc=$RC_UNKNOWN cmd_rc=$RC_UNKNOWN
    typeset -i i=0 COUNT=0
    typeset mirror_group="" node="" LINE="" ATTRS="" VALUE=""

    #================================================================
    : 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

    #=================
    : Validate input
    #=================
    if [[ $mirror_groups != *([[:space:]]) ]]; then
        for mirror_group in $mirror_groups; do
            typeset TYPE=
            CL=$LINENO KLIB_HACMP_is_known_mirror_group $mirror_group TYPE
            if (( $? != RC_SUCCESS )); then
                rc=$RC_NOT_FOUND
                /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 102 "\nERROR: \"%1\$s\" does not appear to exist!\n\n" "$mirror_group" 1>&2
                /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 167 "Available Mirror Groups:\n\n" 1>&2

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

            elif [[ $TYPE != ds8k_inband_mm ]]; then
                rc=$RC_INCORRECT_INPUT
                /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 9999 "\nERROR: \"%1\$s\" is not a DS-Series Inband (HyperSwap) mirror group!\n\n" "$mirror_group" 1>&2
                /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 151 "Available Mirror Groups:\n\n" 1>&2

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

            fi
        done
    fi

    if [[ $nodes != *([[:space:]]) ]]; then
        for node in $nodes; do
            CL=$LINENO KLIB_HACMP_is_known_node $node >/dev/null
            if (( $? != RC_SUCCESS )); then
                rc=$RC_NOT_FOUND
                /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 102 "\nERROR: \"%1\$s\" does not appear to exist!\n\n" "$node" 1>&2
                /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
        done
    fi

    if [[ $sites != *([[:space:]]) ]]; then
        typeset site=""
        typeset -i ERR=0
        for site in $sites; do
            CL=$LINENO KLIB_HACMP_is_known_site $site >/dev/null
            if (( $? != RC_SUCCESS )); then
                rc=$RC_NOT_FOUND
                ERR=1
                /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 102 "\nERROR: \"%1\$s\" does not appear to exist!\n\n" "$site" 1>&2
            fi
        done

        if (( ERR )); then
            /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 157 "Available Sites:\n\n" 1>&2

            typeset available
            CL=$LINENO KLIB_HACMP_list_sites 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 [[ -n $system_groups && $system_groups != @(y|t|n|f)* ]]; then
        /usr/bin/dspmsg -s $CLVT_SET $CLVT_MSGS 110 "\nERROR: invalid value specified for \"%1\$s\":  \"%2\$s\".\n" SYSTEM_GROUPS "$system_groups" 1>&2
        /usr/bin/dspmsg -s $CLVT_SET $CLVT_MSGS 3 "Valid values: %1\$s\n\n" "yes, no" 1>&2
        rc=$RC_INCORRECT_INPUT
    else
        case $system_groups in
            @(y|t)*) system_groups="yes" ;;
            @(n|f)*) system_groups="no"  ;;
        esac
    fi

    if [[ -n $repository_group && $repository_group != @(y|t|n|f)* ]]; then
        /usr/bin/dspmsg -s $CLVT_SET $CLVT_MSGS 110 "\nERROR: invalid value specified for \"%1\$s\":  \"%2\$s\".\n" REPOSITORY_GROUP "$repository_group" 1>&2
        /usr/bin/dspmsg -s $CLVT_SET $CLVT_MSGS 3 "Valid values: %1\$s\n\n" "yes, no" 1>&2
        rc=$RC_INCORRECT_INPUT
    else
        case $repository_group in
            @(y|t)*) repository_group="yes" ;;
            @(n|f)*) repository_group="no"  ;;
        esac
    fi

    #=====================================================================
    : Perform the operation, $OPERATION, if no errors have been detected
    #=====================================================================
    if (( $rc == RC_UNKNOWN || $rc == RC_SUCCESS )); then
        typeset XD_CLI="/usr/es/sbin/cluster/xd_generic/xd_cli"

        if [[ $mirror_groups != *([[:space:]]) ]]; then
            for mirror_group in $mirror_groups; do
                typeset -A mgAttrs
                CL=$LINENO KLIB_HACMP_get_mirror_group_attributes "$mirror_group" mgAttrs
                if [[ ${mgAttrs[MG_TYPE]} == "system" ]]; then
                    typeset node_list="$nodes"
                    node_list=${node_list/${mgAttrs[SYS_MG_NODE]}/}
                    [[ -n $node_list ]] && node_list=" $node_list"
                    node_list="${mgAttrs[SYS_MG_NODE]}$node_list"

                    : Swapping SYSTEM mirror group $mirror_groups via node $node_list
                    print -- "$0()[$LINENO]($SECONDS): $XD_CLI/cl_clxd_manage_mg_smit -t sys -m \"$mirror_group\" -n \"$node_list\" -o $OPERATION" >>$CLMGR_TMPLOG
                    $XD_CLI/cl_clxd_manage_mg_smit -t sys -m "$mirror_group" -n "$node_list" -o $OPERATION >$TMPDIR/clmgr.KHrmg.op.$$
                    cmd_rc=$?
                    (( rc == RC_UNKNOWN || rc == RC_SUCCESS )) && rc=$cmd_rc
                    unset node_list

                elif [[ -n ${mgAttrs[MG_TYPE]} ]]; then
                    : Swapping ${mgAttrs[MG_TYPE]} mirror group $mirror_group
                    print -- "$0()[$LINENO]($SECONDS): $XD_CLI/cl_clxd_manage_mg_smit -t ${mgAttrs[MG_TYPE]} -m \"$mirror_group\" -o $OPERATION" >>$CLMGR_TMPLOG
                    $XD_CLI/cl_clxd_manage_mg_smit -t ${mgAttrs[MG_TYPE]} -m "$mirror_group" -o $OPERATION >$TMPDIR/clmgr.KHrmg.op.$$
                    cmd_rc=$?
                    (( rc == RC_UNKNOWN || rc == RC_SUCCESS )) && rc=$cmd_rc
                    print -- "$0()[$LINENO]($SECONDS): cl_clxd_manage_mg_smit RC: $rc" >>$CLMGR_TMPLOG

                else
                    print -u2 "
Usage error:

 clmgr $OPERATION mirror_group <mirror_group_name>[,<mg#2>,...] \\
             [ NODE=<node_name> ]
 clmgr $OPERATION mirror_group \\
             NODES=<node_name>[,<node#2>,...] \\
             [ SYSTEM_GROUPS={yes|no} ]
 clmgr $OPERATION mirror_group \\
             SITE=<site_name>[,<site#2>] \\
             [ SYSTEM_GROUPS={yes|no} ] \\
             [ REPOSITORY_GROUP={yes|no} ]
"
                    rc=$RC_MISSING_INPUT
                fi

                if [[ $OPERATION == "activepath" ]]; then
                    display_active_path_info $TMPDIR/clmgr.KHrmg.op.$$
                else
                    cat $TMPDIR/clmgr.KHrmg.op.$$
                    /usr/bin/rm -f  $TMPDIR/clmgr.KHrmg.op.$$
                fi
            done # End of the mirror groups loop

        elif [[ -n $nodes ]]; then
            : Swapping mirror groups by nodes ${nodes// /, }
            [[ -z $system_groups ]] && system_groups="yes"

            print -- "$0()[$LINENO]($SECONDS): $XD_CLI/cl_clxd_manage_mg_smit -t node -n \"$nodes\" -s $system_groups -o $OPERATION" >>$CLMGR_TMPLOG
            $XD_CLI/cl_clxd_manage_mg_smit -t node -n "$nodes" -s $system_groups -o $OPERATION >$TMPDIR/clmgr.KHrmg.op.$$
            cmd_rc=$?
            (( rc == RC_UNKNOWN || rc == RC_SUCCESS )) && rc=$cmd_rc

            if [[ $OPERATION == "activepath" ]]; then
                display_active_path_info $TMPDIR/clmgr.KHrmg.op.$$
            else
                cat $TMPDIR/clmgr.KHrmg.op.$$
                /usr/bin/rm -f  $TMPDIR/clmgr.KHrmg.op.$$
            fi

        elif [[ -n $sites ]]; then
            : Swapping mirror groups by sites ${sites// /, }
            [[ -z $system_groups ]] && system_groups="yes"
            [[ -z $repository_group ]] && repository_group="yes"

            print -- "$0()[$LINENO]($SECONDS): $XD_CLI/cl_clxd_manage_mg_smit -t site -i \"$sites\" -s $system_groups -r $repository_group -o $OPERATION" >>$CLMGR_TMPLOG
            $XD_CLI/cl_clxd_manage_mg_smit -t site -i "$sites" -s $system_groups -r $repository_group -o $OPERATION >$TMPDIR/clmgr.KHrmg.op.$$
            cmd_rc=$?
            (( rc == RC_UNKNOWN || rc == RC_SUCCESS )) && rc=$cmd_rc

            if [[ $OPERATION == "activepath" ]]; then
                display_active_path_info $TMPDIR/clmgr.KHrmg.op.$$
            else
                cat $TMPDIR/clmgr.KHrmg.op.$$
                /usr/bin/rm -f  $TMPDIR/clmgr.KHrmg.op.$$
            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
        typeset ACTION="swap mirror_group"
        [[ $OPERATION == "activepath" ]] && ACTION="view mirror_group"
        [[ $OPERATION == "refresh"    ]] && ACTION="manage mirror_group refresh"

        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" \
        "$ACTION -h" "MIRROR GROUP:" "$CLMGR_PROGNAME" 1>&2
    fi

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