#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/events/utils/cl_tape_resource_release_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 
# @(#)85  1.8  src/43haes/usr/sbin/cluster/events/utils/cl_tape_resource_release_multi.sh, hacmp.sharedtape, 61haes_r714 2/28/03 16:21:52

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

#########################################################################
#									#
#	Name:		cl_tape_resource_release_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 stop script (if any)	#
#			    exists and is usable.			#
#			 o  Invoke an auxiliary script to:		#
#			     - run the stop script (if any)		#
#			     - release the device			#
#			    syncronously or asyncronously,		#
#			    as appropriate.				#
#									#
#	Called by:							#
#			.../events/node_down_local			#
#			.../events/node_up_remote			#
#									#
#	Calls to:	cl_tape_resource_release			#
#									#
#	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 6241 "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_releasing $ALLTAPE $PROGNAME

for TAPE_RESOURCE in $TAPE_RESOURCE_LIST
do

	# Get the tape resource's device name,
	# find out whether device should be stopped a/syncronously,
	# and what, if any, stop script should be used.
	#
	cllstape -c -n $TAPE_RESOURCE |\
	cut -f 3,5,7 -d: | tr ':' ' ' |\
	read TAPE_DEVICE_NAME STOP_SYNC STOP_SCRIPT

	# If a stop 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 "$STOP_SCRIPT" ]
	   then
		sts=0
		if [ ! -e $STOP_SCRIPT ]
		   then	cl_log 6242 \
		   	"stop script '${STOP_SCRIPT}' does not exist" \
					$STOP_SCRIPT
			STOP_SCRIPT=""
			sts=1
			fi
		if [ ! -x $STOP_SCRIPT ]
		   then	cl_log 6243 \
		   	"stop script '${STOP_SCRIPT}' is not executable" \
					$STOP_SCRIPT
			STOP_SCRIPT=""
			sts=1
			fi
		if [ -b $STOP_SCRIPT -o -c $STOP_SCRIPT -o -d $STOP_SCRIPT ]
		   then	cl_log 6244 \
		   	"stop script '${STOP_SCRIPT}' is not a regular file" \
					$STOP_SCRIPT
			STOP_SCRIPT=""
			sts=1
			fi
		if [ ! -s $STOP_SCRIPT ]
		   then	cl_log 6245 \
		   	"stop script '${STOP_SCRIPT}' is empty" \
					$STOP_SCRIPT
			STOP_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_release
	#	  with the tape device name and the stop script name.
	#	*Wait* for cl_tape_resource_release 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_release
	#	  with the tape device name and the stop 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 [ "${STOP_SYNC}" -eq 1 ]
	   then	cl_tape_resource_release  $TAPE_DEVICE_NAME  $STOP_SCRIPT
		sts=$?
		if [ $sts -ne 0 ]
		   then cl_log 6246 \
		   	"cl_tape_resource_release for Tape Resource '${TAPE_RESOURCE}' returned ${sts}" \
				$TAPE_RESOURCE  $sts
			STATUS=$sts
			fi
	   else	cl_tape_resource_release  $TAPE_DEVICE_NAME  $STOP_SCRIPT  &
		sts=$?
		if [ $sts -ne 0 ]
		   then cl_log 6247 \
		   	"Could not spawn cl_tape_resource_release for Tape Resource '${TAPE_RESOURCE}'" \
				$TAPE_RESOURCE
			STATUS=$sts
			fi
	   fi
done

ALLNOERRTAPE="All_nonerror_tape_resources"
cl_RMupdate resource_down $ALLNOERRTAPE $PROGNAME

exit $STATUS
