#!/bin/bash 

# Copyright (c) 2004-2007, The PalCom Project consortium.
# See http://www.ist-palcom.org/consortium.
# 
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
# 
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
# 
#     * Redistributions in binary form must reproduce the above
#       copyright notice, this list of conditions and the following
#       disclaimer in the documentation and/or other materials provided
#       with the distribution.
# 
#     * Neither the name of the PalCom Project nor the names of its
#       contributors may be used to endorse or promote products derived
#       from this software without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


# Used in BASH to test most of the pal-vm based software using developer tools

#set -x 

function usage ()
{
    echo "Usage: pal-dev-test [options]"
    echo "  options:"
    echo "       -h  display this help"
    echo "       -c  run with checked build of pal-vm"
    echo "       -d  run with debug build of pal-vm"
    echo "       -e  test exported tools/libs instead of developer version"
    echo "       -b  do not rebuild tools and libraries before test (implied by -e)"
    echo "       -f  forced: do not ask questions"
    echo "       -m  skip tests dependent on multicast"
    echo "       -x  execute benchmarks after regular tests"
}

# Check arguments
for a 
do
  if [ "$a" == "-h" ]; then 
      usage
      exit
  fi
  if [ "$a" == "-x" ]; then 
      BENCHMARKS=1
      continue
  fi
  if [ "$a" == "-d" ]; then 
      VMARGS="${VMARGS} -d"
      continue
  fi
  if [ "$a" == "-c" ]; then 
      VMARGS="${VMARGS} -c"
      continue
  fi
  if [ "$a" == "-b" ]; then 
      NOBUILD=1
      continue
  fi
  if [ "$a" == "-f" ]; then 
      FORCED=1
      continue
  fi
  if [ "$a" == "-e" ]; then 
      TEST_EXPORTED=1
      NOBUILD=1
      continue
  fi
  if [ "$a" == "-m" ]; then 
      SKIP_MULTICAST_DEPENDENT=1
      continue
  fi
  echo Illegal option: $a
  usage
  exit 1
done

if [ "$TEST_EXPORTED" == "1" ]; then
    echo "Testing exported tools and libraries (-e option)"
    DEV=
else
    echo Testing developer tools and libraries
    echo "(use -e option to test exported tools and libraries)"
    DEV=-dev
fi

PALVM="pal${DEV}-vm $VMARGS"
PALST="pal${DEV}-st"

ROOT=`pal-dev-root`

SRCDIR="${ROOT}developer/runtime-environment/engine/pal-vm"
OUTDIR="${ROOT}developer/runtime-environment/engine/pal-vm/test"
TUTDIR="${ROOT}doc/tutorials"

ERRORS=0

function compare ()
{
    #echo compare...
    file="$1"
    rm -f $file.diff
    if [ ! -f "${OUTDIR}/reference/$file.ref" ]; then
	echo "${OUTDIR}/reference/$file.ref" does not exist.
	echo using generated $file.out as future reference.
	cp $file.out "${OUTDIR}/reference/$file.ref"
	pushd "${OUTDIR}/reference"
	svn add $file.out
	popd
	return
    fi
    if [ "$TEST_EXPORTED" == "1" ]; then
	cat "${OUTDIR}/reference/$file.ref" | sed -e 's/pal-dev-vm/pal-vm/g' > $file.ref
    else
	cp "${OUTDIR}/reference/$file.ref" "$file.ref"
    fi
    if cmp -s $file.out $file.ref; then
	echo "   OK"
	rm -f $file.out $file.ref build-$file.out
    else
	echo "*** ERROR: ***"
	DIFFFLAGS="-u"
	diff $DIFFFLAGS $file.ref $file.out > $file.diff
	cat $file.diff
	ERRORS=`expr $ERRORS + 1`
    fi
}

function test_tst ()
{
    echo "-- building tst with ${PALST} --"
    if [ "$NOBUILD" == "1" ]; then
	BUILD_TST_OPTS="-b"
    fi
    if [ "$TEST_EXPORTED" == "1" ]; then 
	BUILD_TST_OPTS="$BUILD_TST_OPTS -e"
    fi
    pal-dev-build-tst $BUILD_TST_OPTS > build-tst.out
    echo "-- running tst with ${PALVM} --"
    ${PALVM} -cp . tst.prc > tst.out
    compare tst
}

function test_jtst ()
{
    echo "-- building jtst with pal${DEV}-j --"
    pushd "$SRCDIR/../jtst" >/dev/null 2>&1
    if [ "$TEST_EXPORTED" == "1" ]; then
	echo Testing jtst with exported tools NYI
    else
	sh ./build.sh > build-jtst.out
	echo "-- running jtst with ${PALVM} --"
	${PALVM} -cp . ist.palcom.jtst.prc > jtst.out
	compare jtst
    fi 
    popd >/dev/null 2>&1
}

function test_core ()
{
    name=$1
    echo "-- running ${name}.test with ${PALVM} --"
    ${PALVM} -cp . ist.palcom.${name}.test.prc > ${name}.test.out
    compare ${name}.test
}

function test_tutorial ()
{
    dir="${TUTDIR}/$1" 
    name="$2"
    echo "-- building in $dir"
    cd "$dir"
    make clean > /dev/null
    make dev=${DEV} build > build-$name.out
    echo "-- running in $dir"
    make dev=${DEV} run > $name.out
    compare $name
}

cd "${OUTDIR}"

if [ "$NOBUILD" == "1" ]; then
    echo "-- not building tools/libs (due to -b or -e option) --"
else
    if [ "$FORCED" == "1" ]; then
	$answer="y"
    else
	echo -n '-- build basic tools and libraries [use -b option to avoid this question] (n/y) '
	read answer
    fi
    if [ "$answer" = "y" ]; then
	pal-dev-build # > build.out
    fi 
fi

######## MAIN ########

test_tst
test_jtst
if [ "$TEST_EXPORTED" == "1" ]; then 
    echo '-- building core test programes against exported lib --'
    pal-dev-build-core -e -t
    echo ""
fi
test_core palcomthreads
test_core hgraph
test_core storage
if [ "$SKIP_MULTICAST_DEPENDENT" != "1" ]; then
    test_core connections
    test_core messaging
    test_core discovery
    test_core mal.udp
    test_core pubsub
fi

test_tutorial "pal-j/pal-j-example/src" tutorials-pal-j-example
test_tutorial "pal-st/src/Hello" tutorials-st-hello
test_tutorial "pal-st/src/HelloClasses" tutorials-st-helloclasses
test_tutorial "interop/threads/simple/src" tutorials-threads-simple
test_tutorial "interop/basic/java4smalltalk/src" tutorials-basic-java4smalltalk
test_tutorial "interop/basic/smalltalk4java/src" tutorials-basic-smalltalk4java
test_tutorial "interop/basic/inheritance/src" tutorials-basic-inheritance
test_tutorial "interop/basic/system/src" tutorials-basic-system

cd ${OUTDIR}

if [ "$BENCHMARKS" = "1" ]; then
    echo "-- running benchmarks with ${PALVM} --"
    ${PALVM} -cp . benchmarks.prc
else
    echo '-- NOT running benchmarks (use -x option to run) --'
fi

#FIXME: potentially some clean up to do here

if [ $ERRORS -gt 0 ]; then
    echo ""
    echo '***' $ERRORS 'problem(s) detected.'
    echo ''
    echo For each test, the differences listed above can also be
    echo found in '<test>.diff', where '<test>' is a name identifying
    echo the test suite '(usually simply a file name)'.
    echo The corresponding '<test>'.out is the actual output, whereas
    echo the '<test>'.ref is the expected output.
    echo The '<test>'.ref is a copy of the corresponding file in 
    echo "  $OUTDIR/reference/"
    echo If the expected output needs to be changed, please edit the
    echo file in that directory.
    echo The temporary files 
    echo -n "  "
    echo '<test>'.{diff,out,ref} 
    echo will be automatically removed upon a succesful test.
fi
