#!/bin/bash
TEMP=`mktemp`

PLOTTYPE=lines
USING=
declare -a TITLES
FORMAT="postscript color"
PLOTCOMMAND=standard

while [ $# -gt 0 -a "$FINISHED" != "yes" ]; do
	case $1 in
		--format)
			FORMAT="$2"				>> $TEMP
			shift 2
			;;
		--output)
			echo "set terminal $FORMAT"		>> $TEMP
			echo "set output '$2'"			>> $TEMP
			shift 2
			;;
		--title)
			echo "set title '`echo $2 | sed -e 's/_/\\\_/g'`'" >> $TEMP
			shift 2
			;;
		--header)
			cat $2 >> $TEMP
			shift 2
			;;
		--xlabel)
			XLABEL="$2"
			shift 2
			;;
		--ylabel)
			YLABEL="$2"
			shift 2
			;;
		--simple|--simple-filter|--simple-samples)
			PLOTTYPE="lines"
			shift
			;;
		--simple-filter-points)
			PLOTTYPE="points"
			shift
			;;
		--linespoint)
			PLOTTYPE="linespoint"
			shift
			;;
		--points)
			PLOTTYPE="points"
			shift
			;;
		--operation-candlesticks)
			OPER_CANDLESTICKS="yes"
			PLOTTYPE="candlesticks"
			USING="1:3:4:5:6:xtic(2)"
			shift
			;;
		--client-candlesticks)
			CLIENT_CANDLESTICKS="yes"
			PLOTTYPE="candlesticks"
			USING="1:2:3:4:5"
			shift
			;;
		--client-errorbars)
			CLIENT_ERRORBARS="yes"
			PLOTTYPE="errorbars"
			shift
			;;
		--client-errorlines)
			PLOTTYPE="errorlines"
			XRANGE="0:"
			shift
			;;
		--single-candlesticks)
			SINGLE_CANDLESTICKS="yes"
			PLOTTYPE="candlesticks"
			USING="1:2:3:4:5"
			shift
			;;
		--candlesticks)
			CANDLESTICKS="yes"
			PLOTTYPE="candlesticks"
			USING="1:2:3:4:5"
			shift
			;;
		--large)
			echo set term $FORMAT size 1200,1200 >> $TEMP
			shift
			;;
		--wide)
			echo set term $FORMAT size 1200,600 >> $TEMP
			shift
			;;
		--histogram)
			HISTOGRAM="yes"
			USING="2:xtic(1)"
			PLOTTYPE="histogram"
			shift
			;;
		--histogram-time)
			HISTOGRAM="yes"
			USING="2:xtic(1)"
			PLOTTYPE="histogram"
			YRANGE="0:"
			shift
			;;
		--stacked-histogram)
			STACKED_HISTOGRAM=yes
			shift
			;;
		--plottype)
			PLOTTYPE=$2
			shift 2
			;;
		--extra)
			EXTRA=$2
			shift 2
			;;
		--smooth)
			SMOOTH="smooth $2"
			shift 2
			;;
		--dump)
			DUMP=yes
			shift
			;;
		--logX)
			LOGX=yes
			if [ "$XRANGE" = "0:" ]; then
				XRANGE="1:"
			fi
			shift
			;;
		--logY)
			LOGY=yes
			shift
			;;
		--logscaleX)
			LOGSCALEX=yes
			shift
			;;
		--logscaleY)
			LOGSCALEY=yes
			shift
			;;
		--titles)
			TIFS=$IFS
			IFS=","
			COUNT=0
			for TITLE in $2; do
				TITLE=`echo $TITLE | sed -e 's/_/\\\_/g'`
				TITLES[$COUNT]="$TITLE"
				COUNT=$(($COUNT+1))
			done
			shift 2
			IFS=$TIFS
			;;
		--xrange)
			XRANGE=$2
			shift 2
			;;
		--yrange)
			YRANGE=$2
			shift 2
			;;
		--xtics)
			XTICS=$2
			shift 2
			;;
		--subreport)
			SUBREPORT=$2
			shift 2
			;;
		*)
			FINISHED="yes"
			;;
	esac
done

FIRST=yes
#XRANGE=1
if [ "$CLIENT_CANDLESTICKS" = "yes" -o "$CLIENT_ERRORBARS" = "yes" ]; then
	echo set boxwidth 0.2 >> $TEMP
fi
if [ "$SINGLE_CANDLESTICKS" = "yes" ]; then
	echo set boxwidth 0.2 >> $TEMP
fi
if [ "$HISTOGRAM" = "yes" ]; then
	echo set style data histogram       >> $TEMP
	echo set xtic rotate by -45         >> $TEMP
	echo set style fill solid border -1 >> $TEMP
fi
if [ "$XTICS" != "" ]; then
	echo set xtics $XTICS               >> $TEMP
	echo set grid xtics                 >> $TEMP
fi
if [ "$OPER_CANDLESTICKS" = "yes" ]; then
	echo "set xtic rotate by -45" >> $TEMP
	echo set boxwidth 0.2 >> $TEMP
fi
if [ "$STACKED_HISTOGRAM" = "yes" ]; then
	echo set style histogram rowstacked >> $TEMP
	echo set style data histograms  >> $TEMP
	echo set boxwidth 0.75 absolute >> $TEMP
	echo set style fill solid 1.00 border -1 >> $TEMP
	PLOTCOMMAND=stacked-histogram
fi
if [ "$XRANGE" != "" ]; then
	echo "set xrange [$XRANGE]" >> $TEMP
fi
if [ "$YRANGE" != "" ]; then
	echo "set yrange [$YRANGE]" >> $TEMP
fi
if [ "$XLABEL" != "" ]; then
	XLABEL=`echo $XLABEL | sed -e 's/_/\\\_/g'`
	echo "set xlabel '$XLABEL'" >> $TEMP
fi
if [ "$YLABEL" != "" ]; then
	YLABEL=`echo $YLABEL | sed -e 's/_/\\\_/g'`
	echo "set ylabel '$YLABEL'" >> $TEMP
fi
if [ "$LOGX" = "yes" ]; then
	echo "set log x 2" >> $TEMP
fi
if [ "$LOGY" = "yes" ]; then
	echo "set log y 20" >> $TEMP
fi
if [ "$LOGSCALEX" = "yes" ]; then
	echo "set logscale x 2" >> $TEMP
fi
if [ "$LOGSCALEY" = "yes" ]; then
	echo "set logscale y 10" >> $TEMP
fi

if [ "$EXTRA" != "" ]; then
	if [ "`grep x2tics $EXTRA`" != "" ]; then
		MINX=`grep "set x2tics" $EXTRA | awk -F '(' '{print $2}' | awk -F ')' '{print $1}' | tr ',' ' ' | awk '{print $2}'`
		MAXX=`grep "set x2tics" $EXTRA | awk -F '(' '{print $2}' | awk -F ')' '{print $1}' | tr ',' ' ' | awk '{print $NF}'`
		echo "set xrange [$MINX:$MAXX]" >> $TEMP
		echo "set x2range [$MINX:$MAXX]" >> $TEMP
	fi
		
	cat $EXTRA >> $TEMP
fi
echo "plot \\"				>> $TEMP

FIRST=yes
COUNT=0
CLEANUP_FILES=
while [ "$1" != "" ]; do
	PLOTFILE=$1
	if [ "$FIRST" = "no" ]; then
		echo ", \\"		>> $TEMP
	fi
	FIRST=no
	TITLE=
	if [ "${TITLES[$COUNT]}" != "" ]; then
		TITLE="title '${TITLES[$COUNT]}'"
	fi

	TIME_OFFSET=
	USING_COMMAND=

	if [ "$USING" != "" -o "$TIME_OFFSET" != "" ]; then
		USING_COMMAND="using $TIME_OFFSET $USING"
	fi

	# Add index information for candlesticks if necessary
	if [ "$CANDLESTICKS" = "yes" -o "$SINGLE_CANDLESTICKS" = "yes" ]; then
		DATACOUNT=`cat $PLOTFILE | wc -l`
		if [ $DATACOUNT -eq 1 ]; then
			DATA=`cat $PLOTFILE`
		else
			DATA=`cat $PLOTFILE | sed -e 's/^[0-9]*//'`
		fi
		echo "$COUNT $DATA" > $PLOTFILE.tmp.$$
		PLOTFILE=$PLOTFILE.tmp.$$
		CLEANUP_FILES="$CLEANUP_FILES $PLOTFILE"
	fi

	# Some versions of gnuplot spaz out smooting two data points
	SMOOTH_ACTUAL=$SMOOTH
	DATA_POINTS=`cat $PLOTFILE | wc -l`
	if [ $DATA_POINTS -le 2 ]; then
		SMOOTH_ACTUAL=
	fi

	# Work out an individual plot string
	case $PLOTCOMMAND in
	standard)
		PLOTSTRING="'$PLOTFILE'"
		if [ "$AWK_FORMAT" != "" ]; then
			ESCAPED=`echo "$PLOTFILE" | sed -e 's/\//\\\\\//g'`
			PLOTSTRING="\"`echo $AWK_FORMAT | sed -e "s/@@PLOT@@/$ESCAPED/"`\""
		fi
		echo -n "$PLOTSTRING $USING_COMMAND $SMOOTH_ACTUAL $TITLE with $PLOTTYPE$PLOTEXTRA" >> $TEMP
		;;
	stacked-histogram)
		PLOTSTRING="'$PLOTFILE'"
		echo -n "'$PLOTFILE' using 2 t \"Alloc\", '$PLOTFILE' using 3 t \"Free\"" >> $TEMP
		;;
	esac
	shift
	COUNT=$(($COUNT+1))
done
echo >> $TEMP

cat $TEMP | gnuplot -name gnuplot -persist
rm $TEMP
if [ "$CLEANUP_FILES" != "" ]; then
	rm $CLEANUP_FILES
fi
