# This file is part of Java-GNOME.
#
# Java-GNOME is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# Java-GNOME is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Java-GNOME; see the file COPYING.  If not, write to
# the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
#

CLASSPATH = @GTKJAR@:@CLASSPATH@:java
NATIVEGTKJAR = @NATIVEGTKJAR@

OBJECTS:= \
	$(shell ls jni/*.c  \
	| sort | sed -e 's=\.c=\.o=g')

SOURCE_PATH:=java/org/gnu/gnomevfs/*.java 

JAVA_SOURCE_PATH:=java

version = @version@
apiversion = @apiversion@
gnomevfsversion = @version@
gnomevfsapiversion = @apiversion@

INSTALL=@INSTALL@

# Set Java related stuff
JAVADOC = @JAVADOC@
JAVAC = @JAVAC@
JAVA = @JAVA@
JAR = @JAR@

# If you need to change this line please let us know
JAVA_INCLUDES = 
#@JAVA_INCLUDES@

# Set C related stuff
CC = @CC@
CFLAGS= @CFLAGS@ -fPIC -Wall
GTK_CFLAGS=@GTK_CFLAGS@
GTK_LIBS=@GTK_LIBS@
LIBGTKHTML_CFLAGS=@LIBGTKHTML_CFLAGS@
LIBGTKHTML_LIBS=@LIBGTKHTML_LIBS@
GNOMEVFS_CFLAGS=@GNOMEVFS_CFLAGS@
GNOMEVFS_LIBS=@GNOMEVFS_LIBS@
LIBGLADE_CFLAGS=@LIBGLADE_CFLAGS@
LIBGLADE_LIBS=@LIBGLADE_LIBS@
GCJ=@GCJ@ -fPIC -fjni
GCJFLAGS=
GCJ_CLASSPATH=@GCJ_CLASSPATH@.

.SUFFIXES: .java .class
.c.o:
	$(CC) -c $(CFLAGS) $< $(JAVA_INCLUDES) $(GNOMEVFS_CFLAGS) \
                -o $@

.java.o:
	$(GCJ) $(GCJFLAGS) -g -O --classpath=${GCJ_CLASSPATH} -c $< \
        -o $@

CLASSES:= \
	$(shell /bin/ls $(SOURCE_PATH) \
				| sort | sed -e 's=\.java=\.class=g' \
				| sed -e 's=\.\./==g' \
				| sed -e 's=\s*\(.*\)\s*= build-java/\1 =g' \
				| sed -e 's=build-java/.*/org/\(.*\)\s*= build-java/org/\1 =g' \
			) 

dirs: ../lib build-java

../lib/:
	$(INSTALL) -d ../lib
build-java:
	$(INSTALL) -d build-java

build-java/%.class: java/%.java
	CLASSPATH=$(CLASSPATH):$(JAVA_SOURCE_PATH) $(JAVAC) -d build-java   \
		$<
	
build-java/%.o: java/%.java
	$(GCJ) $(GCJFLAGS) -g -O --classpath=$(JAVA_SOURCE_PATH):${GCJ_CLASSPATH}:${CLASSPATH} -o $@ -c \
		$<
		
COMPILED_CLASSES:=$(shell echo $(CLASSES) | sed -e 's=\.class=\.o=g' )

../lib/gnomevfs${gnomevfsapiversion}.jar: $(CLASSES)
	@(cd build-java; $(JAR) cf ../../lib/gnomevfs${gnomevfsapiversion}-${gnomevfsversion}.jar `find -name "*.class"` )
	@(cd ../lib; test ! -L gnomevfs${gnomevfsapiversion}.jar && ln -s gnomevfs${gnomevfsapiversion}-${gnomevfsversion}.jar gnomevfs${gnomevfsapiversion}.jar) || exit 0

jar: dirs ../lib/gnomevfs${gnomevfsapiversion}.jar


../lib/libgnomevfsjava${gnomevfsapiversion}.so.${gnomevfsversion} : $(OBJECTS)
	@(test ! -d ../lib && mkdir ../lib) || exit 0
	$(CC) $(CFLAGS) -shared -o ../lib/libgnomevfsjava${gnomevfsapiversion}.so.${gnomevfsversion} \
		$(OBJECTS) \
		$(CFLAGS) @GNOMEVFS_CFLAGS@ @GNOMEVFS_LIBS@ $(JAVA_INCLUDES) -Xlinker --no-undefined
	@(cd ../lib; test ! -L libgnomevfsjava${gnomevfsapiversion}.so && ln -s libgnomevfsjava${gnomevfsapiversion}.so.${gnomevfsversion} libgnomevfsjava${gnomevfsapiversion}.so) || exit 0

nativelib: ../lib/libgnomevfsjava${gnomevfsapiversion}.so.${gnomevfsversion}

../lib/libgnomevfsjar${gnomevfsapiversion}.so.${gnomevfsversion}: $(COMPILED_CLASSES)
	$(GCJ) $(GCJFLAGS) -g -shared $(COMPILED_CLASSES) -o ../lib/libgnomevfsjar${gnomevfsapiversion}.so.${gnomevfsversion}
	@(cd ../lib; test ! -L libgnomevfsjar${gnomevfsapiversion}.so && ln -s libgnomevfsjar${gnomevfsapiversion}.so.${gnomevfsversion} libgnomevfsjar${gnomevfsapiversion}.so) || exit 0

native-gcj: ../lib/libgnomevfsjar${gnomevfsapiversion}.so.${gnomevfsversion}

all: dirs jar nativelib @GCJ_BUILD_TARGETS@

# Targets to clean up after us
.PHONY: mostlyclean clean distclean maintainer-clean dirs all native-gcj nativelib jar

mostlyclean:

clean: mostlyclean
	rm -f jni/*.o 
	rm -rf build-java/
	rm -rf ../lib/

distclean: clean
	rm -f Makefile

maintainer-clean: distclean

