#######################################################################
# Makefile for r test-suite
#######################################################################

LANGUAGE     = r
SCRIPTSUFFIX = _runme.R
WRAPSUFFIX   = .R
RUNR         = R CMD BATCH --no-save --no-restore
srcdir       = @srcdir@
top_srcdir   = @top_srcdir@
top_builddir = @top_builddir@

C_TEST_CASES = copy_struct simple_array legacy
CPP_TEST_CASES = double_delete

include $(srcdir)/../common.mk

# Overridden variables here

# Rules for the different types of tests
%.cpptest: 
	$(setup)
	+$(swig_and_compile_cpp)
	$(run_testcase)

%.ctest:
	$(setup)
	+$(swig_and_compile_c)
	$(run_testcase)

%.multicpptest: 
	$(setup)
	+$(swig_and_compile_multi_cpp)
	$(run_multitestcase)

# Runs the testcase.
# 
# Run the runme if it exists.  If not just load the R wrapper to 
# check for syntactic correctness
run_testcase = \
	if [ -f $(srcdir)/$(SCRIPTPREFIX)$*$(SCRIPTSUFFIX) ]; then ( \
	  env LD_LIBRARY_PATH=.:$$LD_LIBRARY_PATH PATH=.:"$$PATH" \
               $(RUNTOOL) $(RUNR) $(srcdir)/$(SCRIPTPREFIX)$*$(SCRIPTSUFFIX) ;) \
	else \
	  ($(RUNTOOL) $(RUNR) $(srcdir)/$(SCRIPTPREFIX)$*$(WRAPSUFFIX);) \
	fi;

run_multitestcase = \
	for f in `cat $(top_srcdir)/$(EXAMPLES)/$(TEST_SUITE)/$*.list` ; do \
	if [ -f $(srcdir)/$(SCRIPTPREFIX)$${f}$(SCRIPTSUFFIX) ]; then ( \
	  env LD_LIBRARY_PATH=.:$$LD_LIBRARY_PATH PATH=.:"$$PATH" \
               $(RUNTOOL) $(RUNR) $(srcdir)/$(SCRIPTPREFIX)$${f}$(SCRIPTSUFFIX) ;) \
	else \
	  ($(RUNTOOL) $(RUNR) $(srcdir)/$(SCRIPTPREFIX)$${f}$(WRAPSUFFIX);) \
	fi; \
	done
# Clean
clean:
	$(MAKE) -f $(top_builddir)/$(EXAMPLES)/Makefile r_clean

%.clean:
	@rm -f $*.R $*_wrap.so $*_wrap.cpp $*_wrap.c $*_wrap.o $*_runme.Rout $*.Rout
	@if [ -f  $(top_srcdir)/$(EXAMPLES)/$(TEST_SUITE)/$*.list ]; then ( \
	for f in `cat $(top_srcdir)/$(EXAMPLES)/$(TEST_SUITE)/$*.list` ; do \
	   rm -f $${f}.R $${f}.Rout ; \
	done ) \
	fi


