#!/bin/ksh93
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r721 src/43haes/lib/ksh93/util/KLIB_UTIL_parse_arguments.sh 1.9 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2006,2015 
# 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 
# @(#)60	1.9 src/43haes/lib/ksh93/util/KLIB_UTIL_parse_arguments.sh, hacmp.assist, 61haes_r721, 1607A_hacmp721 12/28/15 10:52:49

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

#============================================================================
#
# Name:        KLIB_UTIL_parse_arguments
#
# Description: Parses the "name=value" input pairs for clmgr, and stores
#              them in an associative array.
#
# Inputs:      list    A reference to an associative array. This is
#                      where the parsed results are stored.
#
#              The remaining arguments are whatever pairs were passed
#              in to clmgr for this invocation.
#
# Outputs:     Nothing is output to STDOUT/STDERR.
#
# Returns:     0, if no parsing errors occur. Otherwise, a non-zero.
#
#============================================================================
function KLIB_UTIL_parse_arguments {
    LINENO=2 . $HALIBROOT/log_entry "$0()" "$CL" max
    : version=1.9, src/43haes/lib/ksh93/util/KLIB_UTIL_parse_arguments.sh, hacmp.assist, 61haes_r721, 1607A_hacmp721
    : INPUTS: $*

    typeset -n list=$1
    shift

    typeset    OPT_ARG=$* ORIG_ARGS=$*
    typeset    name="" value="" value_regex=""
    typeset -u nameUC=""
    typeset -i argCnt=0 rc=$RC_UNKNOWN ORIG_SIZE=0 NEW_SIZE=0

    typeset -i brake=50  # Just to be *sure* there are no hangs...!
    while [[ -n $OPT_ARG ]]; do
        OPT_ARG=${OPT_ARG##+([[:space:]])}  # Remove leading whitespace
        (( argCnt++ ))

        #=================================================
        # Search for the first variable name in OPT_ARG.
        # It can be found by simply removing everything
        # after the first equals sign from OPT_ARG.
        #=================================================
        name=${OPT_ARG%%=*}
        if [[ -n $name ]]; then
            OPT_ARG=${OPT_ARG#${name}=}

            #==================================================================
            # If there is another variable assignment, the current values ends
            # where that next assignment begins. Otherwise, all the remaining
            # values are assumed to belong to the current variable.
            #
            # Starting with an OPT_ARG of --> abc=123 def=456 ghi=789
            # The line of code, above, extracts the first/leading variable
            # name, then removes it from OPT_ARG. In this case, that is "abc",
            # leaving a remaining OPT_ARG of "123 def=456 ghi=789".
            #
            # Case 1:
            #     OPT_ARG -------> 123 def=456 ghi=789
            #     Match/Remove -->    ^^^^^
            #     Remainder -----> 123
            #     The remainder is the value assign to the current variable.
            #     So on the first pass in this example, list[abc]=123.
            #
            # Case 2:
            #     OPT_ARG -------> 789
            #     All other variable assignments (if there were multiple)
            #     have already been processed. Therefore, whatever value(s)
            #     remaining in OPT_ARG must be the values for the last
            #     extracted variable name. So in this example, list[ghi]=789.
            #==================================================================
            if [[ $OPT_ARG == *+([[:space:]])+([[:alnum:]]|_|-)=* ]]; then
                value=${OPT_ARG%%+([[:space:]])+([[:alnum:]]|_|-)=*}

                if [[ -n $value ]]; then
                    value_regex=${value//\(/\\\(}
                    value_regex=${value_regex//\)/\\\)}
                    value_regex=${value_regex//\*/\\*}
                    value_regex=${value_regex//\]/\\\]}
                    value_regex=${value_regex//\[/\\\[}

                    # The odd quotes are for ksh93 -n!
                    value_regex=${value_regex//'|'/'\\|'}
                    value_regex=${value_regex//'&'/\\'&'}

                    ORIG_SIZE=${#OPT_ARG}
                    OPT_ARG=${OPT_ARG#$value_regex}  # Remove the value from OPT_ARG
                    NEW_SIZE=${#OPT_ARG}

                    # Make sure the removal was successful
                    if (( $NEW_SIZE == $ORIG_SIZE )); then
                        rc=$RC_ERROR
                        break
                    fi
                fi

            elif (( argCnt == 1 )); then
                value=${OPT_ARG#*=}
                unset OPT_ARG  # Kill the loop. All input has been processed.

            else
                value=$OPT_ARG
                unset OPT_ARG  # Kill the loop. All input has been processed.
            fi

            if [[ $name != -@(c|x|d|h|v|D|E|S) ]]; then
                nameUC=${name//-/_}  # Convert dashes to underscores
                list[$nameUC]=$value # Store the name/value pair in the return list
            fi
        fi

        if (( --brake <= 0 )); then
            rc=$RC_ERROR
            break  # Avoid hangs, please!
        fi
    done

    if (( rc == RC_ERROR )); then
        typeset CLMGR_LOG=$CLMGR_TMPLOG
        if [[ -z $CLMGR_LOG || ! -f $CLMGR_LOG || ! -w $CLMGR_LOG ]]; then
            CLMGR_LOG="$(ODMDIR=/etc/objrepos $HAUTILS/clodmget -q name=clutils.log -f value -n HACMPlogs)/clutils.log"
            [[ ! -w $CLMGR_LOG ]] && CLMGR_LOG="/tmp/clutils.log"
        fi

        print "$0()[$LINENO]($SECONDS):
ERROR: an internal error occurred within KLIB_UTIL_parse_arguments().

          Original Args:  $ORIG_ARGS
   Problem parsing this:  \"$name=$value\"
      Unprocessed Input:  $OPT_ARG
" >>$CLMGR_LOG

    else
        rc=$RC_SUCCESS
    fi

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