#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG
#  This is an automatically generated prolog.
#
#  Copyright (C) Altran ACT S.A.S. 2017,2018,2019,2020,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_modify_snapshot.sh 1.5 
#  
# 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 
# @(#)  c93cc36 43haes/lib/ksh93/hacmp/KLIB_HACMP_modify_snapshot.sh, 726, 2147A_aha726, Feb 25 2021 03:14 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_modify_snapshot

=head1 SYNOPSIS

 clmgr modify snapshot <snapshot> \
             [ NAME="<new_snapshot_name>" ] \
             [ DESCRIPTION="<new_snapshot_description>" ]

 NOTE: the alias for "snapshot" is "sn".

=head1 DESCRIPTION

Attempts to modify the specified PowerHA snapshot
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. snapshot [REQUIRED] [string]
    The name of the snapshot that is to be modified.

 3. new_name [OPTIONAL] [string]
    A new name to use for the snapshot. Note that the .odm suffix of 
    the snapshot file itself is preserved.

 4. new_desc [OPTIONAL] [string]
    A new description  to apply to the specified snapshot.
    Note that this option modifies the "description" section within
    the snapshot file itself but does not rename the snapshot.

=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_modify_snapshot
#
# Description: This is the main, FPATH function that is invoked by clmgr
#              to modify a snapshot of the cluster configuration. Since
#              a snapshot is primarily a backup of the SystemMirror ODMs,
#              the only things that can be modified are the names of the
#              snapshot files, and the description associated with them.
#
# Inputs:      refer to the "devDoc()" function, above.
#
# Outputs:     No explicit outputs, other than the normal clsnapshot
#              output, along with 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, above, for the standard return
#              code values/meanings for clmgr.
#
#============================================================================
function KLIB_HACMP_modify_snapshot {
    . $HALIBROOT/log_entry "$0()" "$CL"
    : version=@(#)  c93cc36 43haes/lib/ksh93/hacmp/KLIB_HACMP_modify_snapshot.sh, 726, 2147A_aha726, Feb 25 2021 03:14 PM
    : INPUTS: $*

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

    typeset new_name=${1//\"/}
    typeset new_desc=${2#\"}
            new_desc=${new_desc%\"}

    [[ $CLMGR_LOGGING == 'med' ]] && set +x  # Only trace param values
 
    #===================================
    : 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 [[ $snapshot != *([[:space:]]) ]]; then
        CL=$LINENO KLIB_HACMP_is_known_snapshot "$snapshot"
        (( $? != RC_SUCCESS )) && rc=$RC_NOT_FOUND
    fi

    #=================
    : Validate input
    #=================
    if [[ -z $snapshot ]]; then
        dspmsg -s $CLMGR_SET $CLMGR_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

    elif (( $rc == RC_NOT_FOUND )); then
        dspmsg -s $CLMGR_SET $CLMGR_MSGS 102 "\nERROR: \"%1\$s\" does not appear to exist!\n\n" "$snapshot" 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 ""
    
    elif [[ -z $new_name ]] && [[ " ${!_ENV_ARGS[*]} " == *\ NAME\ * ]]; then
        cl_dspmsg -s $CLMGR_SET $CLMGR_MSGS 1422 "\nERROR: Cannot modify name of a snapshot as an empty string. Please provide a valid input.\n"
        rc=$RC_INCORRECT_INPUT

    elif [[ "$*" == *([[:space:]]) && " ${!_ENV_ARGS[*]} " != *\ DESCRIPTION\ * ]]; then
        dspmsg -s $CLMGR_SET $CLMGR_MSGS 401 "\nERROR: no valid modifications were specified for \"%1\$s\".\n\n" "$snapshot" 1>&2
        rc=$RC_MISSING_INPUT
    fi

    #============================================================
    : Modify the snapshot if no input errors have been detected
    #============================================================
    if (( $rc == RC_UNKNOWN )); then
        : Provide escaping for some characters that are not safe in an eval
        new_desc=${new_desc//\$\(/\\\$\(}
        new_desc=${new_desc//\`/\\\`}
        new_desc=${new_desc//\"/\\\"}

        : Provide character entity replacement, a la HTML
        new_desc=${new_desc//\&apos\;/\'}
        new_desc=${new_desc//\&quot\;/\\\"}
        new_desc=${new_desc//\&lt\;/\<}
        new_desc=${new_desc//\&gt\;/\>}
        new_desc=${new_desc//\&amp\;/\&}
        new_desc=${new_desc//\&excl\;/\!}

        [[ -n $new_desc ]] && dopt="-d \"$new_desc\""
        [[ -z $new_desc && " ${!_ENV_ARGS[*]} " == *\ DESCRIPTION\ * ]] && dopt="-d \" \""    # This is to handle DESCRIPTION="" case
        [[ -n $new_name ]] && Nopt="-N $new_name"

        print "$0()[$LINENO]($SECONDS): eval clsnapshot -R -n $snapshot $Nopt $dopt" >>$CLMGR_TMPLOG  # Always log commands
        eval "clsnapshot -R -n $snapshot $Nopt $dopt"
        rc=$?
        print "clsnapshot RC: $rc" >>$CLMGR_TMPLOG  # Always log command result
        (( $rc != RC_SUCCESS )) && rc=$RC_ERROR

        #===========================================================
        : If output from this operation was requested, retrieve it
        #===========================================================
        if (( $rc == RC_SUCCESS )); then
            [[ -n $new_name ]] && name=$new_name
            if (( CLMGR_VERBOSE )) || [[ -n $CLMGR_ATTRS ]]; then
                CL=$LINENO KLIB_HACMP_get_snapshot_attributes "$name" properties
            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_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 snapshot -h" "SNAPSHOT:" "$CLMGR_PROGNAME" 1>&2
    fi

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

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

