#! /bin/csh -f
# @(#)logview.sh	1.3	10/14/89
# sysdiag log file menu-based peruser
#

nice -20

#
# provide options for reviewing/reseting log file results
#
# notes:
#
#   if new logfiles have come into existance since 
#   logpos was set, some strange behavior will result
#

# these environment variables should be set:
# TMP	  # the tmp directory
# LOGDIR  # the log file directory
# LOGPOS  # sizes of logfiles before current sysdiag run

if ( (!($?TMP)) || (!($?LOGDIR)) || (!($?LOGPOS)) ) then
    echo 'input environment not setup.'
    echo 'this program is to be run while sysdiag is running'
    exit 0
endif


echo -n looking thru log files...
set nonomatch
#
# the most recent updates to all log files are excised and placed
# in TEMPLOGDIR
#
set errors = 0
set TEMPLOGDIR = $TMP/SYSDIAGLOG
\rm -rf $TEMPLOGDIR
mkdir $TEMPLOGDIR
cp $LOGDIR/* $TEMPLOGDIR
foreach i ($LOGPOS)
    set pos = `echo $i | awk -F- '{print $1 " " $2}'`
    if ($pos[1] != total) then  # total is output from wc
        @ end = $pos[2]
        @ end = $end + 1
        tail +$end $pos[1] > $TEMPLOGDIR/$pos[1]:t
	echo -n .
    endif
end

set errors = `egrep ERROR $TEMPLOGDIR/* | wc`
set errors = $errors[1]
set mtype = `arch -k`
echo " $errors error(s) located"
if ($errors != 0) then
	if ($mtype == "S4000") then
		nvram F
	endif
	echo -n "press RETURN to continue ==> "
	set foo = $<
else if ($errors == 0) then
	if ($mtype == "S4000") then
                nvram P
        endif
endif

set ans = ''
while (1)

    echo ' '
    echo 'p'....read most recent updates to log files
    echo 'P'....read all logfiles
    echo 'e'....read most recent logfiles with errors
    echo 'E'....read all logfiles with errors
    echo 'r'....reset all log files
    echo 'q'....exit this menu
    echo -n "==> "

    set k = $<
    set ans = ($k)

    if ($ans == p) then
	more $TEMPLOGDIR/*

    else if ($ans == P) then
	more $LOGDIR/*

    else if ($ans == e) then
	set y
	foreach i ($TEMPLOGDIR/*)
	    set x = `egrep ERROR $i | wc`
	    set x = $x[1]
	    if ($x != 0) then
		set y = ($y $i)
	    endif
	end
	if ($y[1] != "") more $y

    else if ($ans == E) then
	set y
	foreach i ($LOGDIR/*)
	    set x = `egrep ERROR $i | wc`
	    set x = $x[1]
	    if ($x != 0) then
		set y = ($y $i)
	    endif
	end
	if ($y[1] != "") more $y

    else if ($ans == r) then
 	foreach i ($LOGDIR/*)
 	    echo > $i
 	end
	\rm $TEMPLOGDIR/*

    else if ($ans == q) then
	\rm -rf $TEMPLOGDIR
	break

    else if ($ans != '') then
        echo "unknown option, enter one of: "

    else 
        echo "enter one of: "

    endif
end
unset nonomatch

