#!/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_modify_file_collection.sh 1.4 
#  
# 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_modify_file_collection.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_modify_file_collection

=head1 SYNOPSIS

 clmgr modify file_collection <file_collection> \
             [ NAME="<new_file_collection_label>" ] \
             [ ADD="/path/to/file1,/path/to/file2,..." ] \
             [ DELETE={"/path/to/file1,/path/to/file2,..."|ALL} ] \
             [ REPLACE={"/path/to/file1,/path/to/file2,..."|""} ] \
             [ SYNC_WITH_CLUSTER={no|yes}  ] \
             [ SYNC_WHEN_CHANGED={no|yes} ] \
             [ DESCRIPTION="<file_collection_description>" ]

 NOTE: the aliases for "file_collection" are "fc" and "fi".

=head1 DESCRIPTION

Attempts to modify the specified file collection
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. fcname [REQUIRED] [string]
    The label of the file collection that is to be modified.

 3. new_name [OPTIONAL] [string]
    The new label to attempt to apply to the specified file collection.

 4. new_desc  [OPTIONAL] [string]
    A new description to apply to the specified file collection.

 5. files_to_add [OPTIONAL] [string ref]
    A list of files to be add to the specified file collection.

 6. files_to_remove [OPTIONAL] [string ref]
    A list of files to be removed from the specified file collection.

 7. files_to_replace [OPTIONAL] [string ref]
    A list of files to be replace the existing set of files referenced
    by the specified file collection.

 8. sync_with_cluster [OPTIONAL] [string]
    A Boolean-style indicator of whether or not the file collection should
    propagate its files to all the cluster nodes every time a cluster
    synchronization is performed.

 9. sync_when_changed[OPTIONAL] [string]
    A Boolean-style indicator of whether or not the file collection should
    propagate its files to all the cluster nodes whenever any of those files
    gets changed.

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


function KLIB_HACMP_modify_file_collection {
    . $HALIBROOT/log_entry "$0()" "$CL"
    : version=@(#)  7d4c34b 43haes/lib/ksh93/hacmp/KLIB_HACMP_modify_file_collection.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
    : INPUTS: $*

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

    typeset new_name=${1//\"/}
    typeset new_desc=${2//\"/}
    typeset -n files_to_add=$3
               files_to_add=${files_to_add//\"/}
    typeset -n files_to_remove=$4
               files_to_remove=${files_to_remove//\"/}
    typeset -n files_to_replace=$5
               files_to_replace=${files_to_replace//\"/}
    typeset -l sync_with_cluster=${6//\"/}
    typeset -l sync_when_changed=${7//\"/}

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

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

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

    #================================================================
    : Assuming an object was specified, see if it is a known object
    #================================================================
    if [[ $fcname != *([[:space:]]) ]]; then
        CL=$LINENO KLIB_HACMP_is_known_filecollection "$fcname"
        (( $? != RC_SUCCESS )) && rc=$RC_NOT_FOUND
    fi

    #=================
    : Validate input
    #=================
    if [[ -z $fcname ]]; then
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 100 "\nERROR: a name/label must be provided.\n\n" 1>&2
        rc=$RC_MISSING_INPUT

    elif (( $rc == RC_NOT_FOUND )); then
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 102 "\nERROR: \"%1\$s\" does not appear to exist!\n\n" "$fcname" 1>&2
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 144 "Available File Collections:\n\n" 1>&2

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

    elif [[ -n $new_name && -n "${new_name//[a-zA-Z0-9_]/}" ]]; then
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 105 "\nERROR: one or more invalid characters were detected in \"%1\$s\" (\"%2\$s\").\n\nValid characters include letters, numbers, and underscores only.\n\n" "$new_name" "${new_name//[a-zA-Z0-9_]/}" 1>&2
        rc=$RC_INCORRECT_INPUT

    elif [[ "$*" == *([[:space:]]) ]]; then
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 401 "\nERROR: no valid modifications were specified for \"%1\$s\".\n\n" "$fcname" 1>&2
        rc=$RC_MISSING_INPUT

    elif [[ -n $sync_with_cluster && $sync_with_cluster != @(y|t|n|f)* ]]; then
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 110 "\nERROR: invalid value specified for \"%1\$s\":  \"%2\$s\".\n" SYNC_WITH_CLUSTER "$sync_with_cluster" 1>&2
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 3 "Valid values: %1\$s\n\n" "true, false" 1>&2
        rc=$RC_INCORRECT_INPUT

    elif [[ -n $sync_when_changed && $sync_when_changed != @(y|t|n|f)* ]]; then
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 110 "\nERROR: invalid value specified for \"%1\$s\":  \"%2\$s\".\n" SYNC_WHEN_CHANGED "$sync_when_changed" 1>&2
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 3 "Valid values: %1\$s\n\n" "true, false" 1>&2
        rc=$RC_INCORRECT_INPUT

    #===================================================================
    : Modify the file collection if no input errors have been detected
    #===================================================================
    else
        CL=$LINENO KLIB_HACMP_get_filecollection_attributes "$fcname" fc_attrs
        [[ -z $new_desc && " ${!_ENV_ARGS[*]} " != *\ DESCRIPTION\ * ]] && new_desc=${fc_attrs[DESCRIPTION]}
        [[ -z $sync_with_cluster ]] && sync_with_cluster=${fc_attrs[SYNC_WITH_CLUSTER]}
        [[ $sync_with_cluster == @(y|t)* ]] && sync_with_cluster=yes || sync_with_cluster=no
        [[ -z $sync_when_changed ]] && sync_when_changed=${fc_attrs[SYNC_WHEN_CHANGED]}
        [[ $sync_when_changed == @(y|t)* ]] && sync_when_changed=yes || sync_when_changed=no

        # NOTE: the seemingly odd, out of place leading dashes in the
        #       command below are apparently *required* by the
        #       clfilecollection command. So they are *not* the typos
        #       that they appear to be! I suspect it was an attempt to
        #       preserve them as positional parameters, although there
        #       are other, better ways to accomplish that task.
        print "$0()[$LINENO]($SECONDS): $HAUTILS/clfilecollection -o coll -c \"$fcname\" -\"$new_name\" -\"$new_desc\" \"$sync_with_cluster\" \"$sync_when_changed\"" >>$CLMGR_TMPLOG  # Always log commands
        $HAUTILS/clfilecollection -o coll -c "$fcname" -"$new_name" -"$new_desc" "$sync_with_cluster" "$sync_when_changed"
        rc=$?
        print "clfilecollection RC: $rc" >>$CLMGR_TMPLOG  # Always log command result
        if (( $rc == RC_SUCCESS )); then
            [[ -n $new_name ]] && fcname=$new_name
        else
            rc=$RC_ERROR
        fi
    fi

    if (( $rc == RC_UNKNOWN || $rc == RC_SUCCESS )); then

        #
        # REPLACE
        #
        if [[ -n $files_to_replace ]]; then
            typeset existing_files=
            for attr in ${!fc_attrs[*]}; do
                if [[ $attr == FILE* ]]; then
                    existing_files="$existing_files ${fc_attrs[$attr]}"
                fi
            done

            if [[ -n $existing_files ]]; then
                files_to_remove=$existing_files
            fi
            [[ " $files_to_replace " != *\ ALL\ * ]] && files_to_add=$files_to_replace
        fi

        #
        # REMOVE
        #
        if [[ -n $files_to_remove ]]; then
            [[ " $files_to_remove " == *\ ALL\ * ]] && files_to_remove=${fc_attrs[FILES]//+([[:space:]])/ }

            if [[ -n $files_to_replace ]]; then
                print "$0()[$LINENO]($SECONDS): $HAUTILS/clfilecollection -o file -r \"$fcname\" ${files_to_remove//,/ }" >>$CLMGR_TMPLOG  # Always log commands
                $HAUTILS/clfilecollection -o file -r "$fcname" ${files_to_remove//,/ } >/dev/null 2>&1
            else
                print "$0()[$LINENO]($SECONDS): $HAUTILS/clfilecollection -o file -r \"$fcname\" ${files_to_remove//,/ }" >>$CLMGR_TMPLOG  # Always log commands
                $HAUTILS/clfilecollection -o file -r "$fcname" ${files_to_remove//,/ }
            fi
            rc=$?
            print "clfilecollection RC: $rc" >>$CLMGR_TMPLOG  # Always log command result

            if (( $rc != RC_SUCCESS )); then
                /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 404 "\nERROR: an error occurred while attempting to remove file(s)\n       \"%1\$s\" from file collection \"%2\$s\".\n\n" "${files_to_remove//,/ }" "$fcname" 1>&2
                rc=$RC_ERROR
            fi
        fi

        #
        # ADD
        #
        if (( $rc == RC_UNKNOWN || $rc == RC_SUCCESS )); then
            if [[ -n $files_to_add ]]; then
                print "$0()[$LINENO]($SECONDS): $HAUTILS/clfilecollection -o file -a \"$fcname\" ${files_to_add//,/ }" >>$CLMGR_TMPLOG  # Always log commands
                $HAUTILS/clfilecollection -o file -a "$fcname" ${files_to_add//,/ }
                rc=$?
                print "clfilecollection RC: $rc" >>$CLMGR_TMPLOG  # Always log command result
                if (( $rc != RC_SUCCESS )); then
                    /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 405 "\nERROR: an error occurred while attempting to add file(s)\n       \"%1\$s\" to file collection \"%2\$s\".\n\n" "${files_to_add//,/ }" "$fcname" 1>&2
                    rc=$RC_ERROR
                fi
            fi
        fi
    fi

    #===========================================================
    : If output from this operation was requested, retrieve it
    #===========================================================
    if (( $rc == RC_SUCCESS )); then
        if (( CLMGR_VERBOSE )) || [[ -n $CLMGR_ATTRS ]]; then
            CL=$LINENO KLIB_HACMP_get_filecollection_attributes "$fcname" properties
        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=$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" \
        "modify file_collection -h" "FILE COLLECTION:" "$CLMGR_PROGNAME" 1>&2
    fi

    log_return_msg "$rc" "$0()" "$LINENO"
    return $?
} # End of "KLIB_HACMP_modify_file_collection()"
#================================================
# The following, commented line enforces coding
# standards when this file is edited via vim.
#================================================
# vim:tabstop=4:shiftwidth=4:expandtab:smarttab
#================================================
