#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG                                                    
#  This is an automatically generated prolog.                                  
#                                                                              
#  Copyright (C) Altran ACT S.A.S. 2018,2019,2020,2021.  All rights reserved.  
#                                                                              
#  ALTRAN_PROLOG_END_TAG                                                      
#                                                                              
# @(#)  5f56dbd 43haes/lib/ksh93/hacmp/KLIB_HACMP_delete_backup_profile.sh, 726, 2147A_aha726, Mar 08 2021 04:44 PM

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

=head1 SYNOPSIS

 clmgr delete backup_profile {<resource_group>[,<resource_group#2>,...] | ALL} \
                                [ CANCEL_BACKUP={no|yes} ]

 backup_profile => bp, backup_p, replication_profile

=head1 DESCRIPTION

 Attempts to delete backup profile configuration and backup data for specified resource group or All resource groups.

=head1 ARGUMENTS

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

 2. resource_group [REQUIRED] [string]
    Resource group name can be single or multiple resource groups
    All resource group can be deleted if 'All' is specified.

 3. CANCEL_BACKUP [OPTIONAL] [string]
    Cancels running backup process.
    Default value no.

=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 POD-formatted documentation.

#============================================================================
#
# Name:        KLIB_HACMP_delete_backup_profile
#
# Description: This is the main, FPATH function that is invoked by clmgr
#              to delete backup configuration. The cl_cbm_list utility is
#              used to query backup configuraton and cl_cbm_delete utility
#              is invoked to delete data from xml file.
#
# Inputs:      See the "devDoc()" function at the top of this file.
#
# Outputs:     The properties hash is populated. The only other outputs are
#              any error messages that might be needed.
#
# Returns:     Zero if no errors are detected. Otherwise, an appropriate
#              non-zero value is returned. Refer to the "RETURN" section
#              of the "devDoc()" function at the top of this file for
#              the standard return code values/meanings for clmgr.
#
#============================================================================
function KLIB_HACMP_delete_backup_profile {
    . $HALIBROOT/log_entry "$0()" "$CL"
    : version=@(#)  5f56dbd 43haes/lib/ksh93/hacmp/KLIB_HACMP_delete_backup_profile.sh, 726, 2147A_aha726, Mar 08 2021 04:44 PM
    : INPUTS: $*

    typeset rgs=${1//\"}
    rgs=${rgs//,/ }
    typeset cancel_backup=${2//\"}
    [[ -z $TAB ]] && TAB=$'\t'

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

    #===================================
    : Declare and initialize variables
    #===================================
    typeset -i rc=$RC_SUCCESS
    typeset rg= available= output= unknown_rg=
    typeset -l lc_rg=$rgs
    typeset nodelist=""

    #====================================
    : Check python is installed or not
    #====================================
    LANG=C cl_get_python_version 2>&1 >>/dev/null
    rc=$?
    if (( $rc != 0 )); then
        cl_dspmsg -s $CLMGR_SET $CLMGR_MSGS 1315 "\nERROR: Python must be installed for using backup feature.\n\n" 1>&2
        log_return_msg "$rc" "$0()" "$LINENO"
        return $rc
    fi

    #================================================================
    : Check for a defined cluster. No need to continue without one.
    #================================================================
    CL=$LINENO isClusterDefined
    if (( $? != RC_SUCCESS )); then
        rc=$RC_MISSING_DEPENDENCY
    else
        #=================
        : Validate input
        #=================
        if [[ -z $rgs ]]; then
            cl_dspmsg -s $CLMGR_SET $CLMGR_MSGS 100 "\nERROR: a name/label must be provided.\n\n" 1>&2
            rc=$RC_MISSING_INPUT
        else
            print -- "$0()[$LINENO]($SECONDS): cl_cbm_list" >>$CLMGR_TMPLOG # Always log command result
            available=$(LANG=C cl_cbm_list 2>>$CLMGR_TMPLOG)
            rc=$?
            print -- "$0()[$LINENO]($SECONDS): cl_cbm_list RC:$rc" >>$CLMGR_TMPLOG # Always log command result
            if [[ $lc_rg != "all" ]]
            then
                for rg in $rgs
                do
                    echo "$available" | grep -wq $rg || unknown_rg=${unknown_rg:+$unknown_rg,}$rg
                done
                if [[ -n $unknown_rg ]]
                then
                    available=$(echo "$available" | sed "s/^/$TAB/g")
                    cl_dspmsg -s $CLMGR_SET $CLMGR_MSGS 1191 "\nERROR: Resource group \"%1\$s\" not configured for backup.\n\n" "$unknown_rg" 1>&2
                    cl_dspmsg -s $CLMGR_SET $CLMGR_MSGS 1192 "Configured resource groups for backup:\n\n" 1>&2
                    print -u2 "$available"
                    print -u2 ""
                    rc=$RC_NOT_FOUND
                fi
            else
                # Delete all configured RGs
                rgs=""
                for rg in $available
                do
                    rgs="${rgs:+$rgs }$rg"
                done
                # If no RG to delete, consider it is ok
                if [[ -z $rgs ]]; then
                    cl_dspmsg -s $CLMGR_SET $CLMGR_MSGS 1197 "\nERROR: No resource group configured for backup.\n\n" 1>&2
                fi
                rc=$RC_SUCCESS
            fi
        fi

        # Validate cancel_backup value.
        if [[ -n $cancel_backup ]]
        then
            CL=$LINENO verify_in_set CANCEL_BACKUP "$cancel_backup" "yes no" cancel_backup
            if (( $? != RC_SUCCESS ));then
                rc=$RC_INCORRECT_INPUT
            fi
        else
            cancel_backup="no"
        fi

        if (( $rc == RC_SUCCESS )) ; then
            for rg in $rgs; do
                if [[ $cancel_backup == "yes" ]]
                then
                    #Even if cancellations fails log error and continue with delete
                    print -- "$0()[$LINENO]($SECONDS): cl_cbm_cancel_backup $rg" >>$CLMGR_TMPLOG # Always log command result
                    LANG=C cl_cbm_cancel_backup "$rg" 2>>$CLMGR_TMPLOG
                    rc=$?
                    print -- "$0()[$LINENO]($SECONDS): cl_cbm_cancel_backup $rg RC:$rc" >>$CLMGR_TMPLOG # Always log command result
                fi
                print -- "$0()[$LINENO]($SECONDS): cl_cbm_delete $rg" >>$CLMGR_TMPLOG # Always log command result
                output=$(LANG=C cl_cbm_delete "$rg" 2>>$CLMGR_TMPLOG)
                rc=$?
                print -- "$0()[$LINENO]($SECONDS): cl_cbm_delete $rg RC:$rc" >>$CLMGR_TMPLOG # Always log command result
                # If rootvg_profile is removed from backup configuration, then coresponding timer should be removed
                if [[ $rg == "rootvg_profile" ]]
                then
                    cl_cbm_rootvg_timer -r
                    odmdelete -o HACMPtimersvc -q "name = cbm_rootvg_timer" >>/dev/null
                fi
                if (( $rc != RC_SUCCESS ));then
                    print -- "$0()[$LINENO]($SECONDS): cl_cbm_delete $rg RC:$rc \noutput=$output" >>$CLMGR_TMPLOG # Always log command result
                    cl_dspmsg -s $CLMGR_SET $CLMGR_MSGS 1198 "\nERROR: failed to remove backup configuration for resource group \"%1\$s\".\n\n" "$rg" 1>&2
                    rc=$RC_ERROR
                    break
                else
                    # if backup profile got deleted then remove the corresponding HACMPresource entry
                    print -- "$0()[$LINENO]($SECONDS): odmdelete -o  HACMPresource -q "group=$rg AND name=BACKUP_ENABLED" " >>$CLMGR_TMPLOG
                    odmdelete -o  HACMPresource -q "group=$rg AND name=BACKUP_ENABLED" >>/dev/null
                    print -- "$0()[$LINENO]($SECONDS): odmdelete -o  HACMPresource -q "group=$rg AND name=BACKUP_ENABLED" RC:$?" >>$CLMGR_TMPLOG
                fi
            done
        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_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 backup_profile -h" "$CLVT_PROGNAME" 1>&2
    fi

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

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