#!/bin/ksh93
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/lib/libcspoc/get_rc.sh 1.3 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1996,2011 
# 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 
# @(#)82        1.3 src/43haes/lib/libcspoc/get_rc.sh, hacmp.cspoc, 61haes_r714 11/28/11 14:53:05
# $Id: get_rc.sh,v 1.4 1996/05/02 21:39:36 acarr Exp $
#
#   COMPONENT_NAME: CSPOC
#
#   FUNCTIONS:
#
#   ORIGINS:
#
#
#   (C) COPYRIGHT International Business Machines Corp. 1996
#   All Rights Reserved
#   US Government Users Restricted Rights - Use, duplication or
#   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
#
################################################################################
#
# Name:
#       get_rc
#
# Description:
#	Gets the return code that was stored in the stderr output file
#       specified for the node specified.  The stderr output file is
#       of the format <prefix>.err and is created by cdsh.
#
# Arguments:
#       
#
# Return Values:
#       The return value of the AIX Command that is retrieved from the
#       stderr output file.
#
################################################################################
function get_rc
{
  typeset USAGE="USAGE: get_rc <prefix> <node>"

  # CHECK USAGE
  (( $# != 2 )) && {
    print "$USAGE"
  }

  typeset NODE ERR_FILE RC

  # CHECK FILE EXISTS
  ERR_FILE=$1.err
  NODE=$2

  [[ ! -f ${ERR_FILE} ]] && {
    nls_msg -2 0 0 "%s: get_rc: Unable to open file: %s\n" \
	"$_CMD" "${ERR_FILE}"
    return 1
  }

  # GET RETURN CODE
  # Due to the fact that some C-SPOC commands, like cl_lsvg, produce
  # multiple return codes for the same machine, make sure we only grab
  # the last return code for the node.
  RC=$(egrep "^${NODE}: RETURN_CODE=" ${ERR_FILE} | tail -1)
  RC=${RC#*=}
  [[ -z $RC ]] && RC=0

  # Display the return code to stdout.  This is necessary because Korn shell
  # seems to have a problem using "return" with codes that are larger than
  # 255 - for example, 1000 gets returned as 232.
  print $RC
  #return $RC
}

if [[ -n $@ ]]
then
    get_rc "$@"
fi
