#
#	$Header: snmpgraph,v 4.300 91/06/09 01:00:06 root Rel41 $
#	Copyright 1990, Performance Systems International Inc.
#	Copyright 1989, NYSERNet, Inc.
#	Copyright 1990, Sony Corp.
#	All Rights Reserved.
#	Created February 1989, William Caloccia
#
set	ReportRoot=/export/DIST/snmp/report				# 
#
#
set	DailyBin=$ReportRoot/daily/bin		# Depends on ReportRoot
set	SedAwk=$ReportRoot/plot/bin/sedawk	# Depends on ReportRoot
set	Data=/export/DIST/snmp/report/data				# where the raw data is
						# same as /export/DIST/snmp/report/data in report-env

set	PlotDir=$Data/plot			# Depends on Data

#
#
set	prgm=$0

if ($#argv  < 1) then
  echo "Usage: $prgm:t {-Z} <poll_data_file>"
  exit 
endif

#	any of the following may be altered by input from user

set	VARS=( {In,Out}Octets {In,Out}UcastPkts)
set	XSCALE=( -x 0 104 4 )
set	GRAPH=(graph -b -g 1 $XSCALE )
set	PLOTPRGM=psplot
set	TMP=/tmp/$$
set	OUT=$TMP.csh
set	GENERATE	PLOT

while ( "$argv[1]" =~ -* )	# process input from user (for marty)

  if ($?Echo) echo '  Argv :=' $argv[*]

  switch ("$argv[1]")

  case "-date":				# specify data directory
  case "-d"
	shift; set ext=$argv[1]; breaksw;

  case "-xpand":			# file name expansion
  case "-x":
	set	EXPAND; breaksw;

  case "-noprint":			# don't actually print
  case "-np":
	set NoPrint ;	breaksw ;

  case "-compress":			# compress when not found that way
  case "-Z":
	set DOCOMPRESS ;	breaksw ;

  case "-verbose":			# usual, for debugging
  case "-v":
	set verbose ; # NO BREAKSW !!!	# also will set echo
					# Position dependent
  case "-echo":				# status or debugging
  case "-e"
	set Echo ; breaksw;

  case "-log"				# plot type (default)
  case "-l":
	set dolog ;	breaksw ;

  case "-dual":				# plot type
  case "-d":
	set dodual ; breaksw ;

  case "-single":			# plot type
  case "-s":
	set dosingle ;	breaksw ;

  case "-all":				# plot type
  case "-a":
	set dolog ; set dodual ; set dosingle ;	breaksw ;

  case "-plotter":			# specify plot(1g) filter
  case "-p":
	shift ; set PLOTPRGM=($argv[1]) ; breaksw;

  #  the following arguments allow the gifcs script to be run
  #  automatically, producing the points to plot for each variable,
  #  and leaving this data (input to graph(1g) in a specified directory
  #  and then allowing operators to choose how they want this data
  #  displayed.  This is much more flexible than the present incarnation
  #  of ''graphdaily'' which is actually this script after a total
  #  lobotomy.

  case "-Dailysave":
  case "-DS":
	set ext=$argv[2]
	set	DailySave Daily
	unset	PLOT
	set	TMP=$PlotDir/$ext
	set	OUT=$PlotDir/$ext/plotall
	breaksw;

  case "-Dailyplot":
  case "-DP":
	set	ext=$argv[2]
	set	DailyPlot Daily
	unset	GENERATE
	set	TMP=$PlotDir/$ext
	set	OUT=/tmp/plot.csh.$ext.$$
	breaksw;

  # the following variables are not supported in a short form,
  # because they drastically alter the behaviour of the script

  case "-graph":
	shift ; set GRAPH="$argv[1]" ; breaksw;

  case "-xscale":			# the following must have arguments
	shift ; set XSCALE=( -x $argv[1]); breaksw;

  case "-vars":				# must be quotated if more than one
	shift ; set VARS=($argv[1]) ; breaksw;

  default:
	echo "$prgm:t don't understand option: $argv[1]" 
	exit
	breaksw
  endsw
  shift
end

if ( $?dolog == 0 && $?dosingle == 0 && $?dodual == 0 ) then
  set	dolog
endif


set	LOG=(-y l)
set	LOGSCALE=(awk -f $SedAwk/yscale.l.awk )	# graph(1g) arguments
set	SCALE=(awk -f $SedAwk/yscale.awk )

#	ok, let's get to work...
if ($?ext) then
  cd	$Data/$ext
endif
if ($?Daily) then 
  if ($?DailyPlot && $#argv > 1) then
    shift
    set	FILES=($argv[*])
  else
    set	FILES=(*-*)
  endif
else if ($?ext) then
  cd	$Data/$ext
  set	FILES=($argv[*])
else
  set	FILES=($argv[*])
endif
if ($?EXPAND) then
  set	InPut=($FILES)
  set	FILES
  while ($#InPut)
    set	FILES=( *${InPut[1]}* $FILES )
    shift	InPut
  end
endif

if ($?GENERATE) then
  mkdir	$TMP
  
  foreach file ($FILES)				#   awk for points
    unset	COMPRESS
    if ($?Echo) echo  '  Input:  '$file
    if ( $file:e == "Z") then
      uncompress	$file
      set	file=$file:r
      set COMPRESS
    endif
    set	ifc=$TMP/$file
    awk -f	$SedAwk/plot.1.awk $file >! $ifc
  
    foreach v ($VARS)				#     seperate points by type
      grep $v $ifc | sed "s/$v	//" | \
        awk -f	$SedAwk/plot.2.awk 	>! $ifc.$v
      echo $v >> $ifc.$v
    end
    grep '"'	$ifc	>! $ifc.errs		#   seperate errors
    if ($?DOCOMPRESS || $?COMPRESS)	compress $file
    if ($?DailySave)	/bin/rm $ifc
  end
endif
  
if ($?PLOT) then
  cat	/dev/null >! $OUT
  chmod	755	$OUT
  set date
  foreach name ($FILES)				# foreach ifc
    if ($?Daily) then				#   set up label
      if ($?ext) then
        set date=(`$DailyBin/prsdate $ext`)
        set label="$name on $date"
      else
	set label="$name (date not available)"
      endif
    else					#    must extract date
      if ( $name:e == "Z") then
	set date=(`zcat $name | head -1`)
	set name=$name:r
      else
	set date=(`head -1 $name`)
      endif
      if ($#date > 4) then 
	set label="$name on $date[1-3] $date[5]"
      else
	set label="$name (date not available)"
      endif
    endif
    if ($?Echo) echo '  Out: '$label
  
    set	ifc=$TMP/$name				#   do plot
  
    if ($?dolog) then
      echo "cat $ifc.* | $LOGSCALE | $GRAPH $LOG  -l "'"'$label'"'" | $PLOTPRGM" >> $OUT
    endif
  
    if ($?dodual) then
      echo "cat $ifc.*Octets | $SCALE | $GRAPH -l "'"'$label'  Bytes"'" | $PLOTPRGM" >> $OUT
      echo "cat $ifc.*Pkts | $SCALE | $GRAPH -l "'"'$label'  Packets"'" | $PLOTPRGM" >> $OUT
    endif
  
    if ($?dosingle) then
      foreach v ($VARS)
        echo "cat $ifc.$v | $SCALE | $GRAPH -l "'"'$label $v '"'" | $PLOTPRGM" >> $OUT
      end
    endif

  end

  if ($?NoPrint == 0) then
  	$OUT | lpr -Pps
  	if ($?Daily == 0) /bin/rm -rf $TMP $TMP.csh
  else
    echo "$OUT | lpr -Pps"
  endif
endif
exit
