#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG
#  This is an automatically generated prolog.
#
#  Copyright (C) Altran ACT S.A.S. 2019,2021.  All rights reserved.
#
#  ALTRAN_PROLOG_END_TAG

# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/lib/ksh93/hacmp/KLIB_HACMP_delete_snapshot.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 
# @(#)  7d4c34b 43haes/lib/ksh93/hacmp/KLIB_HACMP_delete_snapshot.sh, 726, 2147A_aha726, Feb 05 2021 09:50 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_snapshot

=head1 SYNOPSIS

clmgr delete snapshot {<snapshot>[,<snapshot#2>,<snapshot#n>,...] | ALL}

=head1 DESCRIPTION

Deletes one or more snapshots from the local system.

=head1 ARGUMENTS

 1. snapshot [REQUIRED] [string]
    One or more (comma or space separated) snapshots to attempt to delete.

=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_snapshot
#
# Description: This is the main, FPATH function that is invoked by clmgr
#              to delete a cluster snapshot.
#
# Inputs:      See the "devDoc()" function, above.
#
# Outputs:     None.
#
# Returns:     Zero if no errors are detected. Otherwise, an appropriate
#              non-zero value is returned. Refer to the "RETURN" section
#              of the "devDoc()" function, above, for the standard return
#              code values/meanings for clmgr.
#
#============================================================================
function KLIB_HACMP_delete_snapshot {
    . $HALIBROOT/log_entry "$0()" "$CL"
    : version=@(#)  7d4c34b 43haes/lib/ksh93/hacmp/KLIB_HACMP_delete_snapshot.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
    : INPUTS: $*

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

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

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

    #=================
    : Validate input
    #=================
    if [[ -z $snapshots ]]; then
        dspmsg -s $CLVT_SET $CLVT_MSGS 100 "\nERROR: a name/label must be provided.\n\n" 1>&2

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

        rc=$RC_MISSING_INPUT

    else
        typeset -u uctext=$snapshots
        if [[ $uctext == "ALL" ]]; then
            snapshots=
            typeset available
            CL=$LINENO KLIB_HACMP_list_snapshots available
            for (( i=0; i<${#available[*]}; i++ )); do
                snapshots="$snapshots ${available[$i]}"
            done

            if [[ $snapshots == *([[:space:]]) ]]; then
                dspmsg -s $CLVT_SET $CLVT_MSGS 333 "Warning: there are no snapshots to delete.\n"
                rc=$RC_SUCCESS
            fi
        fi
    fi

    #============================================================
    : Delete the snapshots if no input errors have been detected
    #============================================================
    integer PROBLEMS=0
    if (( $rc == RC_UNKNOWN )); then
        # loop through the list of snapshots - since this is a loop we
        # dont preserve specific errors (e.g. not found or failed to delete)
        # since rc could be overwritten in the next loop iteration - instead
        # we simply record that there were problems and throw RC_ERROR later
        for snapshot in $snapshots; do

            CL=$LINENO KLIB_HACMP_is_known_snapshot "$snapshot"
            if (( $? != RC_SUCCESS )); then
                dspmsg -s $CLVT_SET $CLVT_MSGS 102 "\nERROR: \"%1\$s\" does not appear to exist!\n\n" "$snapshot" 1>&2

                (( PROBLEMS++ ))

            else
                print -- "$0()[$LINENO]($SECONDS): clsnapshot -r -n \"$snapshot\"" >>$CLMGR_TMPLOG  # Always log commands
                clsnapshot -r -n "$snapshot"
                rc=$?
                if (( $rc != RC_SUCCESS )); then
                    dspmsg -s $CLVT_SET $CLVT_MSGS 300 "\nERROR: failed to delete \"%1\$s\".\n\n" "$snapshot" 1>&2
                    (( PROBLEMS++ ))
                fi
            fi
        done

        if (( PROBLEMS )) 
        then
            # add an extra space after error messages
            print -u2

            # if there were problems, list the available snapshots
            dspmsg -s $CLVT_SET $CLVT_MSGS 158 "Available Snapshots:\n\n" 1>&2
            typeset available
            CL=$LINENO KLIB_HACMP_list_snapshots available
            for (( i=0; i<${#available[*]}; i++ )); do
                if [[ ${available[$i]} != *([[:space:]]) ]]; then
                    print -u2 "\t${available[$i]}"
                fi
            done
            rc=$RC_ERROR
        fi
    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_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 snapshot -h" "SNAPSHOT:" "$CLVT_PROGNAME" 1>&2
    fi

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