#!/bin/csh
# program start:


# do you want debugging output?  --its pretty banal, but it seems to work anyway.
set DEBUG = OFF

# does the user want help?  
if ($1 == h) goto USAGE		# h = full help message
# if ($1 == r) goto REPSUMM	# r = report types  for command-line options
				#     not yet added to gla

PRESET:
# set the variables that gla needs
if ($DEBUG == ON) echo "Entering PRESET"

# unset noclobber to ensure you can overwrite the output files
unset noclobber

# LOGSOURCE points to the location of the gopher logfile
set LOGSOURCE = /project/library/gopher-public/etc/logfile
if ($DEBUG == ON) echo "LOGSOURCE = $LOGSOURCE"

# GNUPLOT points to the location of a gnuplot binary that supports local
# .gnuplot files as command files.
set GNUPLOT = /home/jtice/falab/gla/bin/gnuplot 
if ($DEBUG == ON) echo "GNUPLOT found in $GNUPLOT"


# STDT indicates the logfile's starting date
set STDT = `head -1 $LOGSOURCE | awk '{print $2 " " $3}'`
if ($DEBUG == ON) echo "STDT = $STDT"

# GTMP sets the temporary file-name to something that is probably uniq
set GTMP = `date | awk '{print $3 $2 $4}'`
if ($DEBUG == ON) echo "GTMP = $GTMP"

#  This sets the GDATE variable for the current date--for all
set DTNO = `date | awk '{print $3}'`
if ($DEBUG == ON) echo "DTNO = $DTNO"
set DTNUM = `expr $DTNO : '.*'`
if ($DEBUG == ON) echo "DTNUM = $DTNUM"
if ($DTNUM == 1 ) then
   set GDATE = `date | awk '{print $1 "." $2 ".." $3}'`
else
   set GDATE = `date | awk '{print $1 "." $2 "." $3}'`
endif
if ($DEBUG == ON) echo "GDATE = $GDATE"



CREDITS:
# show the nice people what the program is

if ($DEBUG != ON) clear
echo ""
echo "     glas - Gopher Logfile Analysis Script   -    v1.0"
echo "                                    jtice@nero.uvic.ca"
echo "                                            15/02/1993"
echo ""
if ($DEBUG == ON) echo "Credits screen : suceessful"



FIRSTOUTFILE:
# this is the first attempt at getting an output file
# only names it if it isn't on the command line --if its on the command line
# send it to the processor at REPNUMBER

if ($DEBUG == ON) echo "Entering FIRSTOUTFILE"
if ($1 != "") then
   set OUTF = $1
   if ($DEBUG == ON) echo "going to CHECKOUT"
   goto CHECKOUT
else
  echo ""
  echo -n "[default = ./gla-out] Use what output file ? "
  set OUTF = ($<)
  if ($OUTF == "") set OUTF = gla-out
endif
if ($DEBUG == ON) echo "Output file = $OUTF"
if ($DEBUG == ON) echo "going to CHECKOUT"
goto CHECKOUT



OUTFILE:
# for naming an alternative output file if there's a problem with the first one.

if ($DEBUG == ON) echo "Entering OUTFILE"
echo ""
echo -n "[default = ./gla-out] Use what alternate file ? "
set OUTF = ($<)
if ($OUTF == "") set OUTF = ./gla-out
if ($DEBUG == ON) echo "Output file = $OUTF"


CHECKOUT:
# Ok, derive the output filename and directory name from the OUTF variable

if ($DEBUG == ON) echo "Entering CHECKOUT"
echo ""
set FILE = `expr  //$OUTF : '.*/\(.*\)' \|  $OUTF`
set OUTDIR = `dirname $OUTF`
if ($DEBUG == ON) echo "CHECKFILE = $FILE"
if ($DEBUG == ON) echo "OUTFILE DIR = $OUTDIR"


# test to make sure that you have write permission on the output directory
# and that want to overwrite $OUTF if it already exists

if ($DEBUG == ON) echo "Directory permission check"
if ( !(-w $OUTDIR) ) then
 echo ""
 echo "You do not have write priviledges to $OUTDIR"
 echo ""
 echo -n "DO you wish to select another out-put file?"
 set RESP = ($<)
if ($DEBUG == ON) echo "selecting another file: statement entered"
 switch ($RESP)
    case [y,Y]:
	goto OUTFILE
    breaksw
    case [n,N]:
	echo "Cannot write to directory $OUTDIR, exiting"
	goto CLEAN
    breaksw
 endsw
else
 if ( -d $OUTDIR ) then
 if ($DEBUG == ON) echo "Existing file check"
    if ( -e $OUTF ) then
       echo "$OUTF already exists\!"
       echo ""
       echo -n "[O]verwrite file, [A]ppend file, [S]elect Alternate, or e[X]it:"
       set RESP = ($<)
       if ($DEBUG == ON) echo "over/app file: statement entered"
       switch ($RESP)
	   case [a,A]:
		echo ""
		echo "Appending $OUTF with GLA output"
           breaksw
	   case [o,O]:
		echo ""
		echo "overwriting $OUTF with GLA output"
		/bin/rm -f $OUTF
    	   breaksw
	   case [s,S]:
		echo "selecting an alternate out-put file."
		goto OUTFILE
    	   breaksw
	   case [x,X]:
		goto CLEAN
    	   breaksw
	   default:
		echo "selecting an alternate out-put file."
		goto OUTFILE
	   breaksw
       endsw
    else
       echo "writing output to $OUTF"
       echo ""
    endif
  else
     echo "$OUTDIR is not a directory--$OUTF does not exist"
     goto CLEAN
  endif
endif



REPTYPE:
#  Ok, so the out-put directory and file are verified--what report do you want?

if ($DEBUG == ON) echo "Entering REPTYPE"

# Start asking about report types.
echo -n "Report type: [T]otal, to[D]ay, [H]ost, [A]verages, [G]raph, e[X]it : "
set REPTYP = ($<)
if ($REPTYP == "") then
  echo "that was not a valid selection, try again."
  goto REPTYPE
endif

switch ($REPTYP)
   case [x,X]:
	goto CLEAN
   breaksw
   case [t,T]:
	echo -n "Select report: [D]irs, [F]iles, [M]achines, [W]eekdays, [H]ours, e[X]it :"
	set REPSEL = ($<)
	switch ($REPSEL)
		case [x,X]:
			goto CLEAN
		breaksw
		case [d,D]:
			set REPNUM = 1
		breaksw
		case [f,F]:
			set REPNUM = 2
		breaksw
		case [m,M]:
			set REPNUM = 3
		breaksw
		case [w,W]:
			set REPNUM = 4
		breaksw
		case [h,H]:
			set REPNUM = 5
		breaksw
		default:
			echo "That was not a valid selection.  Try again."
			goto REPTYPE
		breaksw
	endsw
	if ($DEBUG == ON) echo "REPNUM = $REPNUM"
   breaksw
   case [d,D]:		
	echo -n "Select report: [D]irs, [F]iles, [M]achines, [T]otal, e[X]it :"
	set REPSEL = ($<)
	switch ($REPSEL)
		case [x,X]:
			goto CLEAN
		breaksw
		case [d,D]:
			set REPNUM = 6
		breaksw
		case [f,F]:
			set REPNUM = 7
		breaksw
		case [m,M]:
			set REPNUM = 8
		breaksw
		case [t,T]:
			set REPNUM = 9
		breaksw
		default:
			echo "That was not a valid selection.  Try again."
			goto REPTYPE
		breaksw
	endsw
   if ($DEBUG == ON) echo "REPNUM = $REPNUM"
   breaksw
   case [h,H]:
	echo -n "Select report: [D]irs, [F]iles, [H]ourly, [W]eekday, e[X]it :"
	set REPSEL = ($<)
	switch ($REPSEL)
		case [x,X]:
			goto CLEAN
		breaksw
		case [d,D]:
			set REPNUM = 10
		breaksw
		case [f,F]:
			set REPNUM = 11
		breaksw
		case [h,H]:
			set REPNUM = 12
		breaksw
		case [w,W]:
			set REPNUM = 13
		breaksw
		default:
			echo "That was not a valid selection.  Try again."
			goto REPTYPE
		breaksw
	endsw
   	if ($DEBUG == ON) echo "REPNUM = $REPNUM"
   breaksw
   case [a,A]:
	echo -n "Select report: [D]ir, [F]ile, [M]achine, [T]otal, e[X]it :"
	set REPSEL = ($<)
	switch ($REPSEL)
		case [x,X]:
			goto CLEAN
		breaksw
		case [d,D]:
			set REPNUM = 14
		breaksw
		case [f,F]:
			set REPNUM = 15
		breaksw
		case [m,M]:
			set REPNUM = 16
		breaksw
		case [t,T]:
			set REPNUM = 17
		breaksw
		default:
			echo "That was not a valid selection.  Try again."
			goto REPTYPE
		breaksw
	endsw
	if ($DEBUG == ON) echo "REPNUM = $REPNUM"
   breaksw
   case [g,G]:
	echo "Select report: [W]eekday, [H]our, [T]oday by hour,"
	echo -n "               HoSt by week[D]ay, Ho[S]t by hour : "
	set REPSEL = ($<)
	switch ($REPSEL)
		case [x,X]:
			goto CLEAN
		breaksw
		case [w,W]:
			set REPNUM = 18
		breaksw
		case [h,H]:
			set REPNUM = 19
		breaksw
		case [t,T]:
			set REPNUM = 20
		breaksw
		case [d,D]:
			set REPNUM = 21
		breaksw
		case [s,S]:
			set REPNUM = 22
		breaksw
		default:
			echo "That was not a valid selection.  Try again."
			goto REPTYPE
		breaksw
	endsw
	if ($DEBUG == ON) echo "REPNUM = $REPNUM"
   breaksw
   default:
	echo "That was not a valid selection.  Try again."
	goto REPTYPE
   breaksw
endsw
goto REPNUMBER



REPNUMBER:
# we now know where the output file is, and what the report type is--lets do it.

if ($DEBUG == ON) echo "Entering REPNUMBER"
if ($DEBUG == ON) echo "REPNUM = $REPNUM"

# Ok, find the report-number and exexute it.

switch ($REPNUM)
   case 1:
	echo "" >> $OUTF
	echo "01:total directories retrieved" >> $OUTF
	echo "------------" >> $OUTF
	echo "" >> $OUTF
	awk '$10 ~ /directory/ {print $11}' $LOGSOURCE | sort | uniq -c | \
	   sort -nr | head -50 >> $OUTF
	echo "" >> $OUTF
	set OUTMODE = 1
   breaksw
   case 2:
	echo "" >> $OUTF
	echo "02:total files retrieved" >> $OUTF
	echo "------------" >> $OUTF
	echo "" >> $OUTF
	awk ' $10 ~ /file/ {print $11}' $LOGSOURCE | sort | uniq -c | \
	   sort -nr | head -50 >> $OUTF
	echo "" >> $OUTF
	set OUTMODE = 1
   breaksw
   case 3:
	echo "" >> $OUTF
	echo "03:total machines and # of connections" >> $OUTF
	echo "------------" >> $OUTF
	echo "" >> $OUTF
	awk '{print $7}' $LOGSOURCE | sort |  uniq -c | sort -nr | \
	   head -50 >> $OUTF
	echo "" >> $OUTF
	set OUTMODE = 1
   breaksw
   case 4:
	echo "" >> $OUTF
	echo "04:total traffic by weekday" >> $OUTF
	echo "------------" >> $OUTF
	echo "" >> $OUTF
	awk '{print $1}' $LOGSOURCE | sort | uniq -c | \
	   awk '{print $2 " " $1}' >> $OUTF
	echo "" >> $OUTF
	set OUTMODE = 1
   breaksw
   case 5:
	echo "" >> $OUTF
	echo "05:total traffic by hour" >> $OUTF
	echo "------------" >> $OUTF
	echo "" >> $OUTF
	awk '{print $4}' $LOGSOURCE | colrm 3 50 | sort | uniq -c |\
	  awk '{print $2 " " $1}' >> $OUTF
	set OUTMODE = 1
   breaksw
   case 6:
	echo "" >> $OUTF
	echo "06:directories retrieved today" >> $OUTF
	echo "------------" >> $OUTF
	echo "" >> $OUTF
	grep "$GDATE" $LOGSOURCE | awk '$10 ~ /directory/ {print $11}' | sort |\
  	   uniq -c | sort -nr >> $OUTF
	echo "" >> $OUTF
	set OUTMODE = 1
   breaksw
   case 7:
	echo "" >> $OUTF
	echo "07:files retrieved today" >> $OUTF
	echo "------------" >> $OUTF
	echo "" >> $OUTF
	grep "$GDATE" $LOGSOURCE | awk ' $10 ~ /file/ {print $11}' | sort | \
	   uniq -c | sort -nr >> $OUTF
	echo "" >> $OUTF
	set OUTMODE = 1
   breaksw
   case 8:
	echo "" >> $OUTF
	echo "08:machines that connected today" >> $OUTF
	echo "------------" >> $OUTF
	echo "" >> $OUTF
	grep "$GDATE" $LOGSOURCE | awk '{print $7}' | sort | uniq -c | \
	   sort -nr >> $OUTF
	echo "" >> $OUTF
	set OUTMODE = 1
   breaksw
   case 9:
	echo "" >> $OUTF
	echo "09:hourly usage for today" >> $OUTF
	echo "------------" >> $OUTF
	echo "" >> $OUTF
	grep "$GDATE" $LOGSOURCE | awk '{print $4}' | colrm 3 50 |\
	sort | uniq -c | awk '{print $2 " " $1}' >> $OUTF
	echo "" >> $OUTF
	set OUTMODE = 1
   breaksw
   case 10:
   	echo -n "What machine: "
	set MACHINE = ($<)
	if ($MACHINE == "") goto CLEAN
	echo "" >> $OUTF
	echo "10: directories retrieved by $MACHINE" >> $OUTF
	echo "------------" >> $OUTF
	echo "" >> $OUTF
	awk '$10 ~ /directory/' $LOGSOURCE | \
	   awk '$7 ~ /'$MACHINE'/ {print $11}' | sort | uniq -c | \
	   sort -nr > $OUTF
	if ( -z $GTMP ) then
	   echo "No connections from $MACHINE since $STDT" >> $OUTF
	   goto LOOP
	echo "" >> $OUTF
	endif
	set OUTMODE = 1
   breaksw
   case 11:
   	echo -n "What machine: "
	set MACHINE = ($<)
	if ($MACHINE == "") goto CLEAN
	echo "" >> $OUTF
	echo "11: files retrieved by $MACHINE" >> $OUTF
	echo "------------" >> $OUTF
	echo "" >> $OUTF
	awk '$10 ~ /file/' $LOGSOURCE | \
	   awk '$7 ~ /'$MACHINE'/ {print $11}' | sort | uniq -c | \
	   sort -nr > $OUTF
	if ( -z $GTMP ) then
	   echo "No connections from this machine since $STDT" >> $OUTF
	   goto LOOP
	echo "" >> $OUTF
	endif
	set OUTMODE = 1
   breaksw
   case 12:
   	echo -n "What machine: "
	set MACHINE = ($<)
	if ($MACHINE == "") goto CLEAN
	echo "" >> $OUTF
	echo "12: hourly breakdown of $MACHINE connections" >> $OUTF
	echo "------------" >> $OUTF
	echo "" >> $OUTF
	awk '$7 ~ /'$MACHINE'/ {print $4}' $LOGSOURCE | colrm 3 50 | sort -n |\
   	   uniq -c | awk '{print $2 " " $1}' | sort -n  > $OUTF
	if ( -z $OUTF ) then
	   echo "No connections from this machine since $STDT" >> $OUTF
	echo "" >> $OUTF
	endif
	set OUTMODE = 1
   breaksw
   case 13:
   	echo -n "What machine: "
	set MACHINE = ($<)
	if ($MACHINE == "") goto CLEAN
	echo "" >> $OUTF
	echo "13: hourly breakdown of $MACHINE connections" >> $OUTF
	echo "------------" >> $OUTF
	echo "" >> $OUTF
	awk '$7 ~ /'$MACHINE'/ {print $1}' $LOGSOURCE | sort | uniq -c |\
	   awk '{print $2 " " $1}' > $OUTF
	if ( -z $OUTF ) then
	   echo "No connections from this machine since $STDT" >> $OUTF
	echo "" >> $OUTF
	endif
	set OUTMODE = 1
   breaksw
   case 14:
   	echo -n "What directory: "
	set DIRECT = ($<)
	if ($DIRECT == "") goto CLEAN
	awk '$10 ~ /directory/' $LOGSOURCE | \
	   awk '$11 ~ /'$DIRECT'/ {print $2 " " $3}' | sort > $GTMP
	if ( -z $GTMP ) then
	   echo "No connections from this machine since $STDT"
	   goto LOOP
	endif
	set DAYS = `uniq -c $GTMP | wc -l | awk '{print $1}'`
	set CONNECTS = `wc -l $GTMP | awk '{print $1}'`
	set AVG = `echo "$CONNECTS/$DAYS" | /usr/bin/bc`
	echo "14: average retrieval of $DIRECT" >> $OUTF
	echo "------------" >> $OUTF
	echo "" >> $OUTF
	echo "$DIRECT was accessed an average of $AVG times" >> $OUTF
	echo "($CONNECTS connections / $DAYS days) since $STDT" >> $OUTF
	echo "" >> $OUTF
	set OUTMODE = 1
   breaksw
   case 15:
   	echo -n "What file: "
	set FILENM = ($<)
	if ($FILENM == "") goto CLEAN
	awk '$11 ~ /'$FILENM'/ {print $2 " " $3}' $LOGSOURCE | sort > $GTMP
	if ( -z $GTMP ) then
	   echo "No connections for this file since $STDT"
	   goto LOOP
	endif
	set DAYS = `uniq -c $GTMP | wc -l | awk '{print $1}'`
	set RETREIVS = `wc -l $GTMP | awk '{print $1}'`
	set AVG = `echo "$RETREIVS/$DAYS" | /usr/bin/bc`
	echo "15: average retrieval of $FILENM" >> $OUTF
	echo "------------" >> $OUTF
	echo "" >> $OUTF
	echo " $FILENM was retrieved $AVG times per day" >> $OUTF
	echo "($RETREIVS connections / $DAYS days) since $STDT" >> $OUTF
	echo "" >> $OUTF
	set OUTMODE = 1
   breaksw
   case 16:
   	echo -n "What machine: "
	set MACHINE = ($<)
	if ($MACHINE == "") goto CLEAN
	awk '$7 ~ /'$MACHINE'/ {print $2 " " $3}' $LOGSOURCE | sort > $GTMP
	if ( -z $GTMP ) then
	   echo "No connections from this machine since $STDT"
	   goto LOOP
	endif
	set DAYS = `uniq -c $GTMP | wc -l | awk '{print $1}'`
	set CONNECTS = `wc -l $GTMP | awk '{print $1}'`
	set AVG = `echo "$CONNECTS/$DAYS" | /usr/bin/bc`
	echo "" >> $OUTF
	echo "16: average retrieval of $MACHINE" >> $OUTF
	echo "------------" >> $OUTF
	echo "" >> $OUTF
	echo "Youve had an average of $AVG connections per day for $MACHINE" >> $OUTF
	echo "($CONNECTS connections / $DAYS days) since $STDT" >> $OUTF
	echo "" >> $OUTF
	set OUTMODE = 1
   breaksw
   case 17:
	awk '{print $1 " " $2 " " $3}' $LOGSOURCE | sort |\
	  uniq -c > $GTMP
	set DAYS = `wc -l $GTMP | awk '{print $1}'`
	set LINES = `wc -l $LOGSOURCE | awk '{print $1}'`
	set AVG = `echo "$LINES/$DAYS" | /usr/bin/bc`
	echo "" >> $OUTF
	echo "17: average total retrieval " >> $OUTF
	echo "------------" >> $OUTF
	echo "" >> $OUTF
	echo "Youve had an average of $AVG connections per day " >> $OUTF
	echo "($LINES entries / $DAYS days) since $STDT" >> $OUTF
	echo "" >> $OUTF
	set OUTMODE = 1
   breaksw
   case 18:
	echo -n "Are you on an X11 Display?"
	set X11DISP = ($<)
	switch ($X11DISP)
	   case [y,Y]:
		echo "set term X11" > .gnuplot
		echo "term set to X11"
	   breaksw
	   case [n,N]:
		echo "set term dumb" > .gnuplot
		echo "term assumed to be dumb"
	   breaksw
	endsw
	echo "set data style linespoints" >> .gnuplot
	if ($DEBUG == ON) echo "show data style" >> .gnuplot
	echo "set xrange [0:6]" >> .gnuplot
	if ($DEBUG == ON) echo "show xrange" >> .gnuplot
	echo "set noautoscale x" >> .gnuplot
	echo "set autoscale y" >> .gnuplot
	echo 'set xlabel "day"' >> .gnuplot
	if ($DEBUG == ON) echo "show xlabel" >> .gnuplot
	echo 'set ylabel "# Connections"' >> .gnuplot
	if ($DEBUG == ON) echo "show ylabel" >> .gnuplot
	endif
	echo -n 'plot "' >> .gnuplot
	echo $OUTF >> .gnuplot
	echo -n "0 " > $OUTF
	awk '$1 ~ /Mon/' $LOGSOURCE | wc -l >> $OUTF
	echo -n "1 " >> $OUTF
	awk '$1 ~ /^Tue/' $LOGSOURCE | wc -l >> $OUTF
	echo -n "2 " >> $OUTF
	awk '$1 ~ /^Wed/' $LOGSOURCE | wc -l >> $OUTF
	echo -n "3 " >> $OUTF
	awk '$1 ~ /^Thu/' $LOGSOURCE | wc -l >> $OUTF
	echo -n "4 " >> $OUTF
	awk '$1 ~ /^Fri/' $LOGSOURCE | wc -l >> $OUTF
	echo -n "5 " >> $OUTF
	awk '$1 ~ /^Sat/' $LOGSOURCE | wc -l >> $OUTF
	echo -n "6 " >> $OUTF
	awk '$1 ~ /^Sun/' $LOGSOURCE | wc -l >> $OUTF
	set OUTMODE = 2
   breaksw
   case 19:
	echo -n "Are you on an X11 Display?"
	set X11DISP = ($<)
	switch ($X11DISP)
	   case [y,Y]:
	echo "set term X11" > .gnuplot
	echo "term set to X11"
	   breaksw
	   case [n,N]:
	echo "set term dumb" > .gnuplot
	echo "term assumed to be dumb"
	   breaksw
	endsw
	echo "set data style linespoints" >> .gnuplot
	if ($DEBUG == ON) echo "show data style" >> .gnuplot
	echo "set xrange [0:23]" >> .gnuplot
	if ($DEBUG == ON) echo "show xrange" >> .gnuplot
	echo "set noautoscale x" >> .gnuplot
	echo "set autoscale y" >> .gnuplot
	echo 'set xlabel "hours"' >> .gnuplot
	if ($DEBUG == ON) echo "show xlabel" >> .gnuplot
	echo 'set ylabel "# Connections"' >> .gnuplot
	if ($DEBUG == ON) echo "show ylabel" >> .gnuplot
	echo -n 'plot "' >> .gnuplot
	echo $OUTF >> .gnuplot
	awk '{print $4}' $LOGSOURCE | colrm 3 50 | sort | \
	   uniq -c | awk '{print $2 " " $1}' > $OUTF
	set OUTMODE = 2
   breaksw
   case 20:
	echo -n "Are you on an X11 Display?"
	set X11DISP = ($<)
	switch ($X11DISP)
		case [y,Y]:
			echo "set term X11" > .gnuplot
			echo "term set to X11"
		breaksw
		case [n,N]:
			echo "set term dumb" > .gnuplot
			echo "term assumed to be dumb"
		breaksw
	endsw
	echo "set data style linespoints" >> .gnuplot
	if ($DEBUG == ON) echo "show data style" >> .gnuplot
	echo "set xrange [0:23]" >> .gnuplot
	if ($DEBUG == ON) echo "show xrange" >> .gnuplot
	echo "set noautoscale x" >> .gnuplot
	echo "set autoscale y" >> .gnuplot
	echo 'set xlabel "hours"' >> .gnuplot
	if ($DEBUG == ON) echo "show xlabel" >> .gnuplot
	echo 'set ylabel "# Connections"' >> .gnuplot
	if ($DEBUG == ON) echo "show ylabel" >> .gnuplot
	echo -n 'plot "' >> .gnuplot
	echo $OUTF >> .gnuplot
	grep "$GDATE" $LOGSOURCE | awk '{print $4}' | colrm 3 50 |\
	sort | uniq -c | awk '{print $2 " " $1}' > $OUTF 
	set OUTMODE = 2
   breaksw
   case 21:
   	echo -n "What machine: "
	set MACHINE = ($<)
	if ($MACHINE == "") goto CLEAN
	echo -n "Are you on an X11 Display?"
	set X11DISP = ($<)
	switch ($X11DISP)
		case [y,Y]:
			echo "set term X11" > .gnuplot
			echo "term set to X11"
		breaksw
		case [n,N]:
			echo "set term dumb" > .gnuplot
			echo "term assumed to be dumb"
		breaksw
	endsw
	echo "set data style linespoints" >> .gnuplot
	if ($DEBUG == ON) echo "show data style" >> .gnuplot
	echo "set xrange [0:6]" >> .gnuplot
	if ($DEBUG == ON) echo "show xrange" >> .gnuplot
	echo "set noautoscale x" >> .gnuplot
	echo "set autoscale y" >> .gnuplot
	echo 'set xlabel "day of the week (monday=0)"' >> .gnuplot
	if ($DEBUG == ON) echo "show xlabel" >> .gnuplot
	echo 'set ylabel "# Connections"' >> .gnuplot
	if ($DEBUG == ON) echo "show ylabel" >> .gnuplot
	echo -n 'plot "' >> .gnuplot
	echo $OUTF >> .gnuplot
	awk '$7 ~ /'$MACHINE'/ {print $1}' $LOGSOURCE | sort | uniq -c |\
	   awk '{print $2 " " $1}' > $GTMP
	endif
	echo -n "0 " > $OUTF
	awk '$1 ~ /Mon/' $GTMP | wc -l >> $OUTF
	echo -n "1 " >> $OUTF
	awk '$1 ~ /^Tue/' $GTMP | wc -l >> $OUTF
	echo -n "2 " >> $OUTF
	awk '$1 ~ /^Wed/' $GTMP | wc -l >> $OUTF
	echo -n "3 " >> $OUTF
	awk '$1 ~ /^Thu/' $GTMP | wc -l >> $OUTF
	echo -n "4 " >> $OUTF
	awk '$1 ~ /^Fri/' $GTMP | wc -l >> $OUTF
	echo -n "5 " >> $OUTF
	awk '$1 ~ /^Sat/' $GTMP | wc -l >> $OUTF
	echo -n "6 " >> $OUTF
	awk '$1 ~ /^Sun/' $GTMP | wc -l >> $OUTF
	set OUTMODE = 2
   breaksw
   case 22:
   	echo -n "What machine: "
	set MACHINE = ($<)
	if ($MACHINE == "") goto CLEAN
	echo -n "Are you on an X11 Display?"
	set X11DISP = ($<)
	switch ($X11DISP)
		case [y,Y]:
			echo "set term X11" > .gnuplot
			echo "term set to X11"
		breaksw
		case [n,N]:
			echo "set term dumb" > .gnuplot
			echo "term assumed to be dumb"
		breaksw
	endsw
	echo "set data style linespoints" >> .gnuplot
	if ($DEBUG == ON) echo "show data style" >> .gnuplot
	echo "set xrange [0:23]" >> .gnuplot
	if ($DEBUG == ON) echo "show xrange" >> .gnuplot
	echo "set noautoscale x" >> .gnuplot
	echo "set autoscale y" >> .gnuplot
	echo 'set xlabel "hours"' >> .gnuplot
	if ($DEBUG == ON) echo "show xlabel" >> .gnuplot
	echo 'set ylabel "# Connections"' >> .gnuplot
	if ($DEBUG == ON) echo "show ylabel" >> .gnuplot
	echo -n 'plot "' >> .gnuplot
	echo $OUTF >> .gnuplot
	awk '$7 ~ /'$MACHINE'/ {print $4}' $LOGSOURCE | colrm 3 50 |sort -n |\
	   uniq -c | awk '{print $2 " " $1}' | sort -n  > $OUTF
	set OUTMODE = 2
   breaksw
   default
	goto CLEAN
   breaksw
endsw



OUTPUT:
#  Ok, we got the work done--now lets output the baby, show what we can do!

if ($DEBUG == ON) echo "Entering OUTPUT"

# show the nice people the output

if ($OUTMODE == 1) then
	if ( -z $OUTF ) then
	   echo "This report produced no data"
	   /bin/rm $GTMP $OUTF 
	   goto LOOP
	else
   	   if ($DEBUG != ON) clear
   	   more $OUTF
   	   /bin/rm -f gtmp1 $GTMP
   	   if ($DEBUG == ON) echo "output : successful"
	endif
endif

if ($OUTMODE == 2) then
   if ( -z $OUTF ) then
      echo "This report produced no data"
      /bin/rm -f gtmp1 .gnuplot $OUTF $GTMP
      goto LOOP
   else
      if ($DEBUG != ON) clear
      echo ""
      echo ""
      echo "You are going to enter gnuplot."
      echo "when finished, type: 'exit' at the gnuplot> prompt."
      echo ""
      echo -n "Hit <ENTER> to view the graph."
      set VGRPH = ($<)
      $GNUPLOT
      /bin/rm -f gtmp1 .gnuplot $OUTF $GTMP
      if ($DEBUG == ON) echo "output : successful"
   endif
endif



LOOP:
# COntinue with other reports?

if ($DEBUG == ON) echo "Entering LOOP"
echo -n "Would you like another analysis report?"
set GOAGAIN = ($<)
switch ($GOAGAIN)
	case [y,Y]:
		unset REPNUM
		unset REPSEL
		unset REPTYP
		goto FIRSTOUTFILE
	breaksw
	case [n,N]:
		echo "OK, exiting."
		goto CLEAN
	breaksw
	default
		unset REPNUM
		unset REPSEL
		unset REPTYP
		goto FIRSTOUTFILE
	breaksw
endsw


USAGE:
if ($DEBUG == ON) echo "Entering USAGE"
# tell the nice people about how to use gla

if ($DEBUG != ON) clear
echo ""
echo ""
echo "	        Gopher Log Analysis Script	-	v1.0"
echo ""
echo "   Usage: glas	                         - brief menus available  "
echo "          glas h                            - this help message "
echo "          glas [output file]                - specifies output file"
echo ""		
echo -n "DO you want to see a summary of the available reports? "
set RESP = ($<)
if ($DEBUG == ON) echo "goto summary statement entered"
switch ($RESP)
   case [y,Y]:
	goto REPSUMM
   breaksw
   case [n,N]:
	echo ""
	echo "OK, Exiting."
	echo ""
	goto END
   breaksw
endsw



REPSUMM:
# for command-line report numbers.

if ($DEBUG == ON) echo "Entering REPSUMM"
if ($DEBUG != ON) clear
echo "Report #	Menu option	Description"
echo "--------  ------------  ----------------"
echo "    1       T-D           List all directories retrieved from most to least often."
echo "    2       T-F           List all files retrieved from most to least often."
echo "    3       T-M           List all machines connected from most to least often."
echo "    4       T-W           List traffic in day-of-the-week summary"
echo "    5       T-H           List traffic in hourly summary"
echo "    6       D-D           List all directories retrieved today"
echo "    7       D-F           List all files retrieved today" 
echo "    8       D-M           List all machines that connected today"
echo "    9       D-T           List the hourly usage for today"
echo "   10       H-D           List all directories retrieved by a specific host"
echo "   11       H-F           List all files retrieved by a specific host"
echo "   12       H-H           List hourly connections by specific host"
echo "   13       H-W           List day-of-the-week connections by specific host"
echo "   14       A-D           List average retrieval of specific directory"
echo "   15       A-F           List average retrieval of specific file"
echo "   16       A-M           List average connection of specific host"
echo "   17       A-T           List average connection altogether"
echo "   18       G-W           Graph day-of-the-week traffic summary"
echo "   19       G-H           Graph hourly traffic summary"
echo "   20       G-T           Graph Today's hourly summary"
echo "   21       G-D           Graph day-of-the-week summary for specific host"
echo "   22       G-S           Graph hourly summary for specific host"
goto END



CLEAN:
# clean up and get out of here...

if ($DEBUG == ON) echo "Entering CLEAN"
if ( -e $GTMP ) /bin/rm -f $GTMP
if ( -e $OUTF ) then
  echo -n "[default = remove] [R]emove $OUTF or [M]aintain it for future reference?"
  set RESP = ($<)
  if ($DEBUG == ON) echo "cleanup options statement entered"
  switch ($RESP)
      case [r,R]:
	/bin/rm -f $OUTF
	echo "Done."
      breaksw
      case [m,M]:
	echo "Leaving $OUTF intact"
      breaksw
   endsw
endif
	

END:
if ($DEBUG == ON) echo "Entering END"
# get the nice people out of here.
exit
