#!/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

check_pmftag

check_state

case $TRIGGER_MONITOR in
        FILE|File|file)
		while read QUEUE
		do
		rc=0

		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
				continue
			else
				FLAG=
				if [ "$rc_check_pmftag" -eq 0 ]
				then
					case $rc_check_state in
					00)
						if [ "$STATE" = "ONLINEOK" ] || [ "$STATE" = "ONLINEFAULTED" ]
						then
							# For timing reasons only restart the trigger monitor 
							# if the start or stop method aren't currently running

							for i in start-trm stop-trm
							do
								if ps -u root -o pid,args | grep $i | grep -w $RESOURCE >/dev/null
								then
									FLAG=delay
								fi
							done

							if [ -z "$FLAG" ]
							then
                                                        	rc_check_qmgr_pids=
                                                        	rc_pid=

                                                        	# Here we simply check for the main processes

                                                        	for pid in amqzxma0 amqhasmx amqzllp0
                                                        	do
									if ps -u mqm -o pid,args | eval /usr/xpg4/bin/grep -E '$PATTERN' |\
										grep -w $pid > /dev/null
                                                                	then
                                                                        	rc_pid=0
                                                                	else
                                                                        	rc_pid=1
                                                                	fi

                                                                	rc_check_qmgr_pids=$rc_check_qmgr_pids$rc_pid
                                                        	done

                                                        	if [ "${rc_check_qmgr_pids}" != 000 ]
                                                        	then
                                                                	break
                                                        	fi

								start_trm_from_test $QUEUE
								log_message notice runmqtrm
                        				fi
						fi;;
		 			*)
						scds_syslog -p daemon.error -t $(syslog_tag) -m \
						"check_state - rc<%s>" \
						"${rc_check_state}"

						rc=50
						break;;
					esac
				else
					rc=100
					break
				fi
			fi
		fi

		done < `dirname $0`/../etc/${QMGR}_trm_queues
		;;
        *)
		# Although PMF will handle the process restart, this test is simply
		# performed to ensure the STATUS is set correctly.

		if ps -u mqm -o pid,args | eval /usr/xpg4/bin/grep -E '$PATTERN' |\
			grep -w runmqtrm | grep -w $TRIGGER_MONITOR > /dev/null
		then
			rc=0
		else
			rc=100
		fi
		;;
esac

if [ "$rc" -eq 0 ]
then
	set_status OK
fi

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