#!/bin/sh

precmd=
perjobs=1
verbose=
prefn=
while getopts "p:j:vf:" OPTION; do
	case "$OPTION" in
		p)
			precmd=$OPTARG
			;;
		j)
			perjobs=$OPTARG
			;;
		f)
			prefn=$OPTARG
			;;
		v)
			verbose="-v"
			;;
	esac
done


FNPREFIX="${prefn}`date +%Y%m%d-%H%M%S`-$(hostname)"
FNJOBS="${FNPREFIX}-jobs.sh"
FNDB="${FNPREFIX}.sqlite"
FNINFO="${FNPREFIX}-info.txt"

echo "git HEAD: `git show HEAD | head -n1`" | tee -a $FNINFO
echo "Started: `date` @ $(hostname)" | tee -a $FNINFO
rm -f $FNJOBS

[ -z "$precmd" ] || precmd="$precmd ;"

for filter in `seq 0 1`; do
for balanced in `seq 0 1`; do
for irrational in `seq 0 1`; do
for zerotest in `seq 0 1`; do
for leda in `seq 0 1`; do

	[ $leda = "1" ] && [ $filter = "0" ] && continue

	prog=
	opts=
	[ $filter = "1" ] && opts="$opts -f"
	[ $balanced = "1" ] && opts="$opts -b"
	[ $irrational = "1" ] && opts="$opts -i"
	[ $zerotest = "1" ] && opts="$opts -z"
	[ $leda = "1" ] && prog="./sum-leda" || prog="./sum-core"

	if [ $irrational = "0" ]; then
		for seed in `seq 1 10`; do
			for i in `seq 1 40`; do
				echo "$precmd ${prog} -r -s $i -e $seed $opts" >> $FNJOBS
			done
		done
	fi

	for i in `seq 20 20 200` `seq 400 200 10000` `seq 12000 2000 200000`; do
		echo "$precmd ${prog} -s $i $opts" >> $FNJOBS
	done
done
done
done
done
done


paralleljobs.py -c $FNJOBS -d $FNDB -p 'filter BOOL, seed INTEGER, size INTEGER, balanced BOOL, sqrt BOOL, irrational BOOL, zerotest BOOL, leda BOOL, expr_time REAL, expr_mem INTEGER, expr_stacksize INTEGER, test_time REAL, test_mem INTEGER, test_stacksize INTEGER, test_result BOOL'
PIDS=
for i in `seq $perjobs`; do
	echo "launch process $i"
	paralleljobs.py -d $FNDB -w $verbose &
	PIDS="$PIDS $!"
	sleep 0.5
done

wait $PIDS
echo "Stopped: `date`" | tee -a $FNINFO
