#! /bin/sh

# This is a script for the CGAL test suite. Such a script must obey
# the following rules:
#
# - the name of the script is cgal_test_with_cmake
# - for every target two one line messages are written to the file 'error.txt'
#     the first one indicates if the compilation was successful
#     the second one indicates if the execution was successful
#   if one of the two was not successful, the line should start with 'ERROR:'
# - running the script should not require any user interaction
# - the script should clean up object files and executables

  ERRORFILE=error.txt
  DO_RUN=
  if [ -z "${MAKE_CMD}" ]; then
    MAKE_CMD=make
  fi
  NEED_CLEAN=

#---------------------------------------------------------------------#
#                    configure
#---------------------------------------------------------------------#

configure()
{
  echo "Configuring... "

  if eval 'cmake "$CMAKE_GENERATOR" -DRUNNING_CGAL_AUTO_TEST=TRUE  \
                                     -DCGAL_DIR="$CGAL_DIR" \
                                     .' ; then

    echo "   successful configuration" >> $ERRORFILE
  else
    echo "   ERROR:    configuration" >> $ERRORFILE
  fi
}

#---------------------------------------------------------------------#
#                    compile_and_run <target>
#---------------------------------------------------------------------#

compile_and_run()
{
  echo "Compiling $1 ... "
  SUCCESS="y"

  if eval '${MAKE_CMD} VERBOSE=ON -fMakefile $1' ; then
    echo "   successful compilation of $1" >> $ERRORFILE
  else
    echo "   ERROR:    compilation of $1" >> $ERRORFILE
    SUCCESS=""
  fi

  if [ -n "$DO_RUN" ] ; then
    if [ -n "${SUCCESS}" ] ; then
      OUTPUTFILE=ProgramOutput.$1.$PLATFORM
      rm -f $OUTPUTFILE
      COMMAND="./$1"
      if [ -f $1.cmd ] ; then
        COMMAND="$COMMAND `cat $1.cmd`"
      fi
      if [ -f $1.cin ] ; then
        COMMAND="cat $1.cin | $COMMAND"
      fi
      echo "Executing $1 ... "
      echo
      ulimit -t 3600 2> /dev/null
      if eval $COMMAND > $OUTPUTFILE 2>&1 ; then
        echo "   successful execution   of $1" >> $ERRORFILE
      else
        echo "   ERROR:    execution   of $1" >> $ERRORFILE
      fi
    else
      echo   "   ERROR:    not executed   $1" >> $ERRORFILE
    fi
  fi
}

#---------------------------------------------------------------------#
#                    remove the previous error file
#---------------------------------------------------------------------#

rm -f $ERRORFILE
touch $ERRORFILE

#---------------------------------------------------------------------#
#                    configure, compile and run the tests
#---------------------------------------------------------------------#

configure

if [ $# -ne 0 ] ; then
  for file in $* ; do
    compile_and_run $file
  done
else
  echo "Run all tests."
if  ${MAKE_CMD} -f Makefile help | grep "Mesh_3$" > /dev/null; then
  compile_and_run Mesh_3
  NEED_CLEAN=y
fi
if  ${MAKE_CMD} -f Makefile help | grep "mesh_3_demo_mesh_3_optimization_plugin" > /dev/null; then
  compile_and_run mesh_3_demo_mesh_3_optimization_plugin
  NEED_CLEAN=y
fi
if  ${MAKE_CMD} -f Makefile help | grep "mesh_3_demo_mesh_3_plugin" > /dev/null; then
  compile_and_run mesh_3_demo_mesh_3_plugin
  NEED_CLEAN=y
fi
if  ${MAKE_CMD} -f Makefile help | grep "mesh_3_demo_io_c3t3_plugin" > /dev/null; then
  compile_and_run mesh_3_demo_io_c3t3_plugin
  NEED_CLEAN=y
fi
if  ${MAKE_CMD} -f Makefile help | grep "mesh_3_demo_io_image_plugin" > /dev/null; then
  compile_and_run mesh_3_demo_io_image_plugin
  NEED_CLEAN=y
fi
if  ${MAKE_CMD} -f Makefile help | grep "mesh_3_demo_io_implicit_function_plugin" > /dev/null; then
  compile_and_run mesh_3_demo_io_implicit_function_plugin
  NEED_CLEAN=y
fi
if  ${MAKE_CMD} -f Makefile help | grep "mesh_3_demo_io_off_plugin" > /dev/null; then
  compile_and_run mesh_3_demo_io_off_plugin
  NEED_CLEAN=y
fi
if  ${MAKE_CMD} -f Makefile help | grep "klein_function_plugin" > /dev/null; then
  compile_and_run klein_function_plugin
  NEED_CLEAN=y
fi
if  ${MAKE_CMD} -f Makefile help | grep "sphere_function_plugin" > /dev/null; then
  compile_and_run sphere_function_plugin
  NEED_CLEAN=y
fi
if  ${MAKE_CMD} -f Makefile help | grep "tanglecube_function_plugin" > /dev/null; then
  compile_and_run tanglecube_function_plugin
  NEED_CLEAN=y
fi
if  ${MAKE_CMD} -f Makefile help | grep "mesh_3_demo_volume_planes_plugin" > /dev/null; then
  compile_and_run mesh_3_demo_volume_planes_plugin
  NEED_CLEAN=y
fi
fi


#
# The clean target generated by CMake under cygwin
# always fails for some reason
#
if [ -n "${NEED_CLEAN}" ]; then
  if ! ( uname | grep -q "CYGWIN" ) ; then
    ${MAKE_CMD} -fMakefile clean || true
  fi
fi
