PATH=:/bin:/usr/bin:/usr/ucb
export PATH
rm -f /tmp/callsto /tmp/cmds /tmp/callsfrom
cd /usr/spool/uucp
if [ $# -gt 0 ]
then
	if [ -f LOGFILE ]
	then
		what="Logfile-DAY Syslog-DAY"
	else
		what="LOG/uucico/.* LOG/uux*/.* LOG/xferstats/.*"
	fi
else
	if [ -f LOGFILE ]
	then
		what="LOGFILE SYSLOG"
	else
		what="LOG/uucico/* LOG/uux/* LOG/xferstats/*"
	fi
fi
sed -e "s/PATH=.*PATH;//
/REQUEST/d"  $what |
/bin/awk '
$4 ~ /SUCCEEDED|OK/ {
	t = substr($3,index($3,"-")+1,5)
	split(t,time,":")
	if($5 !~ /conversation/)
		startup[$2] = time[1]*60+time[2]
} 
$4 ~ /FAILED|CAUGHT|OK|TIMEOUT/ {
	if(startup[$2] == 0 || $5 ~ /startup/)
		continue
	t = substr($3,index($3,"-")+1,5)
	split(t,time,":")
	elapsed = time[1]*60+time[2] - startup[$2]
	if (elapsed < 0 )
		elapsed += 24*60
	if (elapsed == 0)
		elapsed = 1
	if(callto[$2] ){
		totcallto[$2] += elapsed
		numcallto[$2]++
		printf("%s\t%02d:%02d - %02d:%02d\n",$2,startup[$2]/60,startup[$2]%60,time[1],time[2])>>"/tmp/callsto"
	}else{
		totcallby[$2] += elapsed
		numcallby[$2]++
		printf("%s\t%02d:%02d - %02d:%02d\n",$2,startup[$2]/60,startup[$2]%60,time[1],time[2])>>"/tmp/callsfrom"
	}
	callto[$2] = 0
	startup[$2] = 0
}
$4 ~ /SUCCEEDED/{
	if ($5 ~ /call/)
		callto[$2]++
}
$4 ~ /FAILED|CAUGHT/{
	if ($5 ~ /call|conversation|SIGNAL/ )
		failed[$2]++
}
$5 ~ /sent/{
	sentbytes[$2] += $7
	sentfiles[$2] ++
	seconds[$2] += $9
	retries[$2] += $11
}
$5 ~ /received/{
	recbytes[$2] += $7
	recfiles[$2] ++
	seconds[$2] += $9
	retries[$2] += $11
}
$5 ~/XQT/{
	if( $7 ~ /!|@/ && $6 ~ /rmail/)
		printf("%s\t%s!%s\t%s\n",substr($3,index($3,"-")+1,5),$2,$4,$7)>>"/tmp/cmds"
}
$4 ~/XQT/{
	if ($1 ~ /uucp|daemon|root|news/ || $7 != "rmail")
		continue
	printf("%s\t%s\t%s!%s\n",substr($3,index($3,"-")+1,5),$1,$2,$7)>>"/tmp/cmds"
}
$10 ~/secs/ {
	curtime = substr($4, 5, 6)
	dtmp = curtime - int($9) - lasttime[$2]
	if (dtmp > 0 && dtmp < 20)
		dead[$2] += dtmp
	lasttime[$2] = curtime
	live[$2] += $9
}
{
	n = index ($3, "/")
	if (n > 0 )
		lmon = substr($3, 2, n-2)
	m = index ($3, "-")
	n++
	if( (m-n) > 0 )
		lday = substr($3, n, m-n)
	ltime = substr($3,m+1,5)


	if (fmon == "") {
		fmon = lmon
		fday = lday
		ftime = ltime
	}
}
END{
	printf("\n\t\t\t\tUUCP Traffic Summary\n\n")
	printf("\t\t\t\tFrom %d/%d %s To %d/%d %s\n\n",fmon,fday,ftime,lmon,lday,ltime)
	printf("\t      Calls    Minutes     Files       Bytes      Effective   Unused\n")
	printf("Site[Failed] To/From   To/From    To/From   Sent/Received  Baudrate  Bandwidth\n")
	for( i in startup ){
		if(seconds[i]== 0)seconds[i]++
		if(dead[i]+live[i])
			percent = dead[i]*100/(dead[i]+live[i])
		else
			percent = 0
		if (failed[i] || retries[i]) {
			temp = i "[" failed[i]
			if (retries[i])
				temp = temp "/" retries[i] "]"
			else
				temp = temp "]"
		} else
			temp = i
		printf("%-12s %3d/%-3d %5d/%-5d %4d/%-4d %8d/%-8d %6d  %5d%%\n",\
		temp,numcallto[i],numcallby[i],\
		totcallto[i],totcallby[i],\
		sentfiles[i],recfiles[i],\
		sentbytes[i],recbytes[i],\
		(recbytes[i]+sentbytes[i])*8/seconds[i],\
		percent)
	}
}'

echo " "
echo " "
echo " "
echo "		Long Distance Calls"
echo
echo "To	Start - End		    From    Start - End"
cat >/tmp/$$local<<!!!
/^rlgvax/d
/^columbi/d
/^nbs-amr/d
/^kobold/d
/^sdccis/d
/^hugo/d
/^umcp-cs/d
/^elsie/d
/^gl-red/d
/^ut-sall/d
/^nta-vax/d
/^rochest/d
/^cmcl2/d
/^lbl-csa/d
/^trwatf/d
/^lognet2/d
/^prometh/d
/^andi/d
/^tardis/d
/^prcrs/d
/^uwvax/d
/^vrdxhq/d
/^grendel/d
/^sri-iu/d
/^utah-cs/d
/^hadron/d
/^harvard/d
/^netword/d
/^s3sun/d
/^brl-tgr/d
/^mordor/d
/^wuhep/d
/^janus/d
/^topaz/d
!!!
sed -f /tmp/$$local /tmp/callsto | /bin/awk '
{
	if (last == $1)
		pr = ""
	else {
		last = $1
		pr = $1
	}
	printf("%s\t%s - %s\n",pr,$2,$4)
}'>/tmp/$$a
sed -f /tmp/$$local /tmp/callsfrom | /bin/awk '
{
	if (last == $1)
		pr = ""
	else {
		last = $1
		pr = $1
	}
	printf("%s\t%s - %s\n",pr,$2,$4)
}'>/tmp/$$b
pr -t -m /tmp/$$a /tmp/$$b | uniq
rm -f /tmp/$$[ab]
sed -e "
/rmail	seismo!/d
/rmail	rlgvax!/d
/rmail	rochester!/d
/rmail	umcp-cs!/d
/rmail	brl-tgr!/d
/rmail	brl-vgr!/d
s/)$//
" /tmp/cmds >/tmp/$$c
if [ -s /tmp/$$c ]
then
	echo " "
	echo " "
	echo " "
	echo "		Outgoing Mail from Other Sites"
	echo
	echo "When	From	To"
	cat /tmp/$$c
fi
rm -f /tmp/cmds /tmp/$$* /tmp/callsto /tmp/callsfrom
