CC = @CC@
CFLAGS = @CFLAGS@
CPPFLAGS = @CPPFLAGS@
DEFS = @DEFS@
LDFLAGS = @LDFLAGS@
LIBS = @LIBS@
LIBTOOL = @LIBTOOL@

prefix = @prefix@
exec_prefix = @exec_prefix@
libdir = @libdir@

SHELL = @SHELL@
top_builddir = @top_builddir@
top_srcdir = @top_srcdir@
srcdir = @srcdir@
VPATH = @srcdir@

JNI_CFLAGS = @JNI_CFLAGS@
WFDB_CFLAGS = @WFDB_CFLAGS@
WFDB_LIBS = @WFDB_LIBS@

JAVA = @JAVA@
JAVAH = @JAVAH@

classdir = $(srcdir)/../../bin/classes

all: librdsampjni.la

check: librdsampjni.la
	rm -rf test.jar nativelibs
	jar cf test.jar -C $(classdir) .
	$(MAKE) install libdir=`pwd`/nativelibs/custom/lib
	$(JAVA) -cp test.jar org.physionet.wfdb.jni.Rdsamp -r 100s -t s10 > 100s.out
	diff 100s.out $(srcdir)/100s.expected

install: librdsampjni.la
	mkdir -p $(libdir)
	$(LIBTOOL) --mode=install cp librdsampjni.la $(libdir)

librdsampjni.la: rdsampjni.lo
	$(LIBTOOL) --mode=link $(CC) $(CLFAGS) $(LDFLAGS) \
	  -avoid-version -no-undefined -rpath /fnord \
	  rdsampjni.lo -o librdsampjni.la \
	  $(WFDB_LIBS) $(LIBS)

rdsampjni.lo: rdsampjni.c org_physionet_wfdb_jni_Rdsamp.h
	$(LIBTOOL) --mode=compile $(CC) $(CFLAGS) $(CPPFLAGS) \
	  $(DEFS) $(WFDB_CFLAGS) $(JNI_CFLAGS) -I. \
	  -c $(srcdir)/rdsampjni.c

org_physionet_wfdb_jni_Rdsamp.h: $(classdir)/org/physionet/wfdb/jni/Rdsamp.class
	$(JAVAH) -classpath $(classdir) org.physionet.wfdb.jni.Rdsamp

clean:
	$(LIBTOOL) --mode=clean rm -f *.la *.lo
	rm -f org_physionet_wfdb_jni_Rdsamp.h
	rm -rf test.jar nativelibs 100s.out

distclean: clean
	rm -f config.status config.log libtool
	rm -f Makefile

Makefile: Makefile.in config.status
	$(SHELL) ./config.status
config.status: configure
	$(SHELL) ./config.status --recheck

.PHONY: all clean distclean install check
