#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos72D src/bos/usr/lib/nim/methods/c_script.sh 1.14.1.3 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1993,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 
# @(#)47       1.14.1.3  src/bos/usr/lib/nim/methods/c_script.sh, cmdnim, bos72D, d2015_44A4  10/16/15  10:27:05
#   COMPONENT_NAME: CMDNIM
#
#   FUNCTIONS: ./usr/lib/nim/methods/c_script.sh
#
#
#   ORIGINS: 27
#
#
#   (C) COPYRIGHT International Business Machines Corp. 1993, 1996
#   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.

# include common NIM shell defines/functions
NIMPATH=${0%/*}
NIMPATH=${NIMPATH%/*}
[[ ${NIMPATH} = ${0} ]] && NIMPATH=/usr/lpp/bos.sysmgt/nim
NIM_METHODS="${NIMPATH}/methods"
export NIMPATH NIM_METHODS
. ${NIM_METHODS}/c_sh_lib

#---------------------------- local defines     --------------------------------

#---------------------------- module globals    --------------------------------
REQUIRED_ATTRS="location"
OPTIONAL_ATTRS=""
location=""
scriptname=""
global_exp=""
#*---------------------------- c_script           ----------------------------
#
# NAME: c_script
#
# FUNCTION:
#		executes the specified shell script
#
# EXECUTION ENVIRONMENT:
#
# NOTES:
#
# RECOVERY OPERATION:
#
# DATA STRUCTURES:
#		parameters:
#		global:
#
# RETURNS: (int)
#		0							= no errors
#		>0							= failure
#
# OUTPUT:
#-----------------------------------------------------------------------------*/

# signal processing
trap cleanup 0
trap err_signal 1 2 11 15

# NIM initialization
nim_init

# initialize local variables
source=
DBG=
rc=1

# set parameters from command line
while getopts :a:qv c
do
	case ${c} in

		a)		# validate the attr ass
				parse_attr_ass "${OPTARG}"

				# include the assignment for use in this environment
				eval ${variable}=\"${value}\"
		;;

		q)		# show attr info
				cmd_what
				exit 0
		;;

		v)		# verbose mode (for debugging)
				set -x
				for i in $(typeset +f)
				do
					typeset -ft $i
				done
				DBG=TRUE
		;;

		\?)	# unknown option
				error ${ERR_BAD_OPT} ${OPTARG}
		;;
esac
done

# check for missing attrs
ck_attrs

# NOTE: As part of the http availability feature -
#       the initial call will include a check for
#       web download service.. but not as part of
#       NIM_BOSINST_ENV condition.
if [[ -z ${NIM_BOSINST_ENV} && ${location} = ?*:/?* ]]
then
	# c_script should attempt to locate the file
	# using the nimtthp servicing option
	# Preference for ssl ..
	download_srvr=`echo ${location} | ${SED} 's/\:.*$//'`
	download_file=`echo ${location} | ${SED} 's/.*://'`
	download_loc=`echo ${download_file} | ${SED} 's./[^/]*$..'`
	${MKDIR} -p ${TMPDIR}/$download_loc
	${NIMHTTP} -i $download_srvr -f $download_file -o dest=${TMPDIR} -s >/dev/null 2>&1 ||
	${NIMHTTP} -i $download_srvr -f $download_file -o dest=${TMPDIR} >/dev/null 2>&1
	rc=$?

	if [[ $rc -eq 0 ]]; then
		${MV} ${TMPDIR}/$download_file ${TMPDIR}/script >/dev/null 2>&1
		${CHMOD} +x ${TMPDIR}/script >/dev/null 2>&1
		rc=$?

		# set new access point
		access_pnt="${TMPDIR}/script"
	fi
	# end of HTTP
fi

if [[ $rc -ne 0 ]]
then
	# NFS Method

	# If the nim_script resource is globally exported
	# then separate the directory from script filename
	# This is required since the nfs4 export string on
	# the master will only contain the parent dir

	if [[ ${location} = ?*:/export/nim/scripts/?*.script ]] && [[ ! -f ${LSNIM} ]]
	then
		# c_script was passed a nim_script resource,
		# check master for global_export
		global_exp=`${NIMCLIENT} -l -a global_export | ${AWK} '$1 ~ /global_export/ {print $3}'`
		if [[ $global_exp = "yes" ]]; then
			# cut up location into script name and dir name
			scriptname=`echo ${location} | ${SED} -e 's/^.*\///'`
			location=`echo ${location} | ${SED} 's./[^/]*$..'`
		fi
	fi

	# ensure local access to the script (local path returned via access_pnt)
	nim_mount ${location}

	# if scriptname was defined then append it to access_pnt path
	# since access_pnt points to a mounted directory
	if [[ -n "${scriptname}" ]]
	then
		script_access_pnt="${access_pnt}/${scriptname}"
	else
		script_access_pnt="${access_pnt}"
	fi

	# attempt to copy it
	if ${CP} ${script_access_pnt} ${TMPDIR}/script >/dev/null 2>&1
	then

		# make sure the script is executable
		if ${CHMOD} +x ${TMPDIR}/script >/dev/null 2>&1
		then

			# use local copy
			# unmount the source
			nim_unmount ${access_pnt}

			# set new access point
			access_pnt="${TMPDIR}/script"

		fi

	fi
	# end of NFS
fi

# Execute the script... If invoked from a 42+ master, and if called from
# the "nim_script" with the request to show output execute the script 
# appropriately.  Also follow this code path to perform logging if
# running in the BOS install environment.
if [[ -n ${NIM_BOSINST_ENV} || ${NIM_NON_41_MASTER} = yes && ${NIM_SCRIPT} = yes ]]
then

	${C_TIME_STAMP} >> ${SCRIPT_LOG}
	if [[ ${NIM_SHOW_PROGRESS} = yes ]]
	then
		# run the command sending all output to log AND to
		# screen (stdout)
		# NOTE: Since we are piping output to "tee", we
		#       resort to sending the return code from the
		#       command to a file in a sub-shell then
		#       retrieving it for use later on.
		(
		  ${access_pnt}
		  echo $? > ${TMPDIR}/_script.rc 
		) 2>&1 | ${TEE} -a ${SCRIPT_LOG}
		rc=`${CAT} ${TMPDIR}/_script.rc`
		/usr/bin/rm ${TMPDIR}/_script.rc
	else
		${access_pnt} >> ${SCRIPT_LOG} 2>&1
		rc=$?
	fi
	${C_TIME_STAMP} -e >> ${SCRIPT_LOG}

	# If an error occurred, it was logged.  Print a message
	# referring the user to the log for information.
	if [[ $rc -ne 0 ]]
	then
	  ${C_ERRMSG} ${MSG_SEE_LOG_FILE} \
	              ${C_ERRMSG_MSG} "${SCRIPT_LOG}" \
	              "" "" "" >${ERR} 2>&1

	  err_from_cmd ${location}
	fi

elif ${access_pnt} 2>${ERR}
then

	if [[ -n "${DBG}" ]]
	then

		${CAT} ${ERR} 1>&2

	fi

else

	# one or more failures encountered inside of the script
	err_from_cmd ${location}

fi

# success
exit 0

