## Process this with automake to create Makefile.in

KAWALIB = ../kawa-$(VERSION).jar
DISABLE_GCJ_COMPILED_KAWA = KAWALIB=$(KAWALIB) ../bin/kawa.sh
ENABLE_GCJ_COMPILED_KAWA = ../bin/kawa-bin
KAWA = $(@GCJ_COMPILED_SELECTED@_KAWA)
KAWATEST = $(KAWA) -e '(load "testing.zip")' $(OPTIONS)

if ENABLE_SERVLET
SERVLET_TESTS = check-HelloCgiS check-HelloCgiK check-HelloCgiX
endif
if WITH_SAX2
SAX2_TESTS = check-MySAXApp
endif
all:

check: check2-no-full-tailcalls check1 check2-full-tailcalls check-no-inline \
  $(SERVLET_TESTS) $(SAX2_TESTS)

check2-no-full-tailcalls:
	$(MAKE) check2 OPTIONS=--no-full-tailcalls

check2-full-tailcalls:
	$(MAKE) check2 OPTIONS=--full-tailcalls

check-no-inline:
	$(MAKE) check2 OPTIONS=--no-inline

check1:  check-num check-arr check-format check-ppfile scribble.class
check2:  check-scm check-misc check-mac check-obj check-polytype

testing.zip: $(srcdir)/testing.scm
	$(KAWA) -e '(compile-file "'$(srcdir)/testing.scm'" "testing.zip")'

check-scm: testing.zip
	rm -rf tmp?
	$(KAWATEST) \
	  -e '(define this-file-name "$(srcdir)/test.scm")' \
	  -e "(set! symbol-read-case 'D)" \
	  -f $(srcdir)/test.scm -e '(test-report)'

check-num: testing.zip
	$(KAWATEST) -f "$(srcdir)/num-test.scm" -e '(test-report)'

check-arr: testing.zip
	$(KAWATEST) -f "$(srcdir)/arr-test.scm" -e '(test-report)'

check-misc: testing.zip
	$(KAWATEST) -f "$(srcdir)/misc-test.scm" -e '(test-report)'

check-mac: testing.zip
	$(KAWATEST) -e '(define src-prefix "$(srcdir)/")' \
	  -f "$(srcdir)/mac-test.scm" -e '(test-report)'

check-obj: testing.zip module2.class module3.class classes2.class \
  MyFunc.class MyModule.class
	$(KAWATEST) -f "$(srcdir)/obj-test.scm" -e '(test-report)'

check-polytype: testing.zip
	$(KAWATEST) -f "$(srcdir)/polytype.scm" -e '(test-report)'

check-format: testing.zip
	$(KAWATEST) \
	  -f "$(srcdir)/formatst.scm" -e '(test-report)'

check-ppfile:
	$(KAWA) -e "(require 'pprint-file)" \
	  -e '(define out (open-output-file "tmpP"))' \
	  -e '(pprint-file "@top_srcdir@/gnu/kawa/slib/ppfile.scm" out)' \
	  -e '(close-output-port out)'
	diff tmpP $(srcdir)/ppfile.out
	rm tmpP

scribble.class: $(srcdir)/scribble.scm
	$(KAWA) --applet -C $(srcdir)/scribble.scm

module1.class: $(srcdir)/module1.scm
	$(KAWA) -C $(srcdir)/module1.scm

module1a.class: $(srcdir)/module1a.scm
	$(KAWA) -C $(srcdir)/module1a.scm

module2.class: $(srcdir)/module2.scm module1.class module1a.class
	$(KAWA) -C $(srcdir)/module2.scm

module3.class: $(srcdir)/module3.scm module1.class MyTimestamp.class
	$(KAWA) -C $(srcdir)/module3.scm

classes1.class: $(srcdir)/classes1.scm
	$(KAWA) -C $(srcdir)/classes1.scm

SimpleB.class: $(srcdir)/SimpleB.java classes1.class
	CLASSPATH=..:. $(JAVAC) -g $(srcdir)/SimpleB.java -d .

classes2.class: $(srcdir)/classes2.scm SimpleB.class
	CLASSPATH=@conf_classpath@$$CLASSPATH \
	$(KAWA) -C $(srcdir)/classes2.scm

HelloCgiK.class: $(srcdir)/HelloCgiK.krl
	CLASSPATH=@conf_classpath@$$CLASSPATH \
	$(KAWA) --servlet --krl -C $(srcdir)/HelloCgiK.krl

HelloCgiK:
	$(LN_S) ../bin/cgi-servlet HelloCgiK

check-HelloCgiK: HelloCgiK.class HelloCgiK
	CLASSPATH=@conf_classpath@$$CLASSPATH \
	REQUEST_METHOD=GET SCRIPT_NAME=HelloCgiK SERVER_NAME=localhost \
	  KAWALIB=$(KAWALIB) ./HelloCgiK >tmp.out
	@if diff -b $(srcdir)/HelloCgiK.expected tmp.out; \
	  then echo '# HelloCgiK test passes'; \
	  else echo FAIL HelloCgiK test; fi

HelloCgiS.class: $(srcdir)/HelloCgiS.scm
	CLASSPATH=@conf_classpath@$$CLASSPATH \
	$(KAWA) --servlet --output-format xml -C $(srcdir)/HelloCgiS.scm

HelloCgiS:
	$(LN_S) ../bin/cgi-servlet HelloCgiS

check-HelloCgiS: HelloCgiS.class HelloCgiS
	CLASSPATH=@conf_classpath@$$CLASSPATH \
	REQUEST_METHOD=GET SCRIPT_NAME=HelloCgiS SERVER_NAME=localhost \
	  KAWALIB=$(KAWALIB) ./HelloCgiS >tmp.out
	@if diff -b $(srcdir)/HelloCgiS.expected tmp.out; \
	  then echo '# HelloCgiS test passes'; \
	  else echo FAIL HelloCgiS test; fi

HelloCgiX.class: $(srcdir)/HelloCgiX.xql
	CLASSPATH=@conf_classpath@$$CLASSPATH \
	$(KAWA) --servlet -C $(srcdir)/HelloCgiX.xql

HelloCgiX:
	$(LN_S) ../bin/cgi-servlet HelloCgiX

check-HelloCgiX: HelloCgiX.class HelloCgiX
	CLASSPATH=@conf_classpath@$$CLASSPATH \
	REQUEST_METHOD=GET SCRIPT_NAME=HelloCgiX SERVER_NAME=localhost \
	  PATH_TRANSLATED=./HelloCgiX KAWALIB=$(KAWALIB) ./HelloCgiX >tmp.out
	@if diff -b $(srcdir)/HelloCgiX.expected tmp.out; \
	  then echo '# HelloCgiX test passes'; \
	  else echo FAIL HelloCgiX test; fi

MySAXApp.class: $(srcdir)/MySAXApp.java
	CLASSPATH=..:. $(JAVAC) -g $(srcdir)/MySAXApp.java -d .

check-MySAXApp: MySAXApp.class
	CLASSPATH=..:. $(JAVA) -Dorg.xml.sax.driver=gnu.kawa.sax.KawaXMLReader\
	  MySAXApp ../gnu/xquery/testsuite/reviews.xml > tmp.out
	@if diff -b $(srcdir)/MySAXApp.expected tmp.out; \
	  then echo '# MySAXApp test passes'; \
	  else echo FAIL MySAXApp test; fi

MyFunc.class MyModule.class: $(srcdir)/MyFunc.java $(srcdir)/MyModule.java
	CLASSPATH=..:. $(JAVAC) -g -d . $?

MyDate.class MyTimestamp.class: $(srcdir)/MyDate.java $(srcdir)/MyTimestamp.java
	CLASSPATH=..:. $(JAVAC) -g -d . $?

check-pitfalls:
	$(KAWA) -f $(srcdir)/r5rs_pitfall.scm

APPLETVIEWER = appletviewer

# Not done by "make check" as it is interactive.
check-scribble: scribble.class
	$(APPLETVIEWER) -J-cp -J. -J-cp -J.. scribble.html

CLEANFILES = tmp? *.log testing.zip *.class HelloCgiK HelloCgiS
EXTRA_DIST = testing.scm test.scm num-test.scm mac-test.scm mac1.scm \
  classes1.scm classes2.scm SimpleB.java obj-test.scm arr-test.scm \
  MyFunc.java MyModule.java MyDate.java MyTimestamp.java \
  misc-test.scm polytype.scm formatst.scm scribble.scm \
  module1.scm module1a.scm module2.scm module3.scm included-1.scm \
  HelloCgiK.krl HelloCgiS.scm HelloCgiX.xql \
  ppfile.out scribble.html.in \
  MySAXApp.java MySAXApp.expected r5rs_pitfall.scm \
  HelloCgiK.expected HelloCgiS.expected HelloCgiX.expected
