#! /bin/bash
# Shell script to run test suite.

set -e

if [ x"$1" = x"-v" ]; then VERBOSE=1; shift; fi
if [ x"$1" = x"-v" -o x"$1" = x"-vv" ]; then VERBOSE=1; EXTRA_ARGS=-x; shift; fi

# Run by "make check" from build directory
if [ -n "$srcdir" ]; then cd "$srcdir"; fi

# Creates a temporary file and exports the name of the file to
# the provided argument.  Exits on error.
# 
# Usage: create_tempfile TEMPFILE
#
create_tempfile()
{
    if test $# = 0
    then
       echo "No argument passed to create_tempfile()"
       exit 1
    fi

    if [ -x /bin/tempfile ]
    then
        # Debian 
        export $1="`tempfile`"
    elif [ -x  /bin/mktemp ]
    then
        # RedHat et. al.
        export $1="`mktemp /tmp/modtest.XXXXXX`"
    else
        echo "Don't know how to make a temporary file on this "
        echo "system, sorry."
        exit 1
    fi

    if [ $? -ne 0 ]
    then
        echo "Can't create temporary file."
        exit 1
    fi
}
export -f create_tempfile

if [ ! -e "tests/build" ]; then
    echo Making build directory for tests
    mkdir tests/build
fi
if [ ! -e "tests/tmp" ]; then
    echo Making temporary directory for tests
    mkdir tests/tmp
fi

for config in --enable-zlib --disable-zlib; do
    echo Building with $config...

    cd tests/build
    ../../configure $config CFLAGS="-DJUST_TESTING -g -Wall" >/dev/null
    make clean >/dev/null
    # ismod.static doesn't build with -DJUST_TESTING and --enable-zlib
    make insmod.static >/dev/null 2>&1 || touch insmod.static
    make all >/dev/null
    cd ../..

    echo Testing with $config...
    if grep -q CONFIG_USE_ZLIB=1 tests/build/Makefile; then
	CONFIG_HAVE_ZLIB=1
	export CONFIG_HAVE_ZLIB
    else
	unset CONFIG_HAVE_ZLIB
    fi

    # Create endianness links
    case `file tests/build/modprobe` in
	*MSB*) ENDIAN=be;;
	*LSB*) ENDIAN=le;;
	*) echo Unknown endian! >&2; exit 1;;
    esac
    ln -sfn 64-$ENDIAN tests/data/64
    ln -sfn 32-$ENDIAN tests/data/32

    PATH=`pwd`/tests/build:$PATH

    # By default, we want to look like a new kernel.
    MODTEST_UNAME=2.6.27
    export MODTEST_UNAME

    MODTEST_OVERRIDE_ROOT=tests/tmp
    export MODTEST_OVERRIDE_ROOT

    if [ $# -eq 1 ]; then DOING=0; else DOING=1; fi

    for dir in `find tests/* -type d | sort`
      do
    # data, build and tmp dirs don't contain tests.
      case "$dir" in
	tests/data*) continue;;
	tests/build*) continue;;
	tests/tmp*) continue;;
      esac

      if [ -z "$VERBOSE" ]; then
	  echo -n Running tests for $dir.
      else
	  echo Running tests for $dir.
      fi
      shopt -s nullglob
      for f in $dir/[0-9]*; do
	# Ignore backups dir.
	  case "$f" in *~) continue;; esac

	  if [ $DOING -eq 0 ]; then
	      case "$f" in *$1*) DOING=1;; *) continue;; esac
	  fi

	  rm -rf tests/tmp/*
	  if sh -e $EXTRA_ARGS $f; then
	      if [ -z "$VERBOSE" ]; then
		  echo -n .
	      else
		  echo Tests $f succeeded.
	      fi
	  else
	      echo Test for $f failed.
	      exit 1
	  fi
      done
      if [ -z "$VERBOSE" ]; then echo; fi
    done
done

exit 0
