#!/bin/ksh93
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/lib/ksh93/hacmp/KLIB_HACMP_list_smart_assists.sh 1.3 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 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 
# @(#)04	1.3  src/43haes/lib/ksh93/hacmp/KLIB_HACMP_list_smart_assists.sh, hacmp.assist, 61haes_r714 6/10/12 15:39:47

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

=head1 VERSION

 Version Number:  1.3
 Last Extracted:  1/31/14 04:41:56
 Last Changed:    6/10/12 15:39:47

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

=head1 SYNOPSIS

 clmgr query smart_assist \
             [ TYPE={SA|COMP|PROP} ] \
             [ NODES=<node#1>[,<node#2>,<node#n>,...]] \
             [ SAID=<SA_ID> ] \
             [ COMPID=<COMPONENT_ID> ] \
             [ INSTANCEID=<INSTANCE_ID> ] \
             [ ENV="..." ]

=head1 DESCRIPTION

Displays the currently configured set of Smart Assists.

=head1 ARGUMENTS

 1. type [OPTIONAL] [String]
    Indicates what type of Smart Assist object is to be queried.
    The default is to list the available Smart Assists themselves.
    But if type is "COMP", then components will be listed, and if
    type is "PROP", then properties will be listed.
 2. nodes [OPTIONAL] [String]
    One or more nodes to pass to clvtsa.
 3. sa_id [OPTIONAL] [String]
    A Smart Assist identifier.
 4. comp_id [OPTIONAL] [String]
    A component identifier.
 5. inst_id [OPTIONAL] [String]
    An instance identifier.
 6. env [OPTIONAL] [String]
    A misellaneous properties string to pass to clvtsa.

=head1 RETURN

The exit code from clvtsa is returned.

=head1 COPYRIGHT

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

=cut
} # End of POD-formatted documentation.


function KLIB_HACMP_list_smart_assists {
    LINENO=2 . $HALIBROOT/log_entry "$0()" "$CL"
    : version=1.3, src/43haes/lib/ksh93/hacmp/KLIB_HACMP_list_smart_assists.sh, hacmp.assist, 61haes_r714
    : INPUTS: $*

    typeset -u type=$1
    typeset nodes=$2
    typeset sa_id=$3
    typeset comp_id=$4
    typeset inst_id=$5
    typeset env=$6
    
    [[ $CLMGR_LOGGING == 'med' ]] && set +x  # Only trace param values

    #===================================
    : Declare and initialize variables
    #===================================
    typeset -i rc=$RC_UNKNOWN
    typeset -i errors=0
    typeset cmd= var=

    #=================================================================
    : Some of the Smart Assists incorrectly attempt to add quotes to
    : their values, resulting in those quotes getting interpreted as
    : literal parts of the values. Not the intent. So it is safer to
    : remove leading/trailing quotes here, just to be safe.
    #=================================================================
    for var in type nodes sa_id comp_id inst_id env; do
        eval "$var=\${$var##+(\\\")}"
        eval "$var=\${$var%%+(\\\")}"
    done

    if [[ $type == @(C|P)* ]]; then
        if [[ $nodes == *([[:space:]]) ]]; then
            print -u2 "\nERROR: missing the \"NODES\" argument.\n"
            (( errors++ ))
        fi
        if [[ $sa_id == *([[:space:]]) ]]; then
            print -u2 "\nERROR: missing the \"SAID\" argument.\n"
            (( errors++ ))
        fi

        if [[ $type == P* ]]; then
            if [[ $comp_id == *([[:space:]]) ]]; then
                print -u2 "\nERROR: missing the \"COMPID\" argument.\n"
                (( errors++ ))
            fi
        fi
    fi

    if (( errors == 0 )); then
        if [[ $type == P* ]]; then
            print -- "$0()[$LINENO]($SECONDS): $HASABIN/clvtsa -q queryPROPs -n \"$nodes\" -s \"$sa_id\" -c \"$comp_id\" -i \"$inst_id\" -e \"$env\"" >>$CLMGR_TMPLOG  # Always log commands
            $HASABIN/clvtsa -q queryPROPs \
                            -n "$nodes"   \
                            -s "$sa_id"   \
                            -c "$comp_id" \
                            -i "$inst_id" \
                            -e "$env"
        elif [[ $type == C* ]]; then
            print -- "$0()[$LINENO]($SECONDS): $HASABIN/clvtsa -q queryCOMPs -n \"$nodes\" -s \"$sa_id\" -e \"$env\"" >>$CLMGR_TMPLOG  # Always log commands
            $HASABIN/clvtsa -q queryCOMPs \
                            -n "$nodes"   \
                            -s "$sa_id"   \
                            -e "$env"
        else
            print -- "$0()[$LINENO]($SECONDS): $HASABIN/clvtsa -q querySAs" >>$CLMGR_TMPLOG  # Always log commands
            $HASABIN/clvtsa -q querySAs
        fi
        rc=$?
        print "clvtsa RC: $rc" >>$CLMGR_TMPLOG  # Always log command result
        (( $rc != RC_SUCCESS )) && rc=$RC_ERROR
    else
        print -u2
        rc=$RC_ERROR
    fi

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