#!/bin/bash


echo -e '\n\n*************\t Start evaluation.  \n'

run_example () {

    n=10
    if [[("$1" == "base")]];
    then
        start=$(($(date +%s%3N)))
        for ((i=0; i < n; i++));
        do
            scasp -n0 heart_failure_query.pl > output
        done
        end=$(($(date +%s%3N)))
    else
        if [[("$1" == "html")]];
        then
            start=$(($(date +%s%3N)))
            for ((i=0; i < n; i++));
            do
                scasp -n0 --tree $2 $3 --html heart_failure_query.pl > output
            done
            end=$(($(date +%s%3N)))
        else
            start=$(($(date +%s%3N)))
            for ((i=0; i < n; i++));
            do
                scasp -n0 --tree $2 $3 heart_failure_query.pl > output
            done
            end=$(($(date +%s%3N)))
        fi
    fi
    
    if [[("$1" == "base")]];
    then
        basetime=$(bc -l <<< "scale=0; ($end - $start) / $n")
        printf "\n\nBase time is $basetime for $n times\n" >> time
    else
        time=$(bc -l <<< "scale=0; ($end - $start) / $n")
        ratio=$(bc -l <<< "scale=3; ($time / $basetime)")
        printf '& %3.2f   ' $ratio >> time
    fi
}

#base case
run_example 'base'

echo ' ' >> time

echo '  \begin{tabular}{p{4cm}rrrr}' >> time
echo '    \toprule' >> time
echo '    & \multicolumn{2}{c}{} & \multicolumn{2}{c}{--html} \\' >> time
echo '                                & --plain  & --human  & --plain  & --human   \\' >> time
echo '    \midrule' >> time
echo -n '    \code|--tree --short           |   ' >> time

#
run_example 'std' '--short'                   
run_example 'std' '--short' '--human'
run_example 'html' '--short'                   
run_example 'html' '--short' '--human'          

echo ' \\' >> time
echo -n '    \code|--tree --short --neg     |   ' >> time

run_example 'std' '--short --neg'                   
run_example 'std' '--short --neg' '--human'
run_example 'html' '--short --neg'                   
run_example 'html' '--short --neg' '--human'          

echo ' \\' >> time
echo -n '    \code|--tree --mid             |   ' >> time

#
run_example 'std' '--mid'                   
run_example 'std' '--mid' '--human'
run_example 'html' '--mid'                   
run_example 'html' '--mid' '--human'          

echo ' \\' >> time
echo -n '    \code|--tree --mid --neg       |   ' >> time

run_example 'std' '--mid --neg'                   
run_example 'std' '--mid --neg' '--human'
run_example 'html' '--mid --neg'                   
run_example 'html' '--mid --neg' '--human'          

echo ' \\' >> time
echo -n '    \code|--tree --long            |   ' >> time

#
run_example 'std' '--long'                   
run_example 'std' '--long' '--human'
run_example 'html' '--long'                   
run_example 'html' '--long' '--human'          

echo ' \\' >> time
echo '    \bottomrule' >> time
echo '  \end{tabular}' >> time

echo -e '\n\n*************\t End.  \n'


