#! /usr/bin/env zsh

export LC_ALL=C ## for sorting
DEMOBIN=./bin

if [ -z "$PAT" ]; then  PAT='.';  fi

FXT_EXTRA_FLAGS=-Wno-conversion

## system name:
uname -a
echo
## gcc version
gcc --version | head -1
echo
## CPU info:
cat /proc/cpuinfo
echo -e '\n\n\n'

## compile bench
make clobber  &>/dev/null
make cat  &>/dev/null
echo "Compiling the FXT library:"
time make -s lib FXT_EXTRA_FLAGS=${FXT_EXTRA_FLAGS}
echo -e '\n\n\n'

## run FXT benchmarks (whose filename contain the pattern PAT):
DD=$(grep -l 'BENCHARGS' demo/*/*-demo.cc demo/*/*-bench.cc | sort)
for d in ${=DD}; do  ## loop over files
#    echo " :: d=[$d]"
    if [ "${d}" \!= "${d/${PAT}/}" ]; then  ## pattern matches
        echo '------------------------------------------------------------';
        make -s 1demo DSRC=$d DEMOFLAGS='-DTIMING -Wno-unused' FXT_EXTRA_FLAGS=${FXT_EXTRA_FLAGS} RUNDEMO=true
        AA=$(grep 'BENCHARGS' $d | cut -d= -f2- | tr ' ' '@')
        for aa in ${=AA}; do  ## loop over benchmarks in one file
            a=$(echo $aa | tr '@' ' ' );
            echo "----- args=$a"
            eval time ${DEMOBIN} ${=a}
            echo -en '\n'
        done
        echo -en '\n\n'
    fi
done


exit 0;
#################################



## old loop, sorts also args, which is ugly:
#XX=$(grep BENCHARGS demo/*/*-demo.cc demo/*/*-bench.cc | tr ' ' '@' | sort)
#for x in ${=XX}; do
##    echo " :: x=[$x]"
#    d=$(echo $x | cut -d: -f1);
#    a=$(echo $x | cut -d= -f2 | tr '@' ' ');
#    echo "----- file=$d  args=$a"
#    if [ ${d} \!= ${d/${PAT}/} ]; then
##        echo "+++++++++++++++++++++";
#        make -s 1demo DSRC=$d DEMOFLAGS='-DTIMING -Wno-unused' RUNDEMO=true
#        eval time ${DEMOBIN} ${=a}
#        echo -e '\n\n\n'
#    fi
#done
