#!/bin/bash              
ulimit -c 0
if [ $# -lt 4 ]
then
	echo "usage:  interval_plot <directory> <random | geometric> <input_file> <alg> <alg>  ..."
	echo "ex   :  interval_plot max_cut random I10000.011.256 kl wmkl"
	echo "ex   :  interval_plot min_quotient random I10000.011.256 kl wmkl"
	echo "ex   :  interval_plot min_cut random I10000.011.256 kl wmkl"
	echo 
	exit
fi

#likely bug fix:  check the output of extr4 and extr6.  If two fields are
#produced with no space between them, interval_plot will bomb

direct="$1"
mode="$1"
graph="$2"
file="$3"
shift;shift;shift;
algs="$*"

if [ $mode = "max_cut" ]
then
	prefix=""
elif [ $mode = "min_quotient" ]
then
	prefix="m"
else
	prefix="mc"
fi

com_file="/tmp/int.com"
point_file="/tmp/int.points"
point_file2="/tmp/int.points2"

if [ -f $prefix$file.ps ]
then
	echo "error:  $prefix$file.ps already exists"
	exit
fi

for i in $com_file $point_file $point_file2
do
	if [ -f $i ]
	then
		rm $i
	fi
done

echo "set terminal post \"Symbol\" 12" | cat > $com_file
echo "set size .6,.3" | cat >> $com_file
echo "set nokey" | cat >> $com_file
echo "set noytics" | cat >> $com_file
#echo "set xlabel \"\\% edges cut\"" | cat >> $com_file
echo "set format y\"\"" | cat >> $com_file
echo "set yrange [0:.5]" | cat >> $com_file
echo "set boxwidth 0.0" | cat >> $com_file

if [ $mode = "max_cut" ]
then
	extr="extr_max"
elif [ $mode = "min_quotient" ]
then
	extr="extr_min"
else
	extr="extr_min -c"
	direct="min_quotient"
fi

y=0.1

lowest=100.0
highest=0.0

for i in $algs
do
	if [ ! -f $HOME/partitioning/$direct/output/$i/$graph/$file ]
	then
		echo "warning: $direct/output/$i/$graph/$file doesn't exist"
		continue
	fi
        case "$i" in
                wk)     key="w";;
                kl)     key="k";;
                lkl)    key="k";;
                rs)     key="k";;
                sa)     key="s";;
                lsa)    key="s";;
                klo)    key="k";;
                klp3)   key="k";;
                klp3o)  key="k";;
                *)      key="r";;
        esac

	res=`$extr -g $graph -f $file -a $i`
	lower=`echo $res | gawk 'BEGIN {FS="[\]\[, ]+"} /./ {print $5}'`
	higher=`echo $res | gawk 'BEGIN {FS="[\]\[, ]+"} /./ {print $6}'`
	#lower=`gawk 'BEGIN {FS="[\]\[, ]+"} /./ {print $5}' res`
	#higher=`gawk 'BEGIN {FS="[\]\[, ]+"} /./ {print $6}' res`
	echo "$lower $y 0.0" >> $point_file
	echo "$higher $y 0.0" >> $point_file
	echo "" >> $point_file
	echo "$lower 0.5" >> $point_file2
	echo "$lower 0" >> $point_file2
	echo "" >> $point_file2
	echo "$higher 0.5" >> $point_file2
	echo "$higher 0" >> $point_file2
	echo "" >> $point_file2
	ave=`echo "($higher + $lower) / 2" | bc -l`
	y=`echo "$y + .04" | bc -l`
	echo "set label \"m\" at $ave, $y center" >> $com_file
	ave2=`echo "($higher - $lower) / 60" | bc -l`
	ave3=`echo "$ave + (2*$ave2)" | bc -l`
	y1=`echo "$y - .02" | bc -l`
	echo "set label \"$key\" at $ave3, $y1 left" >> $com_file
	lowest=`echo "{if ($lower < $lowest) $lower; if ($lower >= $lowest) $lowest}" | bc -l`
	highest=`echo "{if ($higher < $highest) $highest; if ($higher >= $highest) $higher}" | bc -l`
	y=`echo "$y + .06" | bc -l`
done

xrange=`echo "($highest - $lowest)" | bc -l`
xlow=`echo "$lowest-($xrange/8)" | bc -l`
xhigh=`echo "$highest+($xrange/8)" | bc -l`

echo "set xrange [$xlow:$xhigh]" 
echo "set xrange [$xlow:$xhigh]" >> $com_file
echo "set yrange [0:.5]" >> $com_file
echo "plot \"$point_file\" w lines 1, \"$point_file2\" w lines 3" >> $com_file
#echo "plot \"$point_file\" w lines, \"$point_file\" w errorbars 3 " >> $com_file
#echo "plot \"$point_file\" w lines 2, \"$point_file\" w boxerrorbars  1" >> $com_file

gnuplot < $com_file > $prefix$file.ps

#for i in $com_file $point_file $point_file2 
#do
#	rm $i
#done

sed 's/BoundingBox: 50 50/BoundingBox: 50 90/' $prefix$file.ps > /tmp/d
mv /tmp/d $prefix$file.ps

echo "result is $prefix$file.ps"
