#!/bin/bash

start=6
stop=12

if [ "$1" == "" ]; then
    echo Usage: $0 "["conv\|cconv\|biconv\|conv2\|cconv2\|biconv2\|cconv3"]" "["start"]" "["stop"]"
    exit
fi

N0=10000000
if [ "$HOSTTYPE" == "x86_64-linux" ]; then
    N0=100000000
fi

prune=no
divisor=0;
N=$N0

if [ "$1" == "conv" ]; then
    shift=1;
    offset=2;
    divisor=3;
    outdir=timings1r
    N=$N0
fi
if [ "$1" == "conv2" ]; then
    shift=1;
    offset=2;
    divisor=3;
    outdir=timings2r
    prune=yes
fi
if [ "$1" == "cconv" ]; then
    outdir=timings1c
    N=$N0
fi
if [ "$1" == "cconv2" ]; then
    outdir=timings2c
    prune=yes
fi
if [ "$1" == "biconv" ]; then
    shift=2;
    offset=3;
    divisor=4;
    outdir=timings1b
    N=$N0
fi
if [ "$1" == "biconv2" ]; then
    shift=2;
    offset=3;
    divisor=4;
    outdir=timings2b
    prune=yes
fi
if [ "$1" == "cconv3" ]; then
    outdir=timings3c
    prune=yes
fi

if [ "$outdir" == "" ]; then
    echo "wrong program name!"
    exit
fi
mkdir -p $outdir
cd $outdir
rm -f explicit implicit pruned
cd ..

if [ "$2" != "" ]; then
    start=$2
fi

if [ "$3" != "" ]; then
    stop=$3
fi

echo Timing:
for (( i=$start; i<=$stop; i++ ))
do
    echo $i
    m=$(asy -c "2^$i")
    echo -e "$m \t $(./$1 -m $m -i -n $N| grep -A 1 Implicit | tail -n 1)"| cat >> $outdir/implicit
    if [ "$divisor" != "0" ]; then
	m=$(asy -c "quotient(2^($i+$shift)+$offset,$divisor)")
    fi
    echo -e "$m \t $(./$1 -m $m -e -n $N| grep -A 1 Explicit | tail -n 1)" | cat >> $outdir/explicit
    if [ "$prune" == "yes" ]; then
	echo -e "$m \t $(./$1 -m $m -p -n $N| grep -A 1 Pruned | tail -n 1)" | cat >> $outdir/pruned
    fi

done
