# 
#  Makefile for the Freenet java reference implementation 
#  written by PiXeLpApst 
#  (C) by the Freenet project, GPL v2 applies
#

defaultrule: info

# If you use KAFFE, uncomment the following line
# or define the symbol KAFFE on the command line:
#KAFFE=true

#
# Some settings
#
ifndef JAVAC
  ifdef KAFFE
    JAVAC:=kaffe at.dms.kjc.Main
    JOPT:=-d ./
  else    
    JAVAC:=javac
  endif
endif

CLASSPATH:=$(dir $(shell pwd)):$(CLASSPATH)

SUBDIRS:=client crypt message node support

#
# Definition of the files belonging to the different packages:
#
pmessage := $(addprefix message/, \
 DataInsert DataReply DataRequest DataSend Echo HandshakeReply \
 HandshakeRequest InsertReply InsertRequest KeyedMM QueryRestarted \
 Request RequestCB RequestFailed RequestMM TimedOut )

psupport := $(addprefix support/, \
 BinaryTree ByteCounter Callback CyclicArray Logger SplitOutputStream \
 StreamElement StringKey TLL Ticker testCB Loader StringUtils)

pnode    := $(addprefix node/, \
 Data DataNotReadyException DataStore Node StandardDataStore \
 StandardMessageHandler )

pclient  := $(addprefix client/, \
 ClientCore ClientException ClientMessageHandler DotCounter \
 InsertClient RequestClient )
 # GUI ist not compiled automatically
 # Client, Scrambler and Zipper are not used yet

ptcp     := tcpAddress tcpConnection tcpListener tcpListeningAddress

pcore    := \
 Address BadHandshakeException Conduit ConnectFailedException \
 ConnectTimedOutException Connection ConnectionFactory ConnectionHandler \
 Core Handshake HandshakeFactory HandshakeHandler InvalidMessageException \
 Key KeyException Listener ListenerFactory ListeningAddress Message \
 MessageFactory MessageHandler MessageMemory Params ProtocolAddress \
 ProtocolListeningAddress RawMessage SendFailedException \
 StandardHandshakeHandler Template
 
pcrypt   := $(addprefix crypt/,SHA1)

nodefiles := $(pcore) $(pnode) $(pmessage) $(ptcp) $(psupport)

clientfiles := $(pcore) $(pclient) $(pmessage) $(ptcp) $(psupport) $(pcrypt)

#
# Compilations rules
#
.PHONY: defaultrule info devinfo node cli client \
        gui clean distclean install jar update

.SUFFIXES: .java .class

# generic rule
.java.class:
	$(JAVAC) $(JOPT) $^


#
# Main compilation targets:
#

# Build the node:
node : $(addsuffix .class,$(nodefiles))

# Build the Freenet command-line clients:
cli client : $(addsuffix .class,$(clientfiles))

# Build the Freenet swigg client:
gui: client client/GUI.class


#
# Other useful targets: 
#

# Clean up all class files:
clean:
	-rm -f *.class $(addsuffix /*.class,$(SUBDIRS))
	-rm -f *~ $(addsuffix /*~,$(SUBDIRS))

distclean: clean
	rm -f scripts/freenet_server scripts/freenet_insert
	rm -f freenet.jar scripts/freenet.tgz scripts/freenet.zip

# for the lazy programmers:
update:
	cvs update -d -P

# Install the stuff:
install:
	cd scripts ; ./install.sh ; cd ..

# Make a jar-file (java archive) of all compiled(!) classes
# BTW I include here the client classes, is this correct ?
jar:
	cd ..; jar -cfmv Freenet/freenet.jar Freenet/node.manifest \
	 $(addsuffix /*.class, $(addprefix Freenet/,$(SUBDIRS))) ;



#
# Some classes that are created from other java files
#
# I know this is a mess and I'm not sure if all these dependencys work right
# but I'm not really a java programmer so if you have any better idea, tell me
#
support/TLL.class : support/Ticker.java
support/testCB.class : support/Ticker.java
support/Branch.class : support/BinaryTree.java
support/StreamElement.class : support/SplitOutputStream.java
message/Request$RequestAbortException : message/Request


#
# Display some Infos
#
info:
	@echo "You can compile the following targets:"
	@echo "  make node  - compiles the freenet node"
	@echo "  make cli   - compiles the command line interface client (simple debug-tool)"
	@echo "  make gui   - compiles the GUI client (requires JDK 1.2)"
	@echo "  make clean - remove all the .class files (use distclean for redistribution)"
	@echo "  make jar   - make a jar file of all .class files"
	@echo ""
	@echo "Please type \"make devinfo\" if you are a developer."

devinfo:
	@echo "Short Info for developers and users of cvs :"
	@echo ""
	@echo "Make needs to know the dependencies between the files it shall compile."
	@echo "However I do not know the exact dependencies for most of the class files,"
	@echo "as the corresponding .java file have constructs like \"import Freenet.*\","
	@echo "which isn't exactly clear. So I did the following: I created a dependecy list"
	@echo "for each of the main targets, but not for the individual files. This means"
	@echo "that a \"make node\" should recompile the correct files (i.e. all classes that"
	@echo "were modified and those that depend on them) almoust always, which should be"
	@echo "enough for normal users, but for example a \"make Message.class\" will _not_"
	@echo "work as expected. I know this suxx somewhat but I can't change it."
	@echo "IF your java compiler does resolve the dependencies and check for changed files"
	@echo "on its own, you can just bypass the makefile. However, mine does not make a"
	@echo "very good job of this."
	@echo "BTW the reason why these fuzzy dependecy declarations work at all is that Java"
	@echo "always links dynamically (at least that's what i'm told)."
	@echo "Anyway if you know any better way to write a makefile for this pile of source,"
	@echo "please let me know (pixelpapst@users.sourceforge.net). And yes, I know that"
	@echo "there is a build script which recompiles all, but that's just not the point."
	@echo "                                                                PiXeLpApst"
	@echo ""

