#!/bin/sh 
#
# sendmsg
# Version 1.4
# Mar 05th, 2000
#
# kermit paging script
# SMM - moved Jacob's stuff here, 'cause real shells have no functions!
#
# Code by Jacob Lundqvist jaclu@ibk.se   
#
#   Updated 97-01-11
#
# Modification by Robert-Andre Croteau robert@bb4.com
#
# Bugs:	Mssages can be lost if several processes tries to access kermit
# 	at the same second, I have tried to reduce this risk by
#	introducing some randomness in the sleeptimes.
#	However if one sleeps 22, and some other sleep 73 they might
#	still wake upp at the same second, if thats the case, one or
#	both messages might get lost. This is hopefully not all that serious,
#	If the error is still there the next time bb checks, a new message 
#	will be sent, the risk that the same message would be lost twice,
#	can't be that high...
#	
#

A1=$1			# FIRST ARG
shift;
A2="$*"			# ALL THE REST.  DON'T ASK.  REALLY.

if test ! "$BBTMP"                      # GET DEFINITIONS IF NEEDED
then
        . $BBHOME/etc/bbdef.sh          # INCLUDE STANDARD DEFINITIONS
fi

#-----------------------------------------------------------------------------
#  Wait for device to be available
#-----------------------------------------------------------------------------

	# Get the available devices

	TTYLINE=`$GREP "^ttyline:" $BBHOME/etc/bbwarnsetup.cfg` 2>/dev/null
	if [ "$?" -ne 0 ]
	then
		echo "ttyline token was not setup in bbwarnsetup.cfg"
		echo "Numeric paging will not work"
		exit 1
	else
		set $TTYLINE >/dev/null 2>&1
		if [ "$#" -eq 1 ]
		then
			echo "ttyline token does not have an entry in bbwarnsetup.cfg"
			echo "Numeric paging will not work"
			exit 1
		else
			shift           # remove token from line
			TTYLINE=""
			for ttyline in $*
			do
				if [ -c "$ttyline" ]
				then
					TTYLINE="$TTYLINE $ttyline"
				fi
			done
			if [ "$TTYLINE" = "" ]
			then
				echo "ttyline token does not have a valid entry in bbwarnsetup.cfg"
				echo "Numeric paging will not work"
				exit 1
			fi
		fi
	fi

	# Get the pagemaster users

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

    	#
    	# To separate page-requests arriving the same second, 
	# we give sleep-times depending on PID
	# 
	I=`$EXPR $$ % 10`  		# last digit of PID
	SLEEPTIME=`$EXPR $I + 1`		# plus 1 second
	MAXSLEEPS=`$EXPR 60 / $SLEEPTIME + 2`
	#
	# always do one sleep, to separate processes
	# starting same second in time
	#
	# echo initial sleep of:$SLEEPTIME
	sleep $SLEEPTIME 	

	foundtty=FALSE
	while [ "$foundtty" = "FALSE" ]
	do
		for ttyline in $TTYLINE
		do
    			TTYDEV=`echo "$ttyline" | $SED "s/\/dev\///"`
			if test -f ${LOCKPREFIX}${TTYDEV}  || test -f ${PAGINGLOCK}_${TTYDEV}
    			then
				# echo "page: line busy, sleeping $SLEEPTIME"
				sleep $SLEEPTIME
				MAXSLEEPS=`$EXPR $MAXSLEEPS - 1`
				if [ "$MAXSLEEPS" -lt 0 ]
				then
					if [ "$PAGEMASTER" != "" ]
					then
						echo "Couldn't send page, locks weren't freed" | $MAIL "BB Pager problem" $PAGEMASTER
					fi
					echo "Couldn't send page, locks weren't freed" 
					exit 1
				fi
				# Try next modem line in TTYLINE
			else
				foundtty=TRUE
				break;
			fi	
    		done
	done

$BBHOME/bin/touchtime ${PAGINGLOCK}_${TTYDEV} # prevent race-conditions

#-----------------------------------------------------------------------------
# Send a message using a kermit script, multiple attempts
#-----------------------------------------------------------------------------
#

    I=1
    while test $I -le 10
    do
	# echo "page; trying to send, attempt: $I"
	I=`$EXPR $I + 1`
	$KERMIT $A1 $ttyline $A2 > /dev/null
	if test "$?" = "0" 
	then
	    I=999 # make sure loop ends...
	fi
    done

    $RM -f ${PAGINGLOCK}_${TTYDEV}

# Debuging...

#    if test "$I" = "999"
#    then
#	echo "msg delivered"
#    else
#	echo "msg failed"
#    fi

