#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/events/utils/cl_tape_resource_get_multi.sh 1.8 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1999,2003 
# 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 
# @(#)83  1.8  src/43haes/usr/sbin/cluster/events/utils/cl_tape_resource_get_multi.sh, hacmp.sharedtape, 61haes_r714 2/28/03 16:19:12

#########################################################################
#
#   COMPONENT_NAME: EVENTS
#
#   FUNCTIONS: none
#
#########################################################################

#########################################################################
#									#
#	Name:		cl_tape_resource_get_multi			#
#									#
#	Description:	This script is called				#
#			in order to do the following for each tape	#
#			resource in a resource group:			#
#			 o  Make sure that the start script (if any)	#
#			    exists and is usable.			#
#			 o  Invoke an auxiliary script to:		#
#			     - reserve the device			#
#			     - run the start script (if any)		#
#			    syncronously or asyncronously,		#
#			    as appropriate.				#
#									#
#	Called by:							#
#			.../events/node_down_remote			#
#			.../events/node_up_local			#
#									#
#	Calls to:	cl_tape_resource_get				#
#									#
#	Arguments:	tape_resource_list				#
#									#
#			The tape_resource_list is just a		#
#			blank-separated	list of tape resources		#
#			to be reserved.					#
#									#
#	Returns:	0	success					#
#			1	failure					#
#			2	bad argument				#
#									#
#########################################################################

#########################################################################
#
# Main Starts Here
#
#########################################################################

[ "$VERBOSE_LOGGING" = "high" ] && set -x
PROGNAME=$(basename ${0})
PATH="$($(dirname ${0})/../../utilities/cl_get_path all)"
HA_DIR="$(cl_get_path)"

export PATH

STATUS=0

if [ $# -ne 1 ]
then
    cl_echo 6231 "Usage: $PROGNAME tape_resource_list\n" $PROGNAME
    exit 2
fi

PROC_RES=false

# if JOB_TYPE is set, and it doesn't equal to "GROUP", then
# we are processing for process_resources
if [[ ${JOB_TYPE:-0} != 0 && $JOB_TYPE != "GROUP" ]]; then
   PROC_RES=true
fi

set -u

TAPE_RESOURCE_LIST=$1

# update resource manager with this action
ALLTAPE="All_tape_resources"
cl_RMupdate resource_acquiring $ALLTAPE $PROGNAME

for TAPE_RESOURCE in $TAPE_RESOURCE_LIST
do
	# update resource manager with this action
	cl_RMupdate resource_acquiring $TAPE_RESOURCE $PROGNAME

        # Get the tape resource's device name,
        # find out whether device should be started a/syncronously,
        # and what, if any, start script should be used.
        #
	cllstape -c -n $TAPE_RESOURCE |\
	cut -f 3,4,6 -d: | tr ':' ' ' |\
	read TAPE_DEVICE_NAME START_SYNC START_SCRIPT

	# If a start script was specified for this device,
	# make sure that it:
	#	exists,
	#	is executable,
	#	is not a char- or block-special file or a directory,
	#	and contains at least *something*.
	# If not,
	#	log an error,
	#	set a flag
	#	  to indicate that an error occurred in processing
	#	  at least one of the specified tape resources,
	#	and skip further processing for this tape resource.
	#
	if [ ! -z "$START_SCRIPT" ]
	   then
		sts=0
		if [ ! -e $START_SCRIPT ]
		   then	cl_log 6232 \
		   	"start script '${START_SCRIPT}' does not exist" \
					$START_SCRIPT
			START_SCRIPT=""
			sts=1
			fi
		if [ ! -x $START_SCRIPT ]
		   then	cl_log 6233 \
			"start script '${START_SCRIPT}' is not executable" \
					$START_SCRIPT
			START_SCRIPT=""
			sts=1
			fi
		if [ -b $START_SCRIPT -o -c $START_SCRIPT -o -d $START_SCRIPT ]
		   then	cl_log 6234 \
		   	"start script '${START_SCRIPT}' is not a regular file" \
					$START_SCRIPT
			START_SCRIPT=""
			sts=1
			fi
		if [ ! -s $START_SCRIPT ]
		   then	cl_log 6235 \
		   	"start script '${START_SCRIPT}' is empty" \
					$START_SCRIPT
			START_SCRIPT=""
			sts=1
			fi
		if [ $sts -ne 0 ]
		   then	
			if [[ $PROC_RES = true ]]; then
			    STATUS=11
			else
			    STATUS=1
			fi

			cl_RMupdate resource_error $TAPE_RESOURCE $PROGNAME
		   	continue
		   fi
	   	fi

	# If syncronous operation is desired for this resource:
	#	Invoke cl_tape_resource_get
	#	  with the tape device name and the start script name.
	#	*Wait* for cl_tape_resource_get to finish.
	#	If that script was unsuccessful,
	#		log an error and set a flag
	#		to indicate that an error occurred in processing
	#		at least one of the specified tape resources.
	# BUT --
	# If A-syncronous operation is desired for this resource:
	#	Spawn cl_tape_resource_get
	#	  with the tape device name and the start script name.
	#	If *invocation* was unsuccessful,
	#		log an error and set a flag
	#		to indicate that an error occurred in processing
	#		at least one of the specified tape resources.
	#
	#	In any case, go on to attempt processing for the others.
	#
	if [ "${START_SYNC}" -eq 1 ]
	   then	cl_tape_resource_get  $TAPE_DEVICE_NAME  $START_SCRIPT
		sts=$?
		if [ $sts -ne 0 ]
		   then cl_log 6236 \
		   	"cl_tape_resource_get for Tape Resource '${TAPE_RESOURCE}' returned ${sts}" \
				$TAPE_RESOURCE  $sts

			STATUS=$sts

			fi
	   else	
		cl_tape_resource_get  $TAPE_DEVICE_NAME  $START_SCRIPT  &
		sts=$?
		if [ $sts -ne 0 ]
		   then cl_log 6237 \
		   	"Could not spawn cl_tape_resource_get for Tape Resource '${TAPE_RESOURCE}'" \
				$TAPE_RESOURCE
			STATUS=$sts
			
			fi
	   fi
done

ALLNOERRTAPE="All_nonerror_tape_resources"
cl_RMupdate resource_up $ALLNOERRTAPE $PROGNAME

exit $STATUS
