#!/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
#
# @(#)  7d4c34b 43haes/lib/ksh93/hacmp/KLIB_HACMP_view_snapshot.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM

# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/lib/ksh93/hacmp/KLIB_HACMP_view_snapshot.sh 1.2 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2009,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 

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

 clmgr view snapshot <snapshot> \
             [ 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

The PowerHA snapshot feature captures information about the cluster 
configuration which can be used later to restore the configuration, create
a similar configuration on other nodes, etc. As part of that data capture,
PowerHA can optionally collect data from various system commands like lsvg, 
netstat, etc and store that output in a separate "info" file.
The "view snapshot" command displays the contents of the specified snapshot's 
".info" file, if that file exists, 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. snapshot [REQUIRED] [string]
    The name of the PowerHA snapshot 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

=cut
} # End of POD-formatted documentation.


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

    typeset snapshot=${1##*/}
            snapshot=${snapshot//\"/}
    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 status=0 rc=$RC_UNKNOWN
    typeset -i i=0
    typeset GREP=
    [[ -z $delimiter ]] && delimiter=','
    if [[ -z $SNAPSHOTPATH || ! -d $SNAPSHOTPATH ]]; then
        SNAPSHOTPATH=/usr/es/sbin/cluster/snapshots
    fi

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

    else
        CL=$LINENO KLIB_HACMP_is_known_snapshot "$snapshot" 2>/dev/null
        if (( $? != RC_SUCCESS )); then
            dspmsg -s $CLVT_SET $CLVT_MSGS 102 "\nERROR: \"%1\$s\" does not appear to exist!\n\n" "$snapshot" 1>&2
            dspmsg -s $CLVT_SET $CLVT_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_NOT_FOUND
        elif [[ ! -f "$SNAPSHOTPATH/$snapshot.info" ]]; then
            dspmsg -s $CLVT_SET $CLVT_MSGS 137 "\nERROR: the specified snapshot's information file could not be found.\n\n" 1>&2
            print -u2 "           $snapshot.info\n"
            rc=$RC_NOT_FOUND
        fi
    fi

    if [[ -n $1 ]] && (( tail <= 0 )); then
        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
        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
        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

    #========================================
    : Display the snapshot ".info" contents
    #========================================
    if (( $rc == RC_UNKNOWN )); then
        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/\|/ $SNAPSHOTPATH/$snapshot.info \|}
            else
                GREP="$GREP $SNAPSHOTPATH/$snapshot.info"
            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 [[ "$*" == *([[:space:]]) ]]; then
                 /usr/bin/cat $SNAPSHOTPATH/$snapshot.info
            elif (( tail && head )); then
                 /usr/bin/tail -n $tail $SNAPSHOTPATH/$snapshot.info |\
                 /usr/bin/head -n $head
            elif (( tail )); then
                /usr/bin/tail -n $tail $SNAPSHOTPATH/$snapshot.info
            elif (( head )); then
                /usr/bin/head -n $head $SNAPSHOTPATH/$snapshot.info
            fi
        fi
        (( $? != RC_SUCCESS )) && rc=$RC_ERROR || rc=$RC_SUCCESS
    fi

    #=======================================================================
    : 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_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 snapshot -h" "SNAPSHOT:" "$CLVT_PROGNAME" 1>&2
    fi

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