#!/bin/ksh93
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/lib/ksh93/util/KLIB_UTIL_runCommandGetHash.sh 1.4 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2006,2013 
# 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 

# @(#)39	1.4 src/43haes/lib/ksh93/util/KLIB_UTIL_runCommandGetHash.sh, hacmp.assist, 61haes_r714 1/16/13 13:55:20

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

    typeset -n data=$1
    shift

    typeset headings="" key="" value="" values=""
    typeset OUTPUT_FILE="$TMPDIR/.clmgr.rcgh.$$"
    typeset -i rc=0 try=0 tries=5

    if (( $# == 0 )); then
        rc=1

    else
        #==================================================
        : Run the command, retrying a few times if needed
        #==================================================
        for (( try=0; $try < $tries; )); do
            print "$0()[$LINENO]($SECONDS): $*" >>$CLMGR_TMPLOG
            "$@" >$OUTPUT_FILE 2>>$CLMGR_TMPLOG
            rc=$?
            if (( $rc == RC_SUCCESS )) && \
                [[ -f $OUTPUT_FILE && $(head -1 $OUTPUT_FILE) != \#* ]]
            then
                rc=$RC_ERROR  # Found unexpected, error output
            fi
            (( $rc == RC_SUCCESS )) && break || rc=$RC_ERROR

            (( try++ ))
            if (( $try <= ( $tries - 1 ) )); then
                sleep $try
            fi
        done

        if (( $rc == RC_SUCCESS )) && \
           [[ -f $OUTPUT_FILE && -s $OUTPUT_FILE ]]
        then
            headings=$(head -n 1 $OUTPUT_FILE)
            values=$(tail -n 1 $OUTPUT_FILE)
            while [[ -n $headings ]]; do
                key=${headings%%:*}
                headings=${headings#${key}:}
                value=${values%%:*}
                values=${values#${value}:}

                data[$key]=$value

                [[ $headings != *:* ]] && break
            done
        fi
    fi

    [[ -f $OUTPUT_FILE ]] && /usr/bin/rm -f $OUTPUT_FILE

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