#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG                                                    
#  This is an automatically generated prolog.                                  
#                                                                              
#  Copyright (C) Altran ACT S.A.S. 2017,2021.  All rights reserved.  
#                                                                              
#  ALTRAN_PROLOG_END_TAG                                                      
#                                                                              
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r720 src/43haes/lib/ksh93/hacmp/KLIB_HACMP_modify_log.sh 1.9 
#  
# 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_log.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM

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

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

=head1 VERSION

 Version Number:  1.9
 Last Extracted:  4/1/15 00:23:42
 Last Changed:    3/16/15 09:15:17

 Path, Component, Release(, Level):
 src/43haes/lib/ksh93/hacmp/KLIB_HACMP_modify_log.sh, hacmp.assist, 61haes_r720, 1514A_hacmp720

=head1 SYNOPSIS

 clmgr modify log {<log>|ALL} \
             [ DIRECTORY="{<new_log_directory>|DEFAULT}" ]
             [ FORMATTING={none|standard|low|high} ] \
             [ REMOTE_FS={true|false} ]

 NOTE: when "DEFAULT: is specified for the "DIRECTORY" attribute,
       then the original, default PowerHA directory value is
       restored.
 NOTE: the "FORMATTING" attribute only applies to the "hacmp.out"
       log, and is ignored for all other logs.
 NOTE: the "FORMATTING" attribute only applies
       to the "hacmp.out" and "clstrmgr.debug" logs, and is
       ignored for all other logs.
 NOTE: when "ALL" is specified in place of a log name, then the provided
       DIRECTORY and REMOTE_FS modifications are applied to all the logs.
 NOTE: when "EVENTS" is specified in place of a log name,
       then an events summary report is displayed.

=head1 DESCRIPTION

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

 3. directory [OPTIONAL] [string]
    The directory in which the specified log should be stored.

 4. formatting [OPTIONAL] [string]
    The style of formatting to apply to the specified log (only
    applicable to "hacmp.out"): none, standard, low, high

 5. trace [OPTIONAL] [string]
    The trace level to apply to the specified log (only
    applicable to clstrmgr.debug): low, high

 6. remote_fs [OPTIONAL] [string]
    A Boolean-like value indicating whether or not to allow the
    specified log to be stored on a remote/mounted file system.

    Valid values are "yes" and "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

=head1 COPYRIGHT

COPYRIGHT International Business Machines Corp. 2005,2010
All Rights Reserved

=cut
} # End of POD-formatted documentation.


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

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

    typeset directory=${1//\"/}
    typeset -l formatting=${2//\"/}
    typeset -l trace=${3//\"/}
    typeset -l remote_fs=${4//\"/}

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

    #===================================
    : Declare and initialize variables
    #===================================
    typeset -i rc=$RC_UNKNOWN
    typeset ropt= attr=
    typeset -i i=0
    typeset -A logAttrs
    typeset -u uc_directory=$directory

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

    #=================
    : Validate input
    #=================
    if [[ -z $log ]]; 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" "$log" 1>&2
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 147 "Available Logs:\n\n" 1>&2

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

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

    if [[ $log == "all" && -z $directory ]]; then
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 101 "\nERROR: this operation requires the \"%1\$s\" attribute.\n\n" DIRECTORY 1>&2
        rc=$RC_MISSING_INPUT
    fi

    if [[ -n $directory && $directory != /* && $uc_directory != "DEFAULT" ]]
    then
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 106 "\nERROR: the specified path/file does not appear to be in absolute format:\n       %1\$s\n\n" "$directory" 1>&2
        rc=$RC_INCORRECT_INPUT
    fi

    if [[ -n $directory && $uc_directory != "DEFAULT" && ! -d $directory ]]
    then
        CL=$LINENO cl_dspmsg -s $CLMGR_SET $CLMGR_MSGS 107 '\nERROR: the specified path/file does not appear to exist on "%2$s": %1$s\n\n' "$directory" "$LOCAL_NODE" 1>&2
        rc=$RC_INCORRECT_INPUT
    fi

    if [[ $log != "hacmp.out" ]] && [[ -n $formatting ]]; then
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 1156 "\nERROR: the \"FORMATTING\" attribute is \n       only valid for the \"hacmp.out\" log.\n\n" 1>&2
        rc=$RC_INCORRECT_INPUT
    fi

    if [[ -n $formatting && $formatting != @(none|standard|low|high) ]]; then
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 110 "\nERROR: invalid value specified for \"%1\$s\":  \"%2\$s\".\n" FORMATTING "$formatting" 1>&2
        /usr/bin/dspmsg -s $CLMGR_SET $CLMGR_MSGS 3 "Valid values: %1\$s\n\n" "none, standard, low, high" 1>&2
        rc=$RC_INCORRECT_INPUT
    fi

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

    #=======================================================
    : Modify the log if no input errors have been detected
    #=======================================================
    if (( $rc == RC_UNKNOWN )); then
        if [[ -n $directory || -n $remote_fs ]]; then

            #================================================================
            : If we need to know any current/existing values, retrieve them
            #================================================================
            if [[ -z $directory || -z $remote_fs || $uc_directory == "DEFAULT" ]]
            then
                if [[ $log == "all" ]]; then
                    CL=$LINENO KLIB_HACMP_get_log_attributes "hacmp.out" logAttrs
                else
                    CL=$LINENO KLIB_HACMP_get_log_attributes "$log" logAttrs
                fi
            fi

            #============================================================
            : Make sure that we have a valid directory. It is required.
            #============================================================
            if [[ -z $directory || $uc_directory == "DEFAULT" ]]; then
                if [[ $uc_directory == "DEFAULT" ]]; then
                    directory=${logAttrs[DEFAULT]}
                else
                    directory=${logAttrs[DIRECTORY]}
                fi
            fi

            #============================================
            : Make sure to preserve the current setting
            #============================================
            [[ -z $remote_fs ]] && remote_fs=${logAttrs[REMOTEFS]}

            [[ $remote_fs == @(y|t)* ]] && ropt="-r"

            if [[ $log == "all" ]]; then
                print "$0()[$LINENO]($SECONDS): $HAUTILS/cllog -C -v $directory $ropt" >>$CLMGR_TMPLOG  # Always log commands
                VERBOSE_LOGGING="" $HAUTILS/cllog -C -v $directory $ropt
            else
                print "$0()[$LINENO]($SECONDS): $HAUTILS/cllog -c $log -v $directory $ropt" >>$CLMGR_TMPLOG  # Always log commands
                VERBOSE_LOGGING="" $HAUTILS/cllog -c $log -v $directory $ropt
            fi
            rc=$?
            print "cllog RC: $rc" >>$CLMGR_TMPLOG  # Always log command result
            (( $rc != RC_SUCCESS )) && rc=$RC_ERROR
        fi

        if [[ $log == "hacmp.out" ]]; then
            node=$LOCAL_NODE
            if [[ -n $node ]]; then
                CL=$LINENO KLIB_HACMP_get_log_attributes hacmp.out logAttrs
                typeset new_format=${logAttrs[FORMATTING]}
                typeset new_trace=${logAttrs[TRACE_LEVEL]}

                [[ -z $formatting && -n $new_format ]] && formatting=$new_format
	
                if [[ -n $formatting ]]; then
                    case "$formatting" in
                        "none")     new_format=Default  ;;
                        "standard") new_format=Standard ;;
                        "low")      new_format=Html1    ;;
                        "high")     new_format=Html2    ;;
                    esac
                else
                    new_format="Standard"
                fi
                new_trace="high"

                print "$0()[$LINENO]($SECONDS): $HAUTILS/clchparam -n $node VERBOSE_LOGGING=$new_trace DEBUG_LEVEL=$new_format" >>$CLMGR_TMPLOG  # Always log commands
                $HAUTILS/clchparam -n $node VERBOSE_LOGGING=$new_trace \
                                            DEBUG_LEVEL=$new_format
                print "clchparam RC: $?" >>$CLMGR_TMPLOG # Always log command result
            fi
        elif [[ $log == "clstrmgr.debug" ]]; then
            if [[ -n $trace ]]; then
                print "$0()[$LINENO]($SECONDS): $HAUTILS/cl_clstrmgr_debug_level -c $trace" >>$CLMGR_TMPLOG  # Always log commands
                $HAUTILS/cl_clstrmgr_debug_level -c $trace
                print "cl_clstrmgr_debug_level RC: $?" >>$CLMGR_TMPLOG # Always log command result
            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_log_attributes "$log" 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 log -h" "LOG:" "$CLMGR_PROGNAME" 1>&2
    fi

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