#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/lib/nim/methods/c_nimpush.sh 1.17 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1993,2009 
# 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 
# @(#)17        1.17  src/bos/usr/lib/nim/methods/c_nimpush.sh, cmdnim, bos720  1/21/09  10:57:10
#
#   COMPONENT_NAME: CMDNIM
#
#   FUNCTIONS: ./usr/lib/nim/methods/c_nimpush.sh
#
#   ORIGINS: 27
#
#
#   (C) COPYRIGHT International Business Machines Corp. 1993
#   All Rights Reserved
#   Licensed Materials - Property of IBM
#   US Government Users Restricted Rights - Use, duplication or
#   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
#

#---------------------------- nimpush.sh        --------------------------------
#
# NAME: nimpush.sh
#
# FUNCTION:
#		a ksh script which is used as a wrapper around commands which are remotely
#			executed by the NIM master
#		this command is a wrapper because the NIM remote execution mechanism
#			requires the return code from the command which nimpush will execute
#			as the first thing on stderr, and, it expects it in a specific format:
#				"rc=<exit code>"
#		nimpush accomplishes this by capturing the command's stderr output & its
#			exit status
#
# EXECUTION ENVIRONMENT:
#		nimpush is a ksh script and is invoked by the rshd, which gets called
#			from the NIM master by the nimexec function
#
# NOTES:
#
# RECOVERY OPERATION:
#
# DATA STRUCTURES:
#		parameters:
#			1 						= command to execute
#			2 ->					= args to the command
#		global:
#
# RETURNS: (int)
#
# OUTPUT:
#		writes "rc=<exit code>" as the first thing on stderr; this is followed by
#			any stderr output from the command which nimpush executes
#-------------------------------------------------------------------------------

# set environment defaults
umask 022
PATH=$PATH:/usr/bin:/usr/sbin
export PATH

# initialize local variables
rc=0

# make sure a command was specified
if [[ -z "$1" ]]
then
	print "rc=1 nimpush: no command was specified" 1>&2
	exit 1
else
	cmd=$1
	shift
fi
/usr/bin/rm -fr /tmp/_nim_$$
# make sure command is found & can be executed
exit_status=""
path=$(whence $cmd 2>/tmp/_nim_$$)
if [[ -n "$path" ]]
then

	if [[ -x $path ]] && [[ ! -d $path ]]
	then

		# execute the specified command & direct its stderr into a tmp file
		oldIFS=$IFS
		IFS="	"
		$cmd $@ 2>/tmp/_nim_$$
		rc=$?
		IFS=$oldIFS
		exit_status="rc=$rc"

		# any stderr output?
		if [[ -s /tmp/_nim_$$ ]]
		then

			# is "rc=[0-9]" already part of the stderr output?
			if [[ -z "$(/usr/bin/egrep ^rc=[0-9]+ /tmp/_nim_$$)" ]] ; then
				print ${exit_status} 1>&2
                        else 
			   # is "rc=[0-9]" first line in stderr output?  If it is not,
                           # move it to the top of the file  (defect 666462)

                           /usr/bin/awk '
                                                    # if found "rc=number", jump to "END"

                                            /^rc=[0-9]+/  {  exit  }        

                                                    # print "rc=number" to 2nd temp file, if it
                                                    # is not at the beginning of error text 

                                                END       {  if ( NR > 1 ) print $0 }

                                        ' /tmp/_nim_$$ > /tmp/_2nim_$$

                           # if 2nd temp file exists, and its size is greater than zero
                           
                           if [[ -s /tmp/_2nim_$$ ]]
                           then
                                 # append content of original error to 2nd temp file  
                              /usr/bin/cat /tmp/_nim_$$ >> /tmp/_2nim_$$

                                 # copy content of 2nd tmp file to original error 
                                 # with "rc=number" at the top!

                              /usr/bin/cat /tmp/_2nim_$$ > /tmp/_nim_$$ 
                           fi
           
                           # remove the second tmp file
                           /usr/bin/rm  -f /tmp/_2nim_$$ >/dev/null 2>&1
                        fi

			# The following test statement was added for defect 508385.
			# Creating a mksysb from c_mkbosi returns a successful return code but
			# printed "cannot access" to stderr.  NIM checks for stderr and immediately
			# assumed the operation failed even when the mksysb actually created 
			# successfully.  Now, when we create a mksysb, we only print to stdout, 
			# but still pass the return code along.
			if [[ "${cmd}" = "/usr/lpp/bos.sysmgt/nim/methods/c_mkbosi" ]]; then
				# write the stderr output to stdout for mksysb creation
                                # if c_mkbosi exited without error! 
                                if [[ $rc == 0 ]]
                                then
				  /usr/bin/cat /tmp/_nim_$$
                                else
				  # write the stderr output  (defect 671501)
				  /usr/bin/cat /tmp/_nim_$$ 1>&2
                                fi
                                
			else
				# write the stderr output
				/usr/bin/cat /tmp/_nim_$$ 1>&2
			fi

		else

			# just write the exit status
			print ${exit_status} 1>&2

		fi

	else

		# command not executable
		print "rc=1 nimpush: $cmd not executable" 1>&2
		rc=1

	fi

else

	# command not found
	print "rc=1 nimpush: $cmd not found" 1>&2
	rc=1

fi

# remove the tmp file
/usr/bin/rm /tmp/_nim_$$ >/dev/null 2>&1

# bye-bye
exit $rc

