#! /bin/bash

rm -f tmp*

if test -z "$1"; then
 echo "Usage: _ccc_run <dataset number> <number of runs (optional)>"
 exit
fi

#Searching for dataset info (command line and loop wrapper)
found="0"
exec 5< _ccc_info_datasets
 read <&5 ds_max ;
 while read <&5 line; 
 do 
  if [ "$line" == "=====" ]; then
   read <&5 ds ;
   read <&5 cmd ;
   read <&5 loop_wrap ;
   if [ $ds -eq $1 ] ; then
    found="1"
    break;
   fi
  fi
 done
exec 5>&-

if [ $found != "1" ] ; then
 echo "Can't find dataset info in the _ccc_info_datasets file ..."
 exit
fi

if test -n "$2"; then
 loop_wrap=$2
fi

echo "Found dataset: " $ds
echo "Command line:  " $cmd
echo "Loop wrap:     " $loop_wrap

echo $loop_wrap > _finfo_dataset

if test -n "$CCC_RE"; then
 echo ./a.out $cmd > tmp-ccc-run-cmd
 $CCC_TASKSET $CCC_TIMED_RUN ccc-time -fe $CCC_RE -fp tmp-ccc-run-cmd -ft tmp-ccc-time
else
 echo $cmd > tmp-ccc-run-cmd
 $CCC_TASKSET $CCC_TIMED_RUN ccc-time -fe ./a.out -fp tmp-ccc-run-cmd -ft tmp-ccc-time
fi
