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

typeset opt

while getopts 'R:G:Q:P:B:U:' opt
do
	case "$opt" in
		R)      RESOURCE=$OPTARG;;
                G)	RESOURCEGROUP=$OPTARG;;
		Q)      QMGR=$OPTARG;;
                P)	PORT=$OPTARG;;
                B)	BACKLOG=$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

rc=0

check_pmftag

check_state

for port in $PORT_LIST
do
	if ps -u mqm -o pid,args | eval /usr/xpg4/bin/grep -E '$PATTERN' |\
		grep -w runmqlsr | grep "\-p ${port}" > /dev/null
	then
		continue
	else
		# To accomodate multiple port entries within a resource
		#
		# If the pmftag is not running - exit 100
		# If the resouce STATE and STATUS are ONLINE and OK or FAULTED
		# If the LSR is stopping/restarting - delay
		# If the main QMGR pids are not found - break
		#
		#  Otherwise we allow this script to start (outside of pmf)
		#  a failed listener.
		
		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 listener if the
                                                # start or stop method aren't currently running

                                                for i in start-lsr stop-lsr
                                                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=

                                                	# 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_lsr_from_test $port

                        				# runmqlsr will only ouput error messages, which is
                        				# why log_message outputs with priority error.
                        				# Function log_message only sends a message to syslog if
                        				# $LOGFILE is not empty.

                        				log_message error runmqlsr
                                                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
done

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

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