#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG                                                    
#  This is an automatically generated prolog.                                  
#                                                                              
#  Copyright (C) Altran ACT S.A.S. 2018,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_add_filecollection.sh 1.9 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2006,2013 
# 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_add_filecollection.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_add_filecollection

=head1 SYNOPSIS

 add file_collection <file_collection> \
             FILES="/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 create a file collection within the cluster that conforms
to the provided specifications.

=head1 ARGUMENTS

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

 2. fcname [REQUIRED] [string]
    The label to apply to the new file collection.

 3. files [OPTIONAL] [string]
    A list of files to be referenced by (a.k.a. "included within", although
    not literally!) the new file collection object.

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

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

 6. description  [OPTIONAL] [string]
    A description to apply to the new file collection.

=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_add_filecollection {
    . $HALIBROOT/log_entry "$0()" "$CL"
    : version=@(#)  7d4c34b 43haes/lib/ksh93/hacmp/KLIB_HACMP_add_filecollection.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
    : INPUTS: $*

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

    typeset -n files=$1
               files=${files//\"/}
    typeset -l sync_with_cluster=${2//\"/}
    typeset -l sync_when_changed=${3//\"/}
    typeset description=${4//\"/}

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

    #===================================
    : Declare and initialize variables
    #===================================
    typeset -i rc=$RC_UNKNOWN
    typeset -A tf
    tf=(
        [_DEFAULT_]="no"
        [true]="yes"
        [false]="no"
        [yes]="yes"
        [no]="no"
    )

    typeset existing
    CL=$LINENO KLIB_HACMP_list_filecollection existing 2>>$CLMGR_TMPLOG

    #================================================================
    : Check for a defined cluster. No need to continue without one.
    #================================================================
    CL=$LINENO isClusterDefined
    if (( $? != RC_SUCCESS )); then
        rc=$RC_MISSING_DEPENDENCY

    #=================
    : Validate input
    #=================
    elif [[ -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 [[ " ${existing[*]} " == *\ $fcname\ * ]]; then
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 229 "\nERROR: the specified object already exists: \"%1\$s\"\n\n" "$fcname" 1>&2
        rc=$RC_INCORRECT_INPUT

    elif [[ -n $fcname && -n "${fcname//[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" "$fcname" "${fcname//[a-zA-Z0-9_]/}" 1>&2
        rc=$RC_INCORRECT_INPUT
    fi

    if [[ -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" "no, yes" 1>&2
        rc=$RC_INCORRECT_INPUT
    fi

    if [[ -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" "no, yes" 1>&2
        rc=$RC_INCORRECT_INPUT
    fi

    #===================================================================
    : Create the file collection if no input errors have been detected
    #===================================================================
    if (( $rc == RC_UNKNOWN )); then
        typeset swcl= swch=
        if [[ $sync_with_cluster == *([[:space:]]) ]]; then
            swcl=${tf[_DEFAULT_]}
        else
            swcl=${tf[$sync_with_cluster]}
        fi
        if [[ $sync_when_changed == *([[:space:]]) ]]; then
            swch=${tf[_DEFAULT_]}
        else
            swch=${tf[$sync_when_changed]}
        fi
        print -- "$0()[$LINENO]($SECONDS): $HAUTILS/clfilecollection -o coll -a \"$fcname\" -\"$description\" $swcl $swch" >>$CLMGR_TMPLOG  # Always log commands
        $HAUTILS/clfilecollection -o coll -a "$fcname" \
            -"$description" $swcl $swch
        rc=$?
        print "clfilecollection RC: $rc" >>$CLMGR_TMPLOG  # Always log command result
        if (( $rc != RC_SUCCESS )); then
            rc=$RC_ERROR
            /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 109 "\nERROR: the operation appears to have failed.\n\n" 1>&2

        elif [[ -n $files ]]; then
            print -- "$0()[$LINENO]($SECONDS): $HAUTILS/clfilecollection -o file -a \"$fcname\" ${files//,/ }" >>$CLMGR_TMPLOG  # Always log commands
            $HAUTILS/clfilecollection -o file -a "$fcname" ${files//,/ }
            rc=$?
            print "clfilecollection RC: $rc" >>$CLMGR_TMPLOG  # Always log command result
            if (( $rc != RC_SUCCESS )); then
                rc=$RC_ERROR
                /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 215 "\nERROR: an error occurred while attempting to add \"%1\$s\" to file collection \"%2\$s\".\n       An attempt will be made to remove the file collection, since it was\n       only partially created.\n\n" "${files//,/ }" "$fcname" 1>&2

                : Clean up the failed addition
                print -- "$0()[$LINENO]($SECONDS): $HAUTILS/clfilecollection -o coll -r \"$fcname\"" >>$CLMGR_TMPLOG  # Always log commands
                $HAUTILS/clfilecollection -o coll -r "$fcname"
                typeset -i cmd_rc=$?
                print " RC: $cmd_rc" >>$CLMGR_TMPLOG  # Always log command result
                if (( $cmd_rc != RC_SUCCESS )); then
                    print -n "       "
                    /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 300 "\nERROR: failed to delete \"%1\$s\".\n\n" "$fcname" 1>&2
                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
        else
            /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 201 "\nERROR: failed to create \"%1\$s\".\n\n" "$fcname" 1>&2
        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" \
        "add file_collection -h" "FILE COLLECTION:" "$CLMGR_PROGNAME" 1>&2
    fi

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

