#!/bin/sh

INSTDIR="$(readlink -f $(dirname $0)/..)"

ERROR=""

echo "XMLFLAT_HIERACHICAL_MODELLING"
cd $INSTDIR/examples/xmlflat/hierachical_modelling
$INSTDIR/bin/mbsimflatxml MBS.mbsimprj.flat.xml || ERROR="$ERROR XMLFLAT_HIERACHICAL_MODELLING"
echo "DONE"

echo "XML_HIERACHICAL_MODELLING"
cd $INSTDIR/examples/xml/hierachical_modelling
$INSTDIR/bin/mbsimxml MBS.mbsimprj.xml || ERROR="$ERROR XML_HIERACHICAL_MODELLING"
echo "DONE"

echo "XML_TIME_DEPENDENT_KINEMATICS"
cd $INSTDIR/examples/xml/time_dependent_kinematics
$INSTDIR/bin/mbsimxml MBS.mbsimprj.xml || ERROR="$ERROR XML_TIME_DEPENDENT_KINEMATICS"
echo "DONE"

echo "XML_HYDRAULICS_BALLCHECKVALVE"
cd $INSTDIR/examples/xml/hydraulics_ballcheckvalve
$INSTDIR/bin/mbsimxml MBS.mbsimprj.xml || ERROR="$ERROR XML_HYDRAULICS_BALLCHECKVALVE"
echo "DONE"


export OPENMBVCPPINTERFACE_PREFIX="$INSTDIR"

echo "OPENMBVCPPINTERFACE_SWIG_OCTAVE"
cd $INSTDIR/share/openmbvcppinterface/examples/swig
$INSTDIR/bin/octave octavetest.m || ERROR="$ERROR OPENMBVCPPINTERFACE_SWIG_OCTAVE"
echo "DONE"

if [ "_$MBSIMENV_TEST_PYTHON" == "_1" ]; then
  echo "OPENMBVCPPINTERFACE_SWIG_PYTHON"
  cd $INSTDIR/share/openmbvcppinterface/examples/swig
  python pythontest.py || ERROR="$ERROR OPENMBVCPPINTERFACE_SWIG_PYTHON"
  echo "DONE"
fi

if [ "_$MBSIMENV_TEST_JAVA" == "_1" ]; then
  echo "OPENMBVCPPINTERFACE_SWIG_JAVA"
  cd $INSTDIR/share/openmbvcppinterface/examples/swig
  java -classpath .:$INSTDIR/bin/openmbv.jar javatest || ERROR="$ERROR OPENMBVCPPINTERFACE_SWIG_JAVA"
  echo "DONE"
fi


echo "H5PLOTSERIE"
cd $INSTDIR/examples/xml/hierachical_modelling
$INSTDIR/bin/h5plotserie TS.mbsim.h5 || ERROR="$ERROR H5PLOTSERIE"
echo "DONE"

echo "OPENMBV"
cd $INSTDIR/examples/xml/hierachical_modelling
$INSTDIR/bin/openmbv TS.ombv.xml || ERROR="$ERROR OPENMBV"
echo "DONE"

echo "MBSIMGUI"
$INSTDIR/bin/mbsimgui || ERROR="$ERROR MBSIMGUI"
echo "DONE"

if [ -z $ERROR ]; then
  echo "ALL TESTS PASSED"
else
  echo "THE FOLLOWING TESTS FAILED:"
  echo $ERROR
fi