#!/bin/ksh
:
: uulog
:
# pragma ident "@(#)bnu:uulog	1.6 91/11/07 "
#
# Copyright 1991 Apple Computer, Inc.
# All Rights Reserved.
#
# THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF APPLE COMPUTER, INC.
# The copyright notice above does not evidence any actual or
# intended publication of such source code.
 
# uulog   		1.1 12/10/90 common
# common:uulog		1.2 1/17/90 10:29:47
# AT&T K18 bnu:uulog	2.11 

export IFS PATH
IFS=" 	
"
PATH="/bin:/usr/bin"

# uulog usage:
#	-c	  check uucp log files
#	-fSYSTEM  do a tail on the log file for this SYSTEM
#	-l[HOURS] list log entries within the last num of HOURS (default is 2)
#	-nnn	  do a tail -nnn on each log file
#	-q	  check uux log files
#	-sSYSTEM  report entries from the log file only on this SYSTEM
#	-x	  check uuxqt log files
#	SYSTEM	  exactly like the '-sSYSTEM' option
#
# The default log file is 'uucico'
#
LOGDIR=/usr/spool/uucp/.Log
typeset -i hours=0
typeset -i fflag=0
type=uucico
sys=""
n=""
x=""

usage() {
    echo "usage: uulog [-cqx] [-lHOURS] [-NUM] [-fSYSTEM] [-sSYSTEM] system"
    exit $1
}

cd $LOGDIR

while [ $# -gt 0 ]
do
	case $1 in
	-c)	type=uucp
		shift
		;;
	-q)	type=uux
		shift
		;;

	-x)	type=uuxqt
		shift
		;;

	-l*)	l=`echo $1|cut -c3-`
		shift
		if [ -z "$l" ]; then
		  let hours=2
		else
		  let hours=$l
		fi
		;;

	-[0-9]*)n=`echo $1|cut -c2-`
		shift
		;;

	-f)	let fflag=1
		shift
		if [ $# -eq 0 ]
		then
			echo "uulog: system name must follow -f flag" 1>&2
			usage 1
		else
			case "$1" in
			-*)
			    echo "uulog: system name must follow -f flag" 1>&2
			    usage 1
			    ;;
			esac
		fi
		;;

	-f*)	x=`echo $1|cut -c3-`
		shift
		set - $x $*
		let fflag=1
		;;

	-s)	shift
		if [ $# -eq 0 ]
		then
			echo "uulog: system name must follow -s flag" 1>&2
			usage 1
		else
			case "$1" in
			-*)
			    echo "uulog: system name must follow -s flag" 1>&2
			    usage 1
			    ;;
			esac
		fi
		;;

	-s*)	x=`echo $1|cut -c3-`
		shift
		set - $x $*
		;;

	-*)	echo "Invalid flag $1" 1>&2
		usage 1
		;;

	*)	sys="$sys $1"
		shift
		;;

	esac
done

typeset -i m d eh bh
let m=`date '+%m'`
let d=`date '+%d'`
let eh=`date '+%H'`
let bh=${eh}-${hours}
prefix="${m}/${d}"
start="${bh}"
unset m d eh bh

# set shell arguments to the names of the systems (if any)
set - $sys

if [ ${fflag} -eq 0 ]; then
	## will not "follow" a system log
	if [ $# = 0 ]; then
		## no system names given, so we'll examine all under this type
		set - `/bin/ls -rt $type`
	fi
	## examine each system log
	for i in $*
	do
		if [ -f $type/$i ]
		then
		  ## system log exists
		  if [ -z "${n}" ]; then
		    ## look at the whole system log
		    if [ ${hours} -gt 0 ]; then
		      ## list only the correct lines within the system log
		      awk -v p=${prefix} -v s=${start} '
  		        $3 ~ p {
    		          if((b=index($3, "-")) && (e=index($3, ":"))) {
      		            ++b;
      		            l = e - b;
      		            h = substr($3, b, l) + 0;
      		            s += 0;
      		            if(h > s)
         		      print $0;
    		            }
  		          }
  		        $4 ~ p {
    		          if((b=index($4, "-")) && (e=index($4, ":"))) {
      		            ++b;
      		            l = e - b;
      		            h = substr($4, b, l) + 0;
      		            s += 0;
      		            if(h > s)
         		      print $0;
    		            }
  		          }
		      ' $type/$i
		    else
		      ## list the complete system log
		      cat $type/$i
		    fi
		  else
		    ## look at only the last part of this system log
		    tail -$n $type/$i
		  fi
		else
		    echo "uulog: no log file available for system $i"
		    exit 1
		fi
	done
else
	## will "follow" a system log
	if [ $# != 1 ]; then
		echo "uulog: only one system allowed with -f" 1>&2
		exit 2
	fi
	if [ -f $type/$1 ]
	then
		exec tail -${n}f $type/$1
	else
		echo "uulog: no log file available for system $1"
		exit 1
	fi
fi
