#
#	$Header: snmpxgraph,v 4.300 91/06/09 01:00:09 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	XSCALE=( -lx 0,104 )
#set	GRAPH=(graph -b -g 1 $XSCALE )
set	GRAPH=(xgraph $XSCALE )

set	TMP=/tmp/$$
set	OUT=$TMP.out
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 ;

	#  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;
		shift ; set XSCALE=( -lx $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=-lny

#	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
			echo ""					>! $ifc.$v
			echo '"'$v'"'				>> $ifc.$v
			grep $v $ifc | sed "s/$v	//"	>> $ifc.$v
		end
		#
		#
		echo ""			>! $ifc.errs
		echo '"'"errs"'"'	>> $ifc.errs
		grep '"'	$ifc	>> $ifc.errs	# seperate errors

		#
		#
		if ($?DOCOMPRESS || $?COMPRESS)	compress $file
		#
		#
		if ($?DailySave)	/bin/rm $ifc
	end
endif

#
#
if ($?PLOT) then
	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 | tail -1`)
				set name=$name:r
			else
				set date=(`tail -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
			set	out=$OUT.$ifc:t.log
			cp /dev/null $out
			foreach file ($ifc.*)
				set v = $file:e
				cat $file	>>$out
			end
			$GRAPH $LOG -t "$label" $out &
		endif
	
		if ($?dodual) then
			foreach v (Octets Pkts)
				set	out=$OUT.$ifc:t.$v.dual
				cp /dev/null $out
				foreach file ($ifc.*$v)
					cat $file	>>$out
				end
				$GRAPH -t "$label" $out &
			end
		endif
	
		if ($?dosingle) then
			foreach v ($VARS)
				set	out=$OUT.$ifc:t.$v.single
				cp /dev/null $out
				cat $ifc.$v	>>$out
				$GRAPH -t "$label" $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
wait
rm -rf $TMP*
endif
exit
