#!/bin/sh
#
# bb-page.sh
#
# BIG BROTHER PAGER SCRIPT (BBWARN)
# Robert-Andre Croteau
# Version 1.4
# Mar 05th, 2000
#
# This program is Copyright (c) 1997-2000
# Robert-Andre Croteau, The MacLawran Group Inc.
# All Rights Reserved
#
# Format: bb-page.sh <NUMERIC-MESSAGE|HOST.SVC TAG> <ALPHA-MESSAGE>
# i.e. : bb-page.sh 10010101010 "blah ...."
#	 bb-page.sh host10,domain,com,disk "blah ..."
#

# BIG BROTHER MONITORING SCRIPT
#
# For each entry in the $BBHOME/etc/bbwarnrules.cfg
# that matches the host in the 1st argument, send a notification
# to the specified recipients

# The sendmsg script has been modified to accept more than one 
# modem line (it cascades to the next one if it is unavailable)

# !!!!!!  READ THIS !!!!!!
#
# If you are still using pre-1.08b BB on your U*X clients or haven't
# set BBWARN notification style in BBNT then you may get erreonous reports
#
# example:
# received notification from  10.1.1.11 (101111) but you
# have these hosts defined in bb-hosts: 10.1.1.11 (101111) and 10.11.1.1 (101111)
# As you can see, removing the '.' yields the same number.  So you will
# always match on the first one :-(.  When you'll upgrade all of your
# clients with bb.c) then this type of error will disappear because all
# notification messages are received by their FQDN values not digits as above
# You can get bypass this error by filling your IP addresses in bb-hosts
# (on all of your hosts !)
# 10.1.1.11 is defined as 010.001.001.011
# 10.11.1.1 is defined as 010.011.001.001


# Set up info if message came from pre-1.08b client

getoldinfo ( ) {

	$GREP "^[0-9]" $BBHOSTS | \
	while read hostline
	do
		# The IFS is still '.' change it back
		IFS=$OLDIFS
		set $hostline
		if [ "$#" -lt 2 ]
		then
			continue	# bad host line
		fi
		IPADDR=$1
		BBHOSTNAME=$2
		IFS='.'
		if [ "$FQDN" != "TRUE" ]
		then
			set $BBHOSTNAME
			BBHOSTNAME=$1
		fi
		set $IPADDR
		IFS=$OLDIFS
		IPADDR=${1}${2}${3}${4}
		# Does the IP address match ?
		echo $TARGET | $EGREP -x ".*$IPADDR" >/dev/null 2>&1
		if [ "$?" -eq 0 ]
		then
			IFS=$OLDIFS
			CODE=`echo $TARGET | $SED "s/$IPADDR//"`
			SVCCODE=""
			echo $TARGET | $GREP "^${NETERR}" >/dev/null 2>&1
			if [ "$?" -eq 0 ]
			then
				# Search for network code
				CODE=`echo $CODE | $SED "s/^${NETERR}//"`
				svcline=`$GREP ".*[ 	]${CODE}/tcp" /etc/services`
				if [ "$?" -eq 0 ]
				then
					set $svcline
					SVCCODE=$1
				fi
			else
				# it's a regular code
				# Find the matching service and code
				for svc in $SVCERRLIST
				do
					IFS=':'
					set $svc
					IFS=$OLDIFS
					if [ "$CODE" = "$2" ]
					then
						SVCCODE=$1	
						break
					fi
				done
			fi
			if [ -z "$SVCCODE" ]
			then
				SVCCODE="ERR"
			fi
			redhost=$BBHOSTNAME
			redsvc=$SVCCODE
			MACHSRV=`echo $BBHOSTNAME | $SED 's/\./,/g'`.${SVCCODE}
			echo "$MACHSRV $redhost $redsvc"
			break	# we have a match out of loop
		fi
	done
}

# Set up info if message came from a 1.08b or newer release

gethostinfo ( ) {
	file=$1
	while read hostline
	do
		set $hostline
		# Make sure it's a valid line (well at least 2 args)
		if [ "$#" -ge "2" ]
		then
			IPADDR=$1
			if test "$FQDN" != "TRUE"
			then
				# If the FQDN is specified in bb-host but only
				# the non-FQDN is returned by message
				OLDIFS=$IFS
				IFS='.'
				set $2
				IFS=$OLDIFS
				hostname=$1
			else
				hostname=$2
			fi
			# This test is really required for non-FQDN, it makes sure
			# that a host called "red" will no match a host called "redhat"
			if [ "$redhost" = "$hostname" ]
			then
				MACHIP=`echo $IPADDR | $SED 's/\./ /g' | $AWK '{printf("%03d%03d%03d%03d",$1,$2,$3,$4)}'`
				if [ -n "$CODE" ]
				then
					SVCERR="$CODE"
				else
					for svcerr in $SVCERRLIST  # Get proper code
					do
						OLDIFS=$IFS
						IFS=':'
						set $svcerr
						IFS=$OLDIFS
						if [ "$1" = "$redsvc" ]
						then
							SVCERR=$2
							break
						fi
					done
					if [ -z "$SVCERR" ]
					then
						# [ 	] is really a space
						# and a tab, do not remove !
						SVCERR=`$GREP "^${redsvc}[ 	]" /etc/services | $SED 's/\// /g' | $AWK '{print ENVIRON["NETERR"] $2}'`
					fi
					if [ -z "$SVCERR" ]
					then
						SVCERR="999"
					fi
				fi
				echo "$MACHIP $SVCERR"
				break	# we have a match, get out of loop
			fi
		fi
	done < $file
}


if test "$#" != "1"
then
    echo "bb-page.sh: wrong argument count - $#"
    exit 1
else

    ORIGMSG="$1"		# Save message to keep newlines before
				# a set is done on it ...

    # Pick the machine.service combination from the msg
    set -f		#disable globber
    set $1
    set +f		# reenable globber
    if test "$#" -lt "2"
    then
	echo "bb-page.sh: wrong argument count within message"
	exit 2
    fi

    # ORIGHOST is used to save original host name to be replaced
    # just before being passwd to bb-page1.sh

    ORIGHOST="$1"
    MACHSRV=`echo "$1" | $SED 's/[^A-Za-z0-9.\,_-]//g'`	# i.e. hp1.cpu hp1,domain,com.cpu
    TARGET=$MACHSRV		# save 1st arg

    # Don't do any notification on summaries
    echo "$MACHSRV" | $GREP "^summary\."  >/dev/null 2>&1 
    if [ "$?" -eq 0 ]
    then
	exit 0
    fi

    # Check if notification comes from the paging web page
    # host.service tag is notify-admin.pagehelp
    if [ "$MACHSRV" = "notify-admin.pagehelp" ]
    then
	PAGEHELPUSERNUM="$2"	# Save the user number to call back
    fi

    # Should you notify and not create any np_* tag files ?
    # used when color has become normal state again
    if [ "$2" = "recovered" ]
    then
	RECOVERED=TRUE		# Just notify
    else
	RECOVERED=FALSE	# create the np_* file
    fi
    export RECOVERED

    #
    # FIND OUT IF NOTIFICATIONS ARE ENABLED
    # 

    BBWARN="TRUE"			# default value 
    bbwarnline=`$GREP "^bbwarn:" $BBHOME/etc/bbwarnsetup.cfg` 2>/dev/null
    if [ "$?" -eq 0 ]
    then
        set $bbwarnline >/dev/null 2>&1
        if [ "$#" -gt 1 ]
        then 
                shift           # remove bbwarn token from line
                BBWARN="$1"
        fi
    fi

    if [ "$BBWARN" != "TRUE" ]
    then
	exit 0				# No warning to be issued
    fi

    #
    # GET THE BBWARN RULES CONFIGURATION FILE DELIMITER
    # 

    CFGDELIM=";"			# default value 
    cfgdelimline=`$GREP "^cfgdelim:" $BBHOME/etc/bbwarnsetup.cfg` 2>/dev/null
    if [ "$?" -eq 0 ]
    then
        set $cfgdelimline >/dev/null 2>&1
        if [ "$#" -gt 1 ]
        then 
                shift           # remove cfgdelim token from line
                CFGDELIM="$1"
        fi
    fi

    #
    # GET THE SERVICE:ERROR TAGS
    # 

    # default values 
    SVCERRLIST="disk:100 cpu:200 procs:300 msgs:400 conn:500 http:600 dns:800 ERR:999"
    svcerrlistline=`$GREP "^svcerrlist:" $BBHOME/etc/bbwarnsetup.cfg` 2>/dev/null
    if [ "$?" -eq 0 ]
    then
        set $svcerrlistline >/dev/null 2>&1
        if [ "$#" -gt 1 ]
        then 
                shift           # remove cfgdelim token from line
                SVCERRLIST="$*"
        fi
    fi

    #
    # Get the code for required help when user uses HTML form 
    #    to page the admin(s)
    #    

    PAGEHELPCODE="911"                       # default value
    pagehelpcodeline=`$GREP "^pagehelpcode:" $BBHOME/etc/bbwarnsetup.cfg` 2>/dev/null
    if [ "$?" -eq 0 ]
    then
	set $pagehelpcodeline >/dev/null 2>&1
	if [ "$#" -gt 1 ]
	then
	    shift           # remove pagehelpcode token from line
	    PAGEHELPCODE="$1"
	fi
    fi
    export PAGEHELPCODE

    IPADDR=""
    CODE=""
    OLDIFS=$IFS
    IFS='.'
    set $TARGET
    IFS=$OLDIFS
    if [ "$#" -eq 1 ]	# old style returns 40090909090(example)  as 1st arg
    then
        OLDSTYLEPAGEMSG="Y"
	# We presume this is old style message
	# Rebuild into a new BBWARN style message
	# host.service (host,domain.service) is the 1st argument
	# We have to rebuild the IP address without '.' and match with
	# the incoming

	hostinfo=`getoldinfo`

	# Was a match found ?
	if [ -n "$hostinfo" ]
	then
		set $hostinfo
		MACHSRV=$1
		redhost=$2
		redsvc=$3
	else
	    # An unmatched host may occur when the definition in the
	    # foreign bb-hosts file is not the same as the local one
	    # i.e 10.2.4.5  and 10.2.4.05
	    # one produces 10245 and the other 102405
	    redsvc="ERR"
	    redhost="unmatched-${TARGET}"
	    MACHSRV="$redhost.$redsvc"
	fi
    else
	redhost=`echo $1 | $SED 's/,/\./g'`
	redsvc=$2
        OLDSTYLEPAGEMSG="N"
    fi
    IFS=$OLDIFS
fi
if test ! "$BBTMP"                      # GET DEFINITIONS IF NEEDED
then
	# echo "*** LOADING BBDEF ***"
        . $BBHOME/etc/bbdef.sh          # INCLUDE STANDARD DEFINITIONS
fi

if [ ! -f "$BBHOME/etc/bbwarnrules.cfg" ]
then
	echo "bb-page.sh: bbwarnrules.cfg not found <$BBHOME/etc/bbwarnrules.cfg>"
	exit 1
fi

if [ ! -f "$BBHOME/etc/bbwarnsetup.cfg" ]
then
	echo "bb-page.sh: bbwarnsetup.cfg not found <$BBHOME/etc/bbwarnsetup.cfg> using defaults"
fi

set -f		# disable globber

ignline=`$GREP "^ignforall:" $BBHOME/etc/bbwarnsetup.cfg` 2>/dev/null
if [ "$?" -eq 0 ]
then
	set $ignline
	shift			# skip over ignforall: token
	ignhosts=$*
	echo $MACHSRV | $EGREP -x "$ignhosts" >/dev/null 2>&1
	if [ "$?" -eq 0 ]
	then
		exit 0		# matched the egrep regexp, exit
	fi
fi

set +f		# enable globber (just in case)

#cd $BBHOME/www/logs/

# date is 0-6 (sunday-saturday)
nowday=`$DATE +"%w"`
# time is 0000-2359
nowtime=`$DATE +"%H%M"`

# Get IP address of host (formatted) and service code
SVCERR=""
$GREP "^[0-9][0-9\.]*[ 	][ 	]*${redhost}$" $BBHOSTS > $BBTMP/BBWARN.$$ 2>/dev/null
if [ "$?" -ne 0 ]
then
	$GREP "^[0-9][0-9\.]*[ 	][ 	]*${redhost}[\. 	].*$" $BBHOSTS > $BBTMP/BBWARN.$$ 2>/dev/null
fi
if [ "$?" -eq 0 ]
then
	hostinfo=`gethostinfo $BBTMP/BBWARN.$$`
	if [ -n "$hostinfo" ]
	then
		set $hostinfo
		MACHIP=$1
		SVCERR=$2
	fi
else
	# Is the unmatched a cry for help from the HTML form ?
	if [ "$redhost" != "notify-admin" ]
	then
		# Our unmatched host will make it here and the 
        	if [ "$OLDSTYLEPAGEMSG" = "Y" ]
		then
		    MACHIP=""
		    SVCERR="$TARGET"
		    # redhost & redsvc are already set from gethostinfo failure ...
		else
		    # you have an unmatched when the hostname is not found in bb-hosts
		    redhost="unmatched-${redhost}"
		    MACHSRV="$redhost.$redsvc"
		    MACHIP="999999999999"
		    SVCERR="999"
		fi
	else
		# This is a user help request from HTML form
		SVCERR="$PAGEHELPCODE"
		MACHIP="$PAGEHELPUSERNUM"
	fi
fi

$RM -f $BBTMP/BBWARN.$$

	if [ -n "$SVCERR" ]
	then
		tmppage=$BBTMP/bb-page.$$	
		tmpnopage=$BBTMP/bb-nopage.$$	
		$RM -f $tmppage
		$RM -f $tmpnopage

		$CAT $BBHOME/etc/bbwarnrules.cfg | $GREP -v "^#" | $SED "s/${CFGDELIM}${CFGDELIM}/${CFGDELIM} ${CFGDELIM}/g" | \
		while read line
		do
			if [ -z "$line" ]
			then
				continue
			fi
			set -f 		# disable globber we may have a * in the line

			# For unmatched records and HTML form paging just match on lines
			# that start with "unmatched-" and "notify-admin" respectively
			doline=0
			case "$redhost" in

				unmatched-* )
					case "$line" in
						unmatched-* )
							;;
						*)
							doline=1
							;;
					esac
					;;

				notify-admin )
					case "$line" in
						notify-admin* )
							;;
						*)
							doline=2
							;;
					esac
					;;

				* )
					;;
			esac


			# I used a variable 'cause I don't trust shell returning to the
			# proper loop within embedded case statements ...
			if [ "$doline" -ne 0 ]
			then
				set +f
				continue
			fi

			IFS=$CFGDELIM
			set $line
			IFS=$OLDIFS

			# eval is used to remove unwanted spaces/tabs/...
                        arg1=`eval "echo $1"`
                        arg2=`eval "echo $2"`
                        arg3=`eval "echo $3"`
                        arg4=`eval "echo $4"`
                        days=`eval "echo $5"`
                        times=`eval "echo $6"`
                        PAGERS=`eval "echo $7"`
			# On some shells, the ^ escalation tag will make the eval fail :(
			if [ "$?" -ne 0 ]
			then
				PAGERS="$7"
			fi

                        if [ "$arg1" != "" ]
                        then
				ignore=FALSE
				# !line indicates recipients to ignore if it matches
				# remove ! from line and handle like normal rule line
				tmparg1=`echo "$1" | $SED 's/\!//1'`
				if [ "$tmparg1" != "$1" ]
				then
					ignore=TRUE
					arg1="$tmparg1"
				fi

				HOSTS=""
				for host in $arg1
				do
					case $host
					in
						hg-* )
							hgline=`$GREP "^$host:" $BBHOME/etc/bbwarnsetup.cfg` 2>/dev/null
							if [ "$?" -eq 0 ]
							then
							        set $hgline >/dev/null 2>&1
							        if [ "$#" -gt 1 ]
							        then
						        	        shift           # remove hg- token from arg list
						                	HOSTS="$HOSTS $*"
						        	fi
							fi      
							;;

						*)
							HOSTS="$HOSTS $host"
							;;
					esac
				done
				arg1="$HOSTS"

				hostsdef=`echo $arg1 | $SED 's/*/.*/g'`
				hosts=""
                                for host in $hostsdef
                                do
                                        if [ "$hosts" = "" ]
                                        then
                                                hosts="$host"
                                        else
                                                hosts="$hosts|$host"
                                        fi
                                done
                        else
                                echo "bbwarnrules.cfg: Invalid entry in the file was found" 
                                echo "            Empty hosts column entry"
				set +f 		# reenable globber
                                continue
                        fi

			if [ "$arg2" != "" ]
			then
				HOSTS=""
				for host in $arg2
				do
					case $host
					in
						hg-* )
							hgline=`$GREP "^$host:" $BBHOME/etc/bbwarnsetup.cfg` 2>/dev/null
							if [ "$?" -eq 0 ]
							then
							        set $hgline >/dev/null 2>&1
							        if [ "$#" -gt 1 ]
							        then
						        	        shift           # remove hg- token from arg list
						                	HOSTS="$HOSTS $*"
						        	fi
							fi      
							;;

						*)
							HOSTS="$HOSTS $host"
							;;
					esac
				done
				arg2="$HOSTS"

				exhostsdef=`echo $arg2 | $SED 's/*/.*/g'`
				exhosts=""
				for exhost in $exhostsdef
				do
					if [ "$exhosts" = "" ]
					then
						exhosts="$exhost"
					else
						exhosts="$exhosts|$exhost"
					fi
				done
			else
				exhosts="999999999"
			fi

                        if [ "$arg3" != "" ]
                        then
				svcsdef=`echo $arg3 | $SED 's/*/.*/g'`
				svcs=""
                                for svc in $svcsdef
                                do
                                        if [ "$svcs" = "" ]
                                        then
                                                svcs="$svc"
                                        else
                                                svcs="$svcs|$svc"
                                        fi
                                done
                        else
                                echo "bbwarnrules.cfg: Invalid entry in the file was found" 
                                echo "            Empty services column entry"
				set +f 		# reenable globber
                                continue
                        fi

			if [ "$arg4" != "" ]
			then
				exsvcsdef=`echo $arg4 | $SED 's/*/.*/g'`
				exsvcs=""
				for exsvc in $exsvcsdef
				do
					if [ "$exsvcs" = "" ]
					then
						exsvcs="$exsvc"
					else
						exsvcs="$exsvcs|$exsvc"
					fi
				done
			else
				exsvcs="999999999"
			fi

			NEWPAGER=""
			for page in $PAGERS
			do
				case $page
				in
					pg-* )
						pgline=`$GREP "^$page:" $BBHOME/etc/bbwarnsetup.cfg` 2>/dev/null
						if [ "$?" -eq 0 ]
						then
						        set $pgline >/dev/null 2>&1
						        if [ "$#" -gt 1 ]
						        then
						                shift           # remove pg- token from arg list
						                NEWPAGER="$NEWPAGER $*"
						        fi
						fi      
						;;

					*)
						NEWPAGER="$NEWPAGER $page"
						;;
				esac
			done
			PAGERS="$NEWPAGER"

			set +f 		# reenable globber

			# Does host match ?
			echo $redhost | $EGREP -x "$hosts" | $EGREP -vx "$exhosts" >/dev/null 2>&1
			if [ "$?" -ne 0 ]
			then
				continue
			fi

			# Does service match ?
			echo $redsvc  | $EGREP -x "$svcs" | $EGREP -vx "$exsvcs" >/dev/null 2>&1
			if [ "$?" -ne 0 ]
			then
				continue
			fi

			notify=FALSE

			OLDIFS=$IFS
			IFS='.'
			set $MACHSRV
			IFS=$OLDIFS

			set -f		# disable globber
			for day in $days
			do
				validday=FALSE
				OLDIFS=$IFS
				IFS='-'
				set $day
				IFS=$OLDIFS
				if [ "$#" -eq 2 ]
				then
					if [ $nowday -ge $1 -a $nowday -le $2 ]
					then
						validday=TRUE
					fi
				elif [ "$#" -eq 1 ]
				then
					if [ '*' = $day ]
					then
						validday=TRUE
					elif [ $nowday -eq $day ]
					then
						validday=TRUE
					fi
				fi
				if [ "$validday" = "TRUE" ]
				then
					for time in $times
					do
						validtime=FALSE
						OLDIFS=$IFS
						IFS='-'
						set $time
						IFS=$OLDIFS
						if [ "$#" -eq 2 ]
						then
							if [ $nowtime -ge $1 -a $nowtime -le $2 ]
							then
								validtime=TRUE
							fi
						elif [ "$#" -eq 1 ]
						then
							if [ '*' = $time ]
							then
								validtime=TRUE
							elif [ $nowtime -eq $time ]
							then
								validtime=TRUE
							fi
						fi
						if [ "$validtime" = "TRUE" ]
						then
							notify=TRUE
							break
						fi
					done # on time
					if [ "$notify" = "TRUE" ]
					then
						break
					fi
				fi
			done # on day 

			if [ "$notify" = "TRUE" ]
			then
				# Is it an ignore line ?
				if [ "$ignore" = "TRUE" ]
				then
					# ' *' (notice the space) 
					# Thanks to Scott Walters <scott@PacketPushers.com>
					if [ "$PAGERS" = ' *' ]
					then
						# * means all recipients, so need to continue
						$RM -f ${tmpnopage}
						exit 0
					else
						for rcpt in $PAGERS
						do
							# remove the pagedelay if anybody set one :(
							# you know, cut & paste :))
							OLDIFS=$IFS
							IFS=':'
							set $rcpt
							IFS=$OLDIFS
							echo "$1" >> $tmpnopage
						done
					fi
				else
					for rcpt in $PAGERS
					do
						# setup pagerdelay with leading zeroes to sort in 
						# with lowest value first
						OLDIFS=$IFS
						IFS=':'
						set $rcpt
						IFS=$OLDIFS
						if [ "$#" -eq 1 ]
						then
							echo "$1" >> $tmppage
						else
							# format of overriding pagerdelay are
							# :XX    :^XX     :^XX-YY    :~XX   :~XX-YY
							case $2 in

									# ^XX is escalation time 
									# ~XX is initial delay
									# so they should be
									# at the bottom of the sort, least significant
									# if recipient is defined multiple times
								\^*)
									echo "$1:$2" >> $tmppage
									;;

								\~*)
									echo "$1:$2" >> $tmppage
									;;

								*)
									# recipient if defined multiples should be
									# notified in the shortest delay
									# adding leading zeroes for alpha sort
									# sort cannot trusted for portability in
									# numeric sorting
									delay=`eval "echo $2"`
									if [ "$delay" -eq 0 ]
									then
										delay="0000"
									elif [ "$delay" -gt 0 -a "$delay" -lt 10 ]
									then
										delay="000$delay"
									elif [ "$delay" -lt 100 ]
									then
										delay="00$delay"
									elif [ "$delay" -lt 1000 ]
									then
										delay="0$delay"
									fi
									echo "$1:$delay" >> $tmppage
									;;
							esac
						fi
					done
				fi
			fi

			set +f		# reenable globber

		done # on config file

		# Do we have any recipients ?
		if [ -f $tmppage ]
		then
			# Get rid of duplicates
			$CAT $tmppage | $SORT -u > ${tmppage}.sort
			$RM -f $tmppage
			# if !rule line recipients found then removed them from recipients list
			if [ -f $tmpnopage ]
			then
				$CAT $tmpnopage | $SORT -u > ${tmpnopage}.sort
				$RM -f $tmpnopage

				# now remove the rcpt that appeared in ! rule(s)
				for rcpt in `$CAT ${tmpnopage}.sort`
				do
					$GREP -v "$rcpt" ${tmppage}.sort > ${tmppage}.work
					mv ${tmppage}.work ${tmppage}.sort
				done
			fi

			# Now put them on a single line for PAGERS variable
			# I can't save the variable PAGERS because in some
			# shells it will no be exported back (it will be
			# empty after exiting the while loop)
			$TOUCH $tmppage		#make sure an empty one exists
			PAGERS=""
			$CAT ${tmppage}.sort | \
			while read rcpt
			do
				# skip over empty lines
				if [ -z "$rcpt" ]
				then
					continue
				fi

				# I could echo with the no newline but
				# it differs from BSD and System V
				# and there might be some esoteric shell
				# that won't do it properly. oh well !
				# so I rewrite the file each time

				OLDIFS=$IFS
				IFS=':'
				set $rcpt
				IFS=$OLDIFS

				if [ "$2" != "" ]
				then
					delay=":$2"
				else
					delay=""
				fi
				# The same recipient could be found
				# i.e.  robert@localhost:15 robert@localhost:30
				# different specs on different rule line
				# always use the first recipient:specs found
				if [ "$PAGERS" != "" ]
				then
					$CAT $tmppage | $GREP "$1" >/dev/null 2>&1
					if [ "$?" -ne 0 ]
					then
						PAGERS="$PAGERS $1${delay}"
					fi
				else
					PAGERS="$1${delay}"
				fi
				echo "$PAGERS" > $tmppage
			done
			
			PAGERS=`$CAT $tmppage`
			# get rid of unwanted spaces
			SAVEPAGER=$PAGERS
                        PAGERS=`eval "echo $PAGERS"`
			# On some shells, the ^ escalation tag will make the eval fail :(
			# reset as the original
			if [ "$?" -ne 0 ]
			then
				PAGERS="$SAVEPAGER"
			fi
			export PAGERS	# PAGERS variable contains recipients

			# Do we have recipients ?
			if [ ! -z "$PAGERS"  ]
			then
				echo "$ORIGMSG" | $SED "s/$ORIGHOST/${redhost}.${redsvc}/1" > $BBTMP/pagemsg.$$
				$BBHOME/bin/bb-page1.sh "${SVCERR}.${MACHIP} $BBTMP/pagemsg.$$"
			fi
		fi
	
		$RM -f $tmppage
		$RM -f $tmpnopage
		$RM -f ${tmppage}.sort
		$RM -f ${tmpnopage}.sort
		$RM -f $BBTMP/pagemsg.$$
	fi

exit 0
