#! /bin/sh
#
# callback -- a call-back facility for aiv-staff members.
#
# usage:	callback [baud [waittime] ]
#
# v1 03-10-86  Eelco van Asperen
# v2 06-07-88  Cleaning-up
#
# this is the file where user/telno data are stored:
#
CALLDBS="/usr2/aiv/admi/callback.dbs"
#
# first get callers name:
#
HISNAME=`who am i | cut -f1 -d" " -s`
#
# now see if he/she is in the callback-database:
#
HISENTRY=`grep ^$HISNAME: $CALLDBS`
PHONENUM=`echo $HISENTRY | cut -d":" -f2 -s`
#
if [ -z "$PHONENUM" ]
then
	echo "Your name is not in the call-back database; contact your sysop."
	exit 1
else
	echo "Your phone-number: "$PHONENUM
fi
if [ -n "$1" ]
then
	BAUD=$1
else
	BAUD=`echo $HISENTRY | cut -d":" -f3 -s`
fi
if [ -z "$BAUD" ]
then
	# system default baudrate:
	BAUD=1200
fi
WAIT=15
if [ "$2" != "" ]
then 
	#
	# how long should 'cu' wait for a free dialer ?
	# (in minutes)
	#
	WAIT=$2
fi
#
# Parse the /usr/lib/uucp/Devices-file to find list of dialers;
#
TMPFILE=/tmp/cback.$$
trap 'rm $TMPFILE' 0 1 15 
DEBUG=9

awk -F" " '
BEGIN	{ dialers=""; 
	  speeds="";
	}
/^ACU/	{ if ($4 == reqspeed)
		dialers = dialers " " $2;
	  else	if (speeds == "" || index(speeds,$4) == 0) {
			speeds = speeds " " $4;
		}
	}
END	{ if (dialers == "") {
		print "echo \"Sorry, there are no dialers at",reqspeed,\
			"baud available on this system.\"";
		print "echo \"Dialers are available at the following speeds:\""
		print "echo \"	",speeds,"\""
		print "exit 1";
	  }
	  else {
		print	"for i in",dialers
		print	"do"
		print	"	if [ ! -f /usr/spool/locks/LCK..$i ]"
		print	"	then"
		print	"		echo \"Found free dialer $i\""
		print	"		exit 0\n"\
			"	fi\n";
		print	"done"

		print	"echo \"Sorry, all dialers are currently locked\"";
		print	"echo \"Start callback anyway (y,n) ? \\c\"";
		print	"read answer";
		print	"if [ \"$answer\" != \"y\" ]";
		print	"then";
		print	"	exit 1";
		print	"fi";
		print	"exit 0";
	  }
	}
' reqspeed=$BAUD /usr/lib/uucp/Devices >$TMPFILE

sh $TMPFILE
if [ $? != 0 ]
then
	exit 1
fi
trap "" 0 1 15 
rm -f $TMPFILE
#
# start dialing:
#
(nohup ct -w${WAIT} -s${BAUD} -x${DEBUG} 0-${PHONENUM})&
echo "Dial-back started..."
exit 0
