#!/bin/bash

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

echo "BYTE-COMPILE PYTHON CODE"
$INSTDIR/bin/python -m compileall $INSTDIR

ERROR=""

echo "fmi/hierachical_modelling_py"
cd $INSTDIR/examples/fmi/hierachical_modelling_py
$INSTDIR/bin/mbsimCreateFMU --nocompress FMI.mbsx || ERROR="$ERROR fmucre_fmi/hierachical_modelling_py"
$INSTDIR/bin/fmuCheck.linux64 -f -l 5 mbsim.fmu || ERROR="$ERROR fmuchk_fmi/hierachical_modelling_py"
echo "DONE"

echo "fmi/hierachical_modelling"
cd $INSTDIR/examples/fmi/hierachical_modelling
$INSTDIR/bin/mbsimCreateFMU --nocompress FMI.mbsx || ERROR="$ERROR fmucre_fmi/hierachical_modelling"
$INSTDIR/bin/fmuCheck.linux64 -f -l 5 mbsim.fmu || ERROR="$ERROR fmuchk_fmi/hierachical_modelling"
echo "DONE"

echo "fmi/simple_test"
cd $INSTDIR/examples/fmi/simple_test
$INSTDIR/bin/mbsimCreateFMU --nocompress FMI.mbsx || ERROR="$ERROR fmucre_fmi/simple_test"
$INSTDIR/bin/fmuCheck.linux64 -f -l 5 mbsim.fmu || ERROR="$ERROR fmuchk_fmi/simple_test"
echo "DONE"

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

echo "xmlflat/bouncing_ball_boostodeint"
cd $INSTDIR/examples/xmlflat/bouncing_ball_boostodeint
$INSTDIR/bin/mbsimflatxml MBS.flat.mbsx || ERROR="$ERROR xmlflat/bouncing_ball_boostodeint"
echo "DONE"

echo "xml/hierachical_modelling_py"
cd $INSTDIR/examples/xml/hierachical_modelling_py
$INSTDIR/bin/mbsimxml MBS.mbsx || ERROR="$ERROR xml/hierachical_modelling_py"
echo "DONE"

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

echo "xml/python_mbsimmodule"
cd $INSTDIR/examples/xml/python_mbsimmodule
$INSTDIR/bin/mbsimxml MBS.mbsx || ERROR="$ERROR xml/python_mbsimmodule"
echo "DONE"

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

echo "xml/time_dependent_kinematics_py"
cd $INSTDIR/examples/xml/time_dependent_kinematics_py
$INSTDIR/bin/mbsimxml MBS.mbsx || ERROR="$ERROR xml/time_dependent_kinematics_py"
echo "DONE"

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

echo "fmi_cosim/hierachical_modelling"
cd $INSTDIR/examples/fmi_cosim/hierachical_modelling
$INSTDIR/bin/mbsimCreateFMU --nocompress FMI_cosim.mbsx || ERROR="$ERROR fmucre_fmi_cosim/hierachical_modelling"
$INSTDIR/bin/fmuCheck.linux64 -f -l 5 mbsim.fmu || ERROR="$ERROR fmuchk_fmi_cosim/hierachical_modelling"
echo "DONE"


export OPENMBVCPPINTERFACE_PREFIX="$INSTDIR"

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

echo "OPENMBVCPPINTERFACE_SWIG_PYTHON"
cd $INSTDIR/share/openmbvcppinterface/examples/swig
$INSTDIR/bin/python pythontest.py || ERROR="$ERROR OPENMBVCPPINTERFACE_SWIG_PYTHON"
echo "DONE"

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 MBS.mbsim.mbsh5 || ERROR="$ERROR H5PLOTSERIE"
echo "DONE"

echo "OPENMBV"
cd $INSTDIR/examples/xml/hierachical_modelling
$INSTDIR/bin/openmbv MBS.mbsim.ombvx || ERROR="$ERROR OPENMBV"
echo "DONE"

echo "MBSIMGUI"
cd $INSTDIR/examples/xml/hierachical_modelling
$INSTDIR/bin/mbsimgui MBS.mbsx || ERROR="$ERROR MBSIMGUI"
echo "DONE"

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