#!/usr/bin/ksh
#
# Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

typeset opt

while getopts 'R:G:Q:M:U:' opt
do
	case "$opt" in
		R)      RESOURCE=$OPTARG;;
                G)	RESOURCEGROUP=$OPTARG;;
		Q)      QMGR=$OPTARG;;
		M)	TRIGGER_MONITOR=$OPTARG;;
		U)	USERID=$OPTARG;;
                *)	logger -p daemon.err \
                        "ERROR: `basename $0` Option $OPTARG unknown"
                        exit 1;;
	esac
done
    
. `dirname $0`/../etc/config
. `dirname $0`/functions

validate_options

debug_message "Method: `basename $0` - Begin"
$SET_DEBUG

rm $LOGFILE 2>/dev/null

validate

if [ "$rc_validate" -ne 0 ]
then
        debug_message "Method: `basename $0` - End (Exit 1)"
        exit 1
fi

check_qmgr

# To accomodate multiple trigger monitor entries and therefore multiple process 
# trees we need to ensure that whenever the START method is called all the process
# ids associated with this resource are stopped, so that we can ensure they 
# are (re) started under the pmftag $RESOURCEGROUP,$RESOURCE,0

# Stop any running trigger monitors

case $TRIGGER_MONITOR in
	FILE|File|file)
		while read QUEUE
		do
			if echo $QUEUE | grep '^#' > /dev/null
			then
				continue
			else
				if ps -u mqm -o pid,args | eval /usr/xpg4/bin/grep -E '$PATTERN' |\
					grep -w runmqtrm | grep -w $QUEUE > /dev/null
				then
					stop_trm $QUEUE
				fi
			fi

		done < `dirname $0`/../etc/${QMGR}_trm_queues
		;;
	*)
		if ps -u mqm -o pid,args | eval /usr/xpg4/bin/grep -E '$PATTERN' |\
			grep -w runmqtrm | grep -w $TRIGGER_MONITOR > /dev/null
		then
			stop_trm $TRIGGER_MONITOR
		fi
		;;
esac

# Start the trigger monitors

case $TRIGGER_MONITOR in
	FILE|File|file)
		while read QUEUE
		do
			if echo $QUEUE | grep '^#' > /dev/null
			then
				continue
			else
				start_trm $QUEUE
				log_message notice runmqtrm
			fi

		done < `dirname $0`/../etc/${QMGR}_trm_queues
		;;
	*) 	
		start_trm $TRIGGER_MONITOR
		log_message notice runmqtrm
		;;
esac

debug_message "Method: `basename $0` - End (Exit 0)"
exit 0
