#!/bin/ksh
#
#set -x
#
##############################################################################
#                                                                            #
#  RCS Filename : ergs,v
#  RCS Date     : 1996/08/12 09:24:22
#  RCS Revision : 1.10
#  RCS Author   : lamberts
#  RCS State    : V2_0_B
#                                                                            #
#  Authors: Stefan Lamberts                                                  #
#                                                                            #
##############################################################################

######################################################################
# Defaults
######################################################################

WC=ALL

TYPE="avg"
WHAT="lat"
OTERM="X11"
INFILE="DATA/RESULTS"
SCALE="log"
PFILE="ergs.plot"

OPERATIONS="CWR CRD"
CSYNCS="S"
ISYNCS="S A"
OSYNCS="T F"
MODES="G L R S U"
CCOUNTS="1 2 4 8 16 32 48 64 96"
NODES="N"

######################################################################
# Usage
######################################################################

USAGE='Usage:\n'$0'\n
\t[-t {avg|min|max|erb}]\t\t# Default: '$TYPE'\n
\t[-w {lat|bdw}]\t\t\t# Latency | Bandwidth; Default: '$WHAT'\n
\t[-n]\t\t\t\t# Do not extract but use\n
\t\t\t\t\t# existing cache files\n
\t[-e]\t\t\t\t# Extract only, do not plot\n
\t[-o <output>]\t\t\t# Output file or directory\n
\t[-m {X11|eps|ps|fig}]\t\t# Output device; Default: '$OTERM'\n
\t[-s {log|lin|logx|logy}]\t# Scale of Axes; Default: '$SCALE'\n
\t[-r]\t\t\t\t# Set plot ranges; Default: off\n
\n
\t[-O "<operations>..."]\t\t# Set operations to be considered\n
\t\t\t\t\t# Default: '$OPERATIONS'\n
\t[-C "<client sync flags>..."]\t# Set client sync flags to be considered\n
\t\t\t\t\t# Default: '$CSYNCS'\n
\t[-I "<iod sync flags>..."]\t# Set iod sync flags to be considered\n
\t\t\t\t\t# Default: '$ISYNCS'\n
\t[-S "<O_SYNC flags>..."]\t# Set O_SYNC flags to be considered\n
\t\t\t\t\t# Default: '$OSYNCS'\n
\t[-M "<modes>..."]\t\t# Set modes to be considered\n
\t\t\t\t\t# Default: '$MODES'\n
\t[-N "<nodes>..."]\t\t# Set nodes to be considered\n
\t\t\t\t\t# Default: '$NODES'\n
\t[-U <Client count> ...]\t\t# Set Number of clients to be considered
\t\t\t\t\t# Default: '$CCOUNTS'\n
\n
\t{ AllSyncs | AllNodes | AllModes | AllCCounts | All }+'

####################################################################
#
#	GET COMANDLINE-OPTIONS
#
####################################################################

while getopts "t:w:o:enm:s:rO:C:I:S:M:N:U:" opt
do
	case $opt in
	t)	TYPE=$OPTARG;;
	w)	WHAT=$OPTARG;;
	o)	OUTFILE=$OPTARG;;
	e)	EXTRACT="T";;
	n)	EXTRACT="F";;
	m)	OTERM=$OPTARG;;
	s)	SCALE=$OPTARG;;
	r)	SETRANGE=T;;
	O)	OPERATIONS="$OPTARG";;
	C)	CSYNCS="$OPTARG";;
	I)	ISYNCS="$OPTARG";;
	S)	OSYNCS="$OPTARG";;
	M)	MODES="$OPTARG";;
	N)	NODES="$OPTARG";;
	U)	CCOUNTS="$OPTARG";;
	*)	echo $USAGE; exit 1;;
	esac
done

shift `expr $OPTIND - 1`

if [[ $# -lt 1 ]]
then
	echo $USAGE; exit 1
fi

case $TYPE in
avg|min|max|erb);;
*)		echo $USAGE; exit 1;;
esac

case $WHAT in
bdw|lat);;
*)		echo $USAGE; exit 1;;
esac			

case $OTERM in
eps|fig|ps|X11);;
*)		echo $USAGE; exit 1;;
esac

case $SCALE in
log|lin|logx|logy);;
*)	echo $USAGE; exit 1;;
esac

if [[ -n "$SETRANGE" ]]
then	if [[ ! -r $INFILE ]]
	then	echo "$0: Cannot read input file '$INFILE'"; exit 1
	fi
fi

######################################################################
# set plot_pfs options
######################################################################

OPT=""

if [[ "$EXTRACT" = "F" ]]
then	OPT="$OPT -n"
fi

OPT="$OPT -w $WHAT"

if [[ "$EXTRACT" != "T" ]]
then	OPT="$OPT -t $TYPE"
	OPT="$OPT -m $OTERM"
	OPT="$OPT -s $SCALE"
	OPT="$OPT -p $PFILE"
fi

######################################################################
# Set Minimum and Maximum for y and x values
######################################################################

if [[ "$EXTRACT" != "T" && -n "$SETRANGE" ]]
then
	case $WHAT in
	lat)	OPT="$OPT "`awk '
		BEGIN{miny=-1.0;minx=-1.0;maxy=-1.0;maxx=-1.0}
		/^[^#]/{if ((miny < 0.0)||(miny > $12)) miny=$12
			if (maxy < $13) maxy=$13
			if ((minx < 0.0)||(minx > $11)) minx=$11
			if (maxx < $11) maxx=$11}
		END{print "-y",miny,"-Y",maxy,"-x",minx,"-X",maxx}' $INFILE`;;
	bdw)
		OPT="$OPT "`awk '
		BEGIN{miny=-1.0;minx=-1.0;maxx=-1.0;maxy=-1.0}
		/^[^#]/{if ((miny<0.0)||(miny>($10/$12)) miny=($10/$12)
			if (maxy<($10/$11)) maxy=($10/$11)
			if ((minx<0.0)||(minx>$10)) minx=$10
			if (maxx<$10) maxx=$10}
		END{print "-y",miny,"-Y",maxy,"-x",minx,"-X",maxx}' $INFILE`;;
	esac
fi

######################################################################
# plot_pfs command
######################################################################

if [[ "$EXTRACT" = "T" ]]
then	P="./gd $OPT"
else	P="./plot_pfs $OPT"
fi

######################################################################
# plot descriptions
######################################################################

plot () # DSC
{
	if [[ "$EXTRACT" != "T" && -n "$OUTFILE" ]]
	then	oOPT="-o $OUTFILE$FNAME"
	else	oOPT=""
	fi

#	set -x
	$P $oOPT $*
}

######################################################################
# Set number of machines
######################################################################
setma () # CC
{
	MA=""
	if [[ -r $INFILE.MA ]]
	then    MA=`awk '$1 == '$1'{print $2}' $INFILE.MA`
	fi
	if [[ "$MA" = "" ]]
	then	awk '
		/^[^#]/{ma[$3]=$2}
		END{for (i in ma) print i,ma[i]}' $INFILE > $INFILE.MA
		MA=`awk '$1 == '$1'{print $2}' $INFILE.MA`
	fi
	echo $MA
}

######################################################################
# Plot with wildcards
######################################################################

wildcard () # OP CS IS OS MD CC ND
{
#	set -x
	if [[ $# -ne 7 ]]; then return 1; fi

	if [[ $1 = $WC ]]
	then OPS=$OPERATIONS;	OPSF=$WC
	else OPS=$1;		OPSF=`echo $1 | sed -e 's/[ ]+/-/g'`
	fi
	if [[ $2 = $WC ]]
	then CSS=$CSYNCS;	CSSF=$WC
	else CSS=$2;		CSSF=`echo $2 | sed -e 's/[ ]+/-/g'`
	fi
	if [[ $3 = $WC ]]
	then ISS=$ISYNCS;	ISSF=$WC
	else ISS=$3;		ISSF=`echo $3 | sed -e 's/[ ]+/-/g'`
	fi
	if [[ $4 = $WC ]]
	then OSS=$OSYNCS;	OSSF=$WC
	else OSS=$4;		OSSF=`echo $4 | sed -e 's/[ ]+/-/g'`
	fi
	if [[ $5 = $WC ]]
	then MDS=$MODES;	MDSF=$WC
	else MDS=$5;		MDSF=`echo $5 | sed -e 's/[ ]+/-/g'`
	fi
	if [[ $6 = $WC ]]
	then CCS=$CCOUNTS;	CCSF=$WC
	else CCS=$6;		CCSF=`echo $6 | sed -e 's/[ ]+/-/g'`
	fi
	if [[ $7 = $WC ]]
	then	NDS=$NODES
	else	NDS=$7
	fi
	if [[ $7 = $WC ]]
	then	NDS=$NODES
	else	NDS=$7
	fi
	NDSF=`echo $7 | sed -e 's/[ ]+/-/g'`

	DSC=""
	for op in $OPS
	do for cs in $CSS
	do for is in $ISS
	do for os in $OSS
	do for md in $MDS
	do for cc in $CCS
	do ma=`setma $cc`
	   for nd in $NDS
	do	if [[ $nd = "N" ]]
		then	DSC="$DSC $op:$cs:$is:$os:$md:$cc:$nd:$ma"
		elif [[ $nd = "C" ]]
		then	DSC="$DSC $op:$cs:$is:$os:$md:$cc:$nd:$ma"
		elif [[ $nd -lt $cc ]]
		then	DSC="$DSC $op:$cs:$is:$os:$md:$cc:$nd:$ma"
		fi
	done; done; done; done; done; done; done
	
	FNAME="$OPSF:$CSSF:$ISSF:$OSSF:$MDSF:$CCSF:$NDSF"
	
#	set -x
	if [[ -n "$DSC" ]]
	then	plot $DSC
	fi

	return 0
}

######################################################################
# Plot all Syncs
######################################################################

AllSyncs () #
{
#	set -x
	for ops in $OPERATIONS
	do for mds in $MODES
	do for ccs in $CCOUNTS
	do for nds in $NODES
	do wildcard $ops $WC $WC $WC $mds $ccs $nds
	done; done; done; done
}

AllNodes () #
{
	for ops in $OPERATIONS
	do for mds in $MODES
	do for css in $CSYNCS
	do for iss in $ISYNCS
	do for oss in $OSYNCS
	do for ccs in $CCOUNTS
	do wildcard $ops $css $iss $oss $mds $ccs $WC
	done; done; done; done; done; done
}

AllModes () #
{
	for ops in $OPERATIONS
	do for css in $CSYNCS
	do for iss in $ISYNCS
	do for oss in $OSYNCS
	do for ccs in $CCOUNTS
	do for nds in $NODES
	do wildcard $ops $css $iss $oss $WC $ccs $nds
	done; done; done; done; done; done
}

AllCCounts () #
{
#	set -x
	for ops in $OPERATIONS
	do for mds in $MODES
	do for css in $CSYNCS
	do for iss in $ISYNCS
	do for oss in $OSYNCS
	do for nds in $NODES
	do wildcard $ops $css $iss $oss $mds $WC $nds
	done; done; done; done; done; done
}

Test () #
{
	echo "Test"
}
All () #
{
#    set -x
    wildcard "$OPERATIONS" "$CSYNCS" "$ISYNCS" "$OSYNCS" "$MODES" "$CCOUNTS" "$NODES"
}


cat > $PFILE <<EOF
#!`type gnuplot | awk '{print $3}'`
#
# This file was generated by $0 `date`
#
#
EOF

for cmd in $*
do
	if typeset -f $cmd > /dev/null
	then
		eval $cmd
	else
		echo "$0: No function named '$cmd'."
	fi
done

chmod 755 $PFILE

./$PFILE

exit 0