#!/bin/bash
date
START=`date +%s`
ARGS="$@"
PROFILING=""
RELEASE=""

if [ "$1" == "--release" ]; then
    RELEASE="True"
    shift
    ARGS="$@"
fi

if [ "$1" == "--buildbot" ]; then
    shift
    ARGS="$@"
    #we set the compiledir to the /Tmp dir to make the test faster by bypassing the nfs network.
    BASE_COMPILEDIR=/Tmp/lisa_theano_compile_dir_theano
    ROOT_CWD=/Tmp/nightly_build
    FLAGS=base_compiledir=$BASE_COMPILEDIR
    COMPILEDIR=`THEANO_FLAGS=$FLAGS python -c "from __future__ import print_function; import theano; print(theano.config.compiledir)"`
    cd ${ROOT_CWD}/Theano
    git rev-parse HEAD
    #Run tests from inside the Theano directory to prevent import problem.
#    PROFILING="--with-coverage --cover-package=theano"
    NOSETESTS=${ROOT_CWD}/Theano/bin/theano-nose
    export PYTHONPATH=${ROOT_CWD}:$PYTHONPATH
else
    COMPILEDIR=`python -c "from __future__ import print_function; import theano; print(theano.config.compiledir)"|tail -1`
    NOSETESTS=`python -c "from __future__ import print_function; import theano; print(theano.__path__[0])"|tail -1`/../bin/theano-nose
fi

echo "Number of elements in the compiledir:"
ls ${COMPILEDIR}|wc -l

# We don't want warnings in the buildbot for errors already fixed.
FLAGS=${THEANO_FLAGS},warn.argmax_pushdown_bug=False,warn.gpusum_01_011_0111_bug=False,warn.sum_sum_bug=False,warn.sum_div_dimshuffle_bug=False,warn.subtensor_merge_bug=False,$FLAGS

# We want to see correctly optimization/shape errors, so make make them raise an
# error.
FLAGS=on_opt_error=raise,$FLAGS
FLAGS=on_shape_error=raise,$FLAGS

# Ignore user device and floatX config, because:
#   1. Tests are intended to be run with device=cpu.
#   2. We explicitly add 'floatX=float32' in one run of the test suite below,
#      while we want all other runs to run with 'floatX=float64'.
FLAGS=${FLAGS},device=cpu,floatX=float64


if [ "$RELEASE" ]; then
    echo "Executing tests with default mode and compute_test_value"
    THEANO_FLAGS=${FLAGS},compute_test_value=ignore ${NOSETESTS} ${ARGS}
    echo "Number of elements in the compiledir:"
    ls ${COMPILEDIR}|wc -l
    echo

    echo "Executing tests with linker=vm,floatX=float32"
    echo "THEANO_FLAGS=${FLAGS},linker=vm,floatX=float32 ${NOSETESTS} ${ARGS}"
    THEANO_FLAGS=${FLAGS},linker=vm,floatX=float32 ${NOSETESTS} ${ARGS}
    echo "Number of elements in the compiledir:"
    ls ${COMPILEDIR}|wc -l
    echo

    echo "Executing tests with cxx="
    echo "THEANO_FLAGS=${FLAGS},cxx= ${NOSETESTS} ${ARGS}"
    THEANO_FLAGS=${FLAGS},cxx= ${NOSETESTS} ${ARGS}
    echo "Number of elements in the compiledir:"
    ls ${COMPILEDIR}|wc -l
    echo

fi

echo "Executing tests with mode=FAST_RUN"
echo "THEANO_FLAGS=cmodule.warn_no_version=True,${FLAGS},mode=FAST_RUN ${NOSETESTS} ${PROFILING} ${ARGS}"
THEANO_FLAGS=cmodule.warn_no_version=True,${FLAGS},mode=FAST_RUN ${NOSETESTS} ${PROFILING} ${ARGS}
echo "Number of elements in the compiledir:"
ls ${COMPILEDIR}|wc -l
echo

echo "Executing tests with mode=FAST_RUN,floatX=float32"
echo "THEANO_FLAGS=${FLAGS},mode=FAST_RUN,floatX=float32 ${NOSETESTS} ${ARGS}"
THEANO_FLAGS=${FLAGS},mode=FAST_RUN,floatX=float32 ${NOSETESTS} ${ARGS}
echo "Number of elements in the compiledir:"
ls ${COMPILEDIR}|wc -l
echo

echo "Executing tests with linker=vm,vm.lazy=True,floatX=float32"
echo "THEANO_FLAGS=${FLAGS},linker=vm,vm.lazy=True,floatX=float32 ${NOSETESTS} ${ARGS}"
THEANO_FLAGS=${FLAGS},linker=vm,vm.lazy=True,floatX=float32 ${NOSETESTS} ${ARGS}
echo "Number of elements in the compiledir:"
ls ${COMPILEDIR}|wc -l
echo

#we change the seed and record it everyday to test different combination. We record it to be able to reproduce bug caused by different seed. We don't want multiple test in DEBUG_MODE each day as this take too long.
seed=$RANDOM
echo "Executing tests with mode=DEBUG_MODE with seed of the day $seed"
echo "THEANO_FLAGS=${FLAGS},unittests.rseed=$seed,mode=DEBUG_MODE,DebugMode.check_strides=0,DebugMode.patience=3,DebugMode.check_preallocated_output= ${NOSETESTS} ${ARGS}"
THEANO_FLAGS=${FLAGS},unittests.rseed=$seed,mode=DEBUG_MODE,DebugMode.check_strides=0,DebugMode.patience=3,DebugMode.check_preallocated_output= ${NOSETESTS} ${ARGS}

echo "Number of elements in the compiledir:"
ls ${COMPILEDIR}|wc -l
echo

#We put this at the end as it have a tendency to loop infinitly.
#Until we fix the root of the problem we let the rest run, then we can kill this one in the morning.
# with --batch=1000" # The buildbot freeze sometimes when collecting the tests to run
echo "Executing tests with mode=FAST_COMPILE"
echo "THEANO_FLAGS=${FLAGS},mode=FAST_COMPILE ${NOSETESTS} ${ARGS}"
THEANO_FLAGS=${FLAGS},mode=FAST_COMPILE ${NOSETESTS} ${ARGS}

echo "Number of elements in the compiledir:"
ls ${COMPILEDIR}|wc -l
echo

echo
END=`date +%s`
python -c "from __future__ import print_function; print('Total test time: %dm %ds'%((${END} - ${START})/60, (${END} - ${START})%60))"
date
