#!/bin/ksh93
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/lib/ksh93/hacmp/KLIB_HACMP_view_log.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 
# @(#)00	1.4  src/43haes/lib/ksh93/hacmp/KLIB_HACMP_view_log.sh, hacmp.assist, 61haes_r714 5/15/13 14:22:11

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

=head1 VERSION

 Version Number:  1.4
 Last Extracted:  1/31/14 04:41:46
 Last Changed:    5/15/13 14:22:11

 Path, Component, Release(, Level):
 src/43haes/lib/ksh93/hacmp/KLIB_HACMP_view_log.sh, hacmp.assist, 61haes_r714

=head1 SYNOPSIS

 clmgr view log [ {<log>|EVENTS} ] \
             [ TAIL=<number_of_trailing_lines> ] \
             [ HEAD=<number_of_leading_lines> ] \
             [ FILTER=<pattern>[,<pattern#2>,<pattern#n>,...] ] \
             [ DELIMITER=<alternate_pattern_delimiter> ] \
             [ CASE={insensitive|no|off|false} ]

=head1 DESCRIPTION

Displays the contents of the specified PowerHA log, in the indicated manner.
If one or more filters are specified, then they are applied first, before
limiting the number of lines. If both TAIL and HEAD are provided, TAIL is
applied first, then HEAD.

=head1 ARGUMENTS

 1. log [REQUIRED] [string]
    The shortname of the PowerHA log file that is to be viewed.

 2. tail [OPTIONAL] [integer]
    The number of lines to display from the bottom of the file.

 3. head [OPTIONAL] [integer]
    The number of lines to display from the top of the output.

 4. filters [OPTIONAL] [string]
    One or more search strings.

 5. delimiter [OPTIONAL] [string]
    The string separator to look for in the specified filters.

    Defaults to a comma.

 6. case [OPTIONAL] [string]
    An indicator of whether or not the specified filters should be
    used to search the log in a case sensitive manner (the default).

    Valid values include: {insensitive|no|off|false}

=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 on_exit_view_log {
    [[ -n $EVFILE && -f $EVFILE ]] && /usr/bin/rm -f "$EVFILE"
    (( $rc == RC_UNKNOWN )) && exit $RC_ERROR
}

function KLIB_HACMP_view_log {
    trap 'on_exit_view_log' EXIT
    LINENO=2 . $HALIBROOT/log_entry "$0()" "$CL"
    : version=1.4, src/43haes/lib/ksh93/hacmp/KLIB_HACMP_view_log.sh, hacmp.assist, 61haes_r714
    : INPUTS: $*

    typeset log=${1##*/}
    shift
    typeset -i tail=${1//\"/}
    typeset -i head=${2//\"/}
    typeset filters=${3//\"/}
    typeset delimiter=${4//\"/}
    typeset -l case=${5//\"/}

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

    #===================================
    : Declare and initialize variables
    #===================================
    typeset -i default=500 status=0
    rc=$RC_UNKNOWN  # Declare globally, so it is visible in the exit function
    typeset -i i=0
    typeset DIRECTORY= GREP=
    EVFILE=  # Declare globally, so it is visible in the exit function
    typeset -u UC_LOG=$log
    [[ -z $delimiter ]] && delimiter=','

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

    elif [[ $UC_LOG != EV* ]]; then
        CL=$LINENO KLIB_HACMP_is_known_log "$log" DIRECTORY 2>/dev/null
        if (( $? != RC_SUCCESS )); then
            /usr/bin/dspmsg -s $CLVT_SET $CLVT_MSGS 102 "\nERROR: \"%1\$s\" does not appear to exist!\n\n" "$log" 1>&2
            rc=$RC_NOT_FOUND
        elif [[ ! -f "$DIRECTORY/$log" ]]; then
            /usr/bin/dspmsg -s $CLVT_SET $CLVT_MSGS 102 "\nERROR: \"%1\$s\" does not appear to exist!\n\n" "$DIRECTORY/$log" 1>&2
            rc=$RC_NOT_FOUND
        fi
    fi

    if (( $rc == RC_NOT_FOUND )); then
        /usr/bin/dspmsg -s $CLVT_SET $CLVT_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 [[ -n $1 ]] && (( tail <= 0 )); then
        /usr/bin/dspmsg -s $CLVT_SET $CLVT_MSGS 111 "\nERROR: \"%1\$s\" requires a positive, integer value.\n\n" TAIL 1>&2
        rc=$RC_INCORRECT_INPUT
    elif [[ -n $2 ]] && (( head <= 0 )); then
        /usr/bin/dspmsg -s $CLVT_SET $CLVT_MSGS 111 "\nERROR: \"%1\$s\" requires a positive, integer value.\n\n" HEAD 1>&2
        rc=$RC_INCORRECT_INPUT
    fi

    if [[ $filters == *@(\`|\$\()* ]]; then
        /usr/bin/dspmsg -s $CLVT_SET $CLVT_MSGS 133 "\nERROR: the \"%1\$s\" option contains a shell execution character: %2\$s\n\n" FILTER "$filters" 1>&2
        rc=$RC_INCORRECT_INPUT
    fi

    if (( $rc == RC_UNKNOWN )); then
        if [[ "$*" == *([[:space:]]) ]]; then
            /usr/bin/dspmsg -s $CLVT_SET $CLVT_MSGS 134 "Warning: no options were provided for log \"%1\$s\".\n         Defaulting to the last %2\$s lines.\n" "$log" "$default"
            tail=$default
        fi

        #================================================
        : See if an events summary report was requested
        #================================================
        if [[ $UC_LOG == EV* ]]; then
            DIRECTORY=$TMPDIR
            log=clmgr.KHvl.evs.$$
            EVFILE=$DIRECTORY/$log

            print "$0()[$LINENO]($SECONDS): $HAUTILS/clevsummary" >>$CLMGR_TMPLOG  # Always log commands
            $HAUTILS/clevsummary >$EVFILE 2>/dev/null
            rc=$?
            print "clevsummary RC: $rc" >>$CLMGR_TMPLOG  # Always log command result
        fi

        #===========================
        : Display the log contents
        #===========================
        if [[ -n $filters ]]; then
            IFS=$delimiter
            for filter in $filters; do
                GREP="$GREP | /usr/bin/grep"
                [[ $case == @(i|f|n|off)* ]] && GREP="$GREP -i"
                GREP="$GREP \"$filter\""
            done
            GREP=${GREP# \| }

            #======================
            : Insert the log file
            #======================
            if [[ $GREP == *\|* ]]; then
                GREP=${GREP/\|/ $DIRECTORY/$log \|}
            else
                GREP="$GREP $DIRECTORY/$log"
            fi

            if (( tail && head )); then
                eval $GREP | /usr/bin/tail -n $tail | /usr/bin/head -n $head
            elif (( tail )); then
                eval $GREP | /usr/bin/tail -n $tail
            elif (( head )); then
                eval $GREP | /usr/bin/head -n $head
            else
                eval $GREP
            fi
        else
            if (( tail && head )); then
                 /usr/bin/tail -n $tail $DIRECTORY/$log | /usr/bin/head -n $head
            elif (( tail )); then
                /usr/bin/tail -n $tail $DIRECTORY/$log
            elif (( head )); then
                /usr/bin/head -n $head $DIRECTORY/$log
            fi
        fi
        (( $? != RC_SUCCESS )) && rc=$RC_ERROR
    fi

    [[ -f $EVFILE ]] && /usr/bin/rm -f "$EVFILE"

    #=======================================================================
    : 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=$LINENO 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" \
        "view log -h" "LOG:" "$CLVT_PROGNAME" 1>&2
    fi

    (( $rc == RC_UNKNOWN )) && rc=$RC_SUCCESS

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