#!/bin/ksh93
# @(#)  7d4c34b 43haes/lib/ksh93/hacmp/KLIB_HACMP_cluster_rehearsals.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
#  ALTRAN_PROLOG_BEGIN_TAG                                                    
#  This is an automatically generated prolog.                                  
#                                                                              
#  Copyright (C) Altran ACT S.A.S. 2017,2021.  All rights reserved.  
#                                                                              
#  ALTRAN_PROLOG_END_TAG                                                      
#                                                                              

#=====================================================
# The following, commented line enforces coding
# standards when this file is edited via emacs or 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_cluster_rehearsals

=head1 SYNOPSIS

 clmgr manage cluster rehearsals RESOURCE_GROUP="<resource_group>[,<resource_group#2>,<resource_group#n>,...]"
 NOTE: The alias for "rehearsals" is "reh".

=head1 DESCRIPTION

 Retrieves the Failover Rehearsals for resource group.

=head1 ARGUMENTS

 1. resource_group_name [REQUIRED] [string]
    Get failover rehearsals of Target storages and mirroring of resource group.

=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

=cut
} # End of "devDoc()"

function KLIB_HACMP_cluster_rehearsals {
    LINENO=2 . $HALIBROOT/log_entry "$0()" "$CL"
    : INPUTS: $*

    typeset rgs=${1//\"/}
    rgs=${rgs//,/ }

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

    #===================================
    : Declare and initialize variables
    #===================================
    typeset -i rc=$RC_SUCCESS

    #================================================================
    : 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 $?
    else
        #
        : Check the cluster type, return if not Linked Cluster
        #
        typeset -i cluster_type=$(clodmget -n -f multi_site_lc HACMPcluster)
        if (( $cluster_type != 1 ))
        then
            dspmsg -s $CLMGR_SET $CLMGR_MSGS 1163 "\nERROR: Failover rehearsals are supported in linked cluster only.\n\n" 1>&2
            log_return_msg "$RC_MISSING_DEPENDENCY" "$0()" "$LINENO"
            return $?
        fi
    fi

    #=================
    : Validate input
    #=================
    if [[ $rgs != *([[:space:]]) ]]; then
        for rg in $rgs; do
            CL=$LINENO KLIB_HACMP_is_known_rg "$rg" >/dev/null 2>&1
            if (( $? != RC_SUCCESS )); then
                dspmsg -s $CLVT_SET $CLVT_MSGS 102 '\nERROR: "%1$s" does not appear to exist!\n\n' "$rg" 1>&2
                rc=$RC_NOT_FOUND
            fi
        done
        if (( $rc == RC_NOT_FOUND )); then
            dspmsg -s $CLVT_SET $CLVT_MSGS 154 "Available Resource Groups:\n\n" 1>&2
            typeset available
            CL=$LINENO KLIB_HACMP_list_resourcegroups available
            for (( i=0; i<${#available[*]}; i++ )); do
                if [[ ${available[$i]} != *([[:space:]]) ]]; then
                    print -u2 "\t${available[$i]}"
                fi
            done
            print -u2 ""
        fi
    else
        dspmsg -s $CLMGR_SET $CLMGR_MSGS 200 "\nERROR: missing required argument:  %1\$s\n" RESOURCE_GROUP 1>&2
        rc=$RC_MISSING_INPUT
    fi

    if (( $rc == RC_SUCCESS ))
    then
        print -- "$0()[$LINENO]($SECONDS): cl_get_rehearsals -r \"$rgs\"" >>$CLMGR_TMPLOG
        cl_get_rehearsals -r "$rgs"
        rc=$?
        print -- "$0()[$LINENO]($SECONDS): cl_get_rehearsals -r \"$rgs\" rc=$rc" >>$CLMGR_TMPLOG # Always log command result
    fi

    #=======================================================================
    : If a user input error was detected, provide some helpful suggestions
    #=======================================================================
    if (( $rc == RC_MISSING_INPUT )) && [[ $CLMGR_GUI == *([[:space:]]) ]]
    then
        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" \
        "manage cluster -h" "CLUSTER:" "$CLMGR_PROGNAME" 1>&2
    fi

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