#!/bin/bash              
if [ $# -lt 4 ]
then
	echo usage:"extr_max [-l] -g <graph_type> -f <filename> -a <alg> <alg>..."
	exit
fi

algs_specified="False"
algs=""
print_type="plain"

while [ $# -gt 0 ]
do
	case "$1" in
        -l)     print_type="latex";;
	-f)	shift; file=$1;;
	-g)	shift; graph=$1;;
	-a)	shift; algs_specified="True"; 
		while [ $# -gt 0 ]
		do
			algs="$algs $1"
			shift
		done;;
	 *)	echo argument error;;
	esac
	shift
done

direct="$HOME/partitioning/max_cut/output"
direct1="$HOME/bin"

cd $direct
if [ $algs_specified = "True" ]
then
	algs_to_find="$algs"
else
	algs_to_find=`ls`
fi

for i in $algs_to_find
do
	if [ -d $i ]
	then
		case "$i" in
		rand_kl)		alg_class="kl";;
		line_kl)		alg_class="kl";;
		w_sim_anneal)		alg_class="kl";;
		rand_sim_anneal)	alg_class="kl";;
		line_sim_anneal)	alg_class="kl";;
	 	*)			alg_class="bk";;
		esac

		if [ $alg_class = "kl" ]
		then
			field="8"
		else
			field="6"
		fi

		cd $i/$graph
		files=`ls $file 2>/dev/null`
		if [ "$files" != "" ]
		then

			set `ls $file`

			while [ $# -gt 0 ]
			do

			   if [ -f $1 ]
			   then
				value=`conf_int $i max_cut $graph $1`
				lines=`wc -l $1 | gawk '/./ {print $1}'`
				echo "$i $1 $lines $value" | \
                                if [ $print_type = "plain" ]
                                then
					gawk '/./ {  
				  printf "%-6s %-16s %-4d %-10.4f [%-8.4f,%-8.4f] (%-10.4f/%-10.4f)\n", $1, $2, $3, $4, $5, $6, $7, $8 }'
				else
					gawk '/./ {  
				  printf "%-6s \& %10.4f \& %8.4f,%8.4f \& (%-10.2f/%-10.2f)\\\\\\hline\n", $1, $4, $5, $6, $7, $8 }'
				fi

			   fi
			   shift	
			done
		fi
		cd ../..
	fi
done
