#!/bin/bash
# run_all_tests - Runs a longer test suite using different device drivers etc.
#
# Copyright (c) 2012 Pekka Jääskeläinen / Tampere Univ. of Technology
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

failures=no

function run {
    log=`mktemp`
    if $* > $log 2>&1;
        then
        echo "OK"
        rm -f $log
        else
        echo "FAIL! See $log."
        failures=yes
        fi
}

function run_silently {
   log=`mktemp`
   if $* > $log 2>&1;
        then
        rm -f $log
        else
        echo "FAIL! See $log."
        failures=yes
        fi
}

echo -n "Running 'make check' without ocl-icd and the default device..."
run_silently ./configure --enable-testsuites=all --disable-icd $*
run_silently make clean
run_silently make 
run make check

echo -n "Running 'make check' with ocl-icd and the default device..."
run_silently ./configure --enable-testsuites=all $*
run_silently make clean
run_silently make 
run make check

# The Basic device has broken tests at the moment. Enable after fixed.
#echo -n "Running 'make check' with the 'basic' device (without ICD)..."
#export POCL_DEVICES="basic"
#run make check

#echo -n "Running 'make check' with multiple devices (without ICD)..."
#export POCL_DEVICES="pthread basic pthread pthread"
#run make check

if test "$failures" == "yes";
then
    exit 1
else
    exit 0
fi