#!/bin/bash

set -e
#set -x

usage() {
    cat >&2 <<EOF
Usage: $0 [-c] <parameters file>
Options:
    -h  print this help
    -n  dry-run mode (for debugging)
    -c  (continue) do not overwrite previous results
EOF
    exit $1
}

log() {
    echo "-#- $@" >&2
}

die() {
    echo "ERROR: $@" >&2
    exit 2
}

variable_check() {
    eval test -n "\${$1}" || die "undefined $1"
}

array_check() {
    eval test "\${#$1}" -gt 0 || die "undefined $1"
}

# read args
overwrite=1
debug=0
while getopts "chn" c; do
    case "$c" in
        'c') overwrite=0 ;;
        'h') usage 0 ;;
        'n') debug=1 ;;
        '?') usage 1 ;;
    esac
done
shift $((OPTIND - 1))
[ $# -eq 1 ] || usage 1
parameters="$1"

log "Running: $0 $@"
log "Hostname: $(hostname -f)"

declare -a TOPOLOGIES ALGORITHMS PLATFORMS COMMON_OPTS

# read parameters
log "Reading parameters from \"$1\"."
source "$parameters" || die "cannot read parameters file: \"$parameters\""

array_check TOPOLOGIES
array_check ALGORITHMS
array_check PLATFORMS
variable_check NHOSTS
variable_check LOAD
variable_check DEADLINE

# default values
: ${RESULTS:=$PWD/results}
: ${LOBA:=$PWD/loba}

COMMON_OPTS=(
    --cfg=contexts/factory:raw
)

log "Results from : \"$RESULTS\"."

[ $debug = 1 ] && log "Running in dry-run mode"

outfile() {
    echo "$*" | sed 's,[^ ]*/,,g;s/\.xml//;y/ /_/'
}
params=([0]="Load" [1]="Computation" [2]="Data send amount" [3]="Data send count" )
params_pos=(9 5 7 7)
firsttexline="|l|"
for algo in "${ALGORITHMS[@]}"; do
	for bk in "" "-b"; do
		firsttexline=$(echo "$firsttexline r|")
	done
done
firsttexline=$(echo -e "\\\begin{tabular}{$firsttexline}\n\\hline\c")

lasttexline="\\end{tabular}"

secondtexline="Param/Alg"
for algo in "${ALGORITHMS[@]}"; do
	for bk in "" "-b"; do
            secondtexline=$(echo -e "$secondtexline&\t${algo}${bk}")
	done
done
secondtexline=$(echo -e "$secondtexline\\t \\\\\\\\  \\hline \\hline\c")

mediantexlineend=$(echo -e "\\\\\\\\ \\hline\c")
# mediantexlineend=$(echo -e "$mediantexlineend\c")


for plat in "${PLATFORMS[@]}"; do
    tmp=$(basename "$plat" ".xml")
    plat_output="$RESULTS/plat_$tmp"
	for topo in "${TOPOLOGIES[@]}"; do
		topo_output="$plat_output/topo_$topo"
		topo_data_all_output="$plat_output/data_$topo-all.tex"
		echo "$firsttexline" > "$topo_data_all_output"
		echo "$secondtexline" >> "$topo_data_all_output"
     		for target_i in "0" "1" "2" "3"; do
			target_param=${params[$target_i]};
			target_position=${params_pos[$target_i]};
			outfilesuffix=$(outfile  "${target_param[@]}")
			mediantexline="$target_param\t"
			for algo in "${ALGORITHMS[@]}"; do
				topo_data_out="$plat_output/data_$topo-$outfilesuffix.tex"
				algo_output="$topo_output/algo_$algo"
			        args=(
					 -T"$topo"
					-a"$algo"
                			-N"$NHOSTS"
                			-L"$LOAD"
                			-t"$DEADLINE"
                			"$plat"
            			)
				for bk in "" "-b"; do
                			tmp=$(outfile "loba" $bk "${args[@]}")
                			infile="$algo_output/$tmp.out"
					variable=$(tail -17 "$infile" | awk '/'"$target_param"'/{print $'"$target_position"'}')
					log "Reading $target_param from $infile : $variable"
					mediantexline=$(echo -e "$mediantexline&$variable\t")
					echo  -e "$algo$bk\t$variable\n" >> "$topo_data_out"
            			done
			done
     			echo  "$mediantexline" >> "$topo_data_all_output"
     			echo  "$mediantexlineend" >> "$topo_data_all_output"
     		done
	echo "$lasttexline" >> "$topo_data_all_output"
    	done
done