#! /bin/sh
#
# run-lisp-tests
#
# Copyright (c) 2009-2011 Free Software Foundation, Inc.
#
# This file is part of GNU Zile.
#
# GNU Zile is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# GNU Zile is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU Zile; see the file COPYING.  If not, write to the
# Free Software Foundation, Fifth Floor, 51 Franklin Street, Boston,
# MA 02111-1301, USA.

# FIXME: Rewrite in Perl to avoid problems with spaces in paths

# N.B. Tests that use execute-kbd-macro must note that keyboard input
# is only evaluated once the script has finished running.

# srcdir and builddir are defined in the environment for a build
if test -z "$srcdir"; then
    srcdir=.
fi
if test -z "$builddir"; then
    builddir=.
fi

zile_pass=0
zile_fail=0
emacs_pass=0
emacs_fail=0

# If TERM is not set to a terminal type, choose a default
if test -z "$TERM" || test "$TERM" = "unknown"; then
    TERM=vt100
    export TERM
fi

for test in "$@"; do
    test=`echo $test | sed -e 's/\.el$//'`
    name=`basename $test`

    edit_file=$builddir/$name.input
    args="--no-init-file $edit_file --load $test.el"

    if test -n "$EMACS"; then
        cp $srcdir/lisp-tests/test.input $edit_file
        chmod +w $edit_file
        if $EMACS --quick --batch $args; then
            if diff $test.output $edit_file; then
                emacs_pass=`expr $emacs_pass + 1`
                rm -f $edit_file $edit_file~
            else
                echo "Emacs $name failed to produce correct output"
                emacs_fail=`expr $emacs_fail + 1`
            fi
        else
            echo "Emacs $name failed to run with error code $?"
            emacs_fail=`expr $emacs_fail + 1`
        fi
    fi

    cp $srcdir/lisp-tests/test.input $edit_file
    chmod +w $edit_file
    if $VALGRIND $builddir/zile $args; then
        if diff $test.output $edit_file; then
            zile_pass=`expr $zile_pass + 1`
            rm -f $edit_file $edit_file~
        else
            echo "Zile $name failed to produce correct output"
            zile_fail=`expr $zile_fail + 1`
        fi
    else
        echo "Zile $name failed to run with error code $?"
        zile_fail=`expr $zile_fail + 1`
    fi
done

echo "Zile: $zile_pass pass(es) and $zile_fail failure(s)"
echo "Emacs: $emacs_pass pass(es) and $emacs_fail failure(s)"

exit `expr $zile_fail + $emacs_fail`
