#########################################################################
# COMMON PROTOLIB MAKEFILE STUFF
#

SHELL=/bin/sh

.SUFFIXES: .cpp $(.SUFFIXES)

COMMON = ../src/common
EXAMPLES = ../examples
UNIX = ../src/unix
NS = ../src/sim/ns
MANET = ../src/manet
JAVA =  ../src/java

INCLUDES = $(TCL_INCL_PATH) $(SYSTEM_INCLUDES) -I../include -I/usr/include/libxml2

CFLAGS = -g -DPROTO_DEBUG -DUNIX -D_FILE_OFFSET_BITS=64 -O $(SYSTEM_CFLAGS) $(SYSTEM_HAVES) $(INCLUDES) $(ADDED_CFLAGS)

LDFLAGS = $(SYSTEM_LDFLAGS)

#LIBS = $(SYSTEM_LIBS) -lm -lxml2 -lpthread 
LIBS = $(SYSTEM_LIBS) -lm -lxml2 -pthread 

TARGETS = libprotokit.a libprotokit

# Rule for C++ .cpp extension
.cpp.o:
	$(CC) -c $(CFLAGS) -o $*.o $*.cpp

allExamples: arposer averageExample base64Example detourExample graphExample graphRider graphXMLExample \
jsonExample lfsrExample msg2MsgExample msgExample netExample pcmd pipe2SockExample pipeExample pcapReplay \
protoCapExample protoFileExample queueExample riposer serialExample simpleTcpExample sock2PipeExample \
threadExample timerTest ting treeTest vifExample vifLan protoExample eventExample tokenatorExample unitTests

KIT_SRC = $(COMMON)/protoAddress.cpp  $(COMMON)/protoApp.cpp $(COMMON)/protoBase64.cpp \
          $(COMMON)/protoBitmask.cpp $(COMMON)/protoCap.cpp $(COMMON)/protoChannel.cpp \
          $(COMMON)/protoCheck.cpp $(COMMON)/protoDebug.cpp $(COMMON)/protoDispatcher.cpp \
          $(COMMON)/protoEvent.cpp $(COMMON)/protoFlow.cpp  $(COMMON)/protoPipe.cpp \
          $(COMMON)/protoJson.cpp $(COMMON)/protoPkt.cpp $(COMMON)/protoPktARP.cpp \
          $(COMMON)/protoPktETH.cpp $(COMMON)/protoPktGRE.cpp $(COMMON)/protoPktIGMP.cpp \
          $(COMMON)/protoPktIP.cpp $(COMMON)/protoPktTCP.cpp $(COMMON)/protoPktRIP.cpp \
          $(COMMON)/protoPktRTP.cpp $(COMMON)/protoSocket.cpp $(COMMON)/protoRouteMgr.cpp \
          $(COMMON)/protoRouteTable.cpp $(COMMON)/protoTime.cpp $(COMMON)/protoTimer.cpp \
          $(COMMON)/protoTree.cpp $(COMMON)/protoList.cpp $(COMMON)/protoQueue.cpp \
          $(COMMON)/protoVif.cpp $(COMMON)/protoSerial.cpp $(COMMON)/protoLFSR.cpp \
          $(COMMON)/protoNet.cpp $(COMMON)/protoFile.cpp $(COMMON)/protoString.cpp \
          $(SYSTEM_SRC)

KIT_OBJ = $(KIT_SRC:.cpp=.o)

libprotokit.a:    $(KIT_OBJ)
	rm -f $@ 
	$(AR) rcv $@ $(KIT_OBJ)
	$(RANLIB) $@
	mkdir -p ../lib
	cp $@ ../lib/$@

# Build libprotokit as a shared library (i.e., libprotokit.so, libprotokit.dylib, etc)
libprotokit.$(SYSTEM_SOEXT): $(KIT_OBJ)
	$(CC) $(SYSTEM_SOFLAGS) $(KIT_OBJ) -o libprotokit.$(SYSTEM_SOEXT) $(LIBS)
	mkdir -p ../lib
	cp $@ ../lib/$@

# This builds the Protolib JNI stub libary for Java interfaces
#  (currently this consists of an interface to the ProtoPipe class)
LIBPROTO = ../lib/libprotokit.a
JNI_SRC = $(JAVA)/protoPipeJni.cpp 
JNI_OBJ = $(JNI_SRC:.cpp=.o)

libprotolibjni.$(SYSTEM_JNIEXT): $(JNI_OBJ) libprotokit.a
	$(CC) $(SYSTEM_JNIFLAGS) $(SYSTEM_FRAMEORK) $(JNI_OBJ) $(LIBPROTO) \
	$(LIBS) $(LDFLAGS) -o $@
	mkdir -p ../lib
	cp $@ ../lib/$@

        
# Simple example command-line application using socket & timer

EXAMPLE_SRC = $(EXAMPLES)/protoExample.cpp
EXAMPLE_OBJ = $(EXAMPLE_SRC:.cpp=.o)

protoExample:    $(EXAMPLE_OBJ) libprotokit.a
	$(CC) $(CFLAGS) -o $@ $(EXAMPLE_OBJ) $(LDFLAGS) $(LIBS) libprotokit.a 
	mkdir -p ../bin
	cp $@ ../bin/$@

# Runs a few unit tests

UNITTESTS_SRC = $(EXAMPLES)/unitTests.cpp $(EXAMPLES)/testFuncs.cpp $(MANET)/manetMsg.cpp
UNITTESTS_OBJ = $(UNITTESTS_SRC:.cpp=.o)

unitTests:    $(UNITTESTS_OBJ) libprotokit.a
	$(CC) $(CFLAGS) -o $@ $(UNITTESTS_OBJ) $(LDFLAGS) $(LIBS) libprotokit.a 
	mkdir -p ../bin
	cp $@ ../bin/$@


# Simple example command-line tcp client or server application

TCP_EXAMPLE_SRC = $(EXAMPLES)/simpleTcpExample.cpp
TCP_EXAMPLE_OBJ = $(TCP_EXAMPLE_SRC:.cpp=.o)

simpleTcpExample:    $(TCP_EXAMPLE_OBJ) libprotokit.a
	$(CC) $(CFLAGS) -o $@ $(TCP_EXAMPLE_OBJ) $(LDFLAGS) $(LIBS) libprotokit.a 
	mkdir -p ../bin
	cp $@ ../bin/$@

# Simple example using our ProtoFile file I/O class to async listen to a file
FILE_EXAMPLE_SRC = $(EXAMPLES)/protoFileExample.cpp
FILE_EXAMPLE_OBJ = $(FILE_EXAMPLE_SRC:.cpp=.o)

protoFileExample:    $(FILE_EXAMPLE_OBJ) libprotokit.a
	$(CC) $(CFLAGS) -o $@ $(FILE_EXAMPLE_OBJ) $(LDFLAGS) $(LIBS) libprotokit.a
	mkdir -p ../bin
	cp $@ ../bin/$@

# Simple ProtoCap example that promiscuously responds to ARP requests
ARPOSER_SRC = $(EXAMPLES)/arposer.cpp $(SYSTEM_SRC_EX) 
ARPOSER_OBJ = $(ARPOSER_SRC:.cpp=.o)

arposer:    $(ARPOSER_OBJ) libprotokit.a
	$(CC) $(CFLAGS) -o $@ $(ARPOSER_OBJ) $(LDFLAGS) $(LIBS) $(SYSTEM_LIB_EX) libprotokit.a 
	mkdir -p ../bin
	cp $@ ../bin/$@


# Simple example that monitors RIP message and can advertise/inject a RIP route
RIPOSER_SRC = $(EXAMPLES)/riposer.cpp
RIPOSER_OBJ = $(RIPOSER_SRC:.cpp=.o)

riposer:    $(RIPOSER_OBJ) libprotokit.a
	$(CC) $(CFLAGS) -o $@ $(RIPOSER_OBJ) $(LDFLAGS) $(LIBS) $(SYSTEM_LIB_EX) libprotokit.a 
	mkdir -p ../bin
	cp $@ ../bin/$@
        
# Simple example using our ProtoVif "virtual interface" class
VIF_EXAMPLE_SRC = $(EXAMPLES)/vifExample.cpp $(COMMON)/protoVif.cpp $(SYSTEM_SRC_EX) 
VIF_EXAMPLE_OBJ = $(VIF_EXAMPLE_SRC:.cpp=.o)

vifExample:    $(VIF_EXAMPLE_OBJ) libprotokit.a
	$(CC) $(CFLAGS) -o $@ $(VIF_EXAMPLE_OBJ) $(LDFLAGS) $(LIBS) $(SYSTEM_LIB_EX) libprotokit.a 
	mkdir -p ../bin
	cp $@ ../bin/$@

# Uses ProtoVif "virtual interface" class to create a virtual LAN

LAN_EXAMPLE_SRC = $(EXAMPLES)/vifLan.cpp $(COMMON)/protoVif.cpp $(SYSTEM_SRC_EX) 
LAN_EXAMPLE_OBJ = $(LAN_EXAMPLE_SRC:.cpp=.o)

vifLan:    $(LAN_EXAMPLE_OBJ) libprotokit.a
	$(CC) $(CFLAGS) -o $@ $(LAN_EXAMPLE_OBJ) $(LDFLAGS) $(LIBS) $(SYSTEM_LIB_EX) libprotokit.a 
	mkdir -p ../bin
	cp $@ ../bin/$@
    
TING_SRC = $(EXAMPLES)/ting.cpp
TING_OBJ = $(TING_SRC:.cpp=.o)

ting:    $(TING_OBJ) libprotokit.a
	$(CC) $(CFLAGS) -o $@ $(TING_OBJ) $(LDFLAGS) $(LIBS) libprotokit.a   
	mkdir -p ../bin
	cp $@ ../bin/$@  
            
TIMER_TEST_SRC = $(EXAMPLES)/timerTest.cpp
TIMER_TEST_OBJ = $(TIMER_TEST_SRC:.cpp=.o)

timerTest:    $(TIMER_TEST_OBJ) libprotokit.a
	$(CC) $(CFLAGS) -o $@ $(TIMER_TEST_OBJ) $(LDFLAGS) $(LIBS) libprotokit.a   
	mkdir -p ../bin
	cp $@ ../bin/$@  
   
   
TREE_TEST_SRC = $(EXAMPLES)/treeTest.cpp
TREE_TEST_OBJ = $(TREE_TEST_SRC:.cpp=.o)

treeTest:    $(TREE_TEST_OBJ) libprotokit.a
	$(CC) $(CFLAGS) -o $@ $(TREE_TEST_OBJ) $(LDFLAGS) $(LIBS) libprotokit.a   
	mkdir -p ../bin
	cp $@ ../bin/$@     
    
TIMER_SCALE_SRC = $(EXAMPLES)/timerScaling.cpp
TIMER_SCALE_OBJ = $(TIMER_SCALE_SRC:.cpp=.o)

timerScale:    $(TIMER_SCALE_OBJ) libprotokit.a
	$(CC) $(CFLAGS) -o $@ $(TIMER_SCALE_OBJ) $(LDFLAGS) $(LIBS) libprotokit.a   
	mkdir -p ../bin
	cp $@ ../bin/$@  
    
THREAD_SRC = $(EXAMPLES)/threadExample.cpp
THREAD_OBJ = $(THREAD_SRC:.cpp=.o)

threadExample:    $(THREAD_OBJ) libprotokit.a
	$(CC) $(CFLAGS) -o $@ $(THREAD_OBJ) $(LDFLAGS) $(LIBS) libprotokit.a
	mkdir -p ../bin
	cp $@ ../bin/$@

EVENT_SRC = $(EXAMPLES)/eventExample.cpp
EVENT_OBJ = $(EVENT_SRC:.cpp=.o)

eventExample:    $(EVENT_OBJ) libprotokit.a
	$(CC) $(CFLAGS) -o $@ $(EVENT_OBJ) $(LDFLAGS) $(LIBS) libprotokit.a
	mkdir -p ../bin
	cp $@ ../bin/$@
    
TK_SRC = $(EXAMPLES)/tokenatorExample.cpp
TK_OBJ = $(TK_SRC:.cpp=.o)

tokenatorExample:    $(TK_OBJ) libprotokit.a
	$(CC) $(CFLAGS) -o $@ $(TK_OBJ) $(LDFLAGS) $(LIBS) libprotokit.a
	mkdir -p ../bin
	cp $@ ../bin/$@    
    
SOCK2PIPE_SRC = $(EXAMPLES)/sock2PipeExample.cpp
SOCK2PIPE_OBJ = $(SOCK2PIPE_SRC:.cpp=.o)

sock2PipeExample:    $(SOCK2PIPE_OBJ) libprotokit.a
	$(CC) $(CFLAGS) -o $@ $(SOCK2PIPE_OBJ) $(LDFLAGS) $(LIBS) libprotokit.a
	mkdir -p ../bin
	cp $@ ../bin/$@

PIPE2SOCK_SRC = $(EXAMPLES)/pipe2SockExample.cpp
PIPE2SOCK_OBJ = $(PIPE2SOCK_SRC:.cpp=.o)

pipe2SockExample:    $(PIPE2SOCK_OBJ) libprotokit.a
	$(CC) $(CFLAGS) -o $@ $(PIPE2SOCK_OBJ) $(LDFLAGS) $(LIBS) libprotokit.a
	mkdir -p ../bin
	cp $@ ../bin/$@

MSG2MSG_SRC = $(EXAMPLES)/msg2MsgExample.cpp 
MSG2MSG_OBJ = $(MSG2MSG_SRC:.cpp=.o)

msg2MsgExample:    $(MSG2MSG_OBJ) libprotokit.a
	$(CC) $(CFLAGS) -o $@ $(MSG2MSG_OBJ) $(LDFLAGS) $(LIBS) libprotokit.a
	mkdir -p ../bin
	cp $@ ../bin/$@

PCMD_SRC = $(EXAMPLES)/pcmd.cpp
PCMD_OBJ = $(PCMD_SRC:.cpp=.o)

pcmd:    $(PCMD_OBJ) libprotokit.a
	$(CC) $(CFLAGS) -o $@ $(PCMD_OBJ) $(LDFLAGS) $(LIBS) libprotokit.a
	mkdir -p ../bin
	cp $@ ../bin/$@
    
PIPE_SRC = $(EXAMPLES)/pipeExample.cpp
PIPE_OBJ = $(PIPE_SRC:.cpp=.o)

pipeExample:    $(PIPE_OBJ) libprotokit.a
	$(CC) $(CFLAGS) -o $@ $(PIPE_OBJ) $(LDFLAGS) $(LIBS) libprotokit.a
	mkdir -p ../bin
	cp $@ ../bin/$@
    
JOIN_SRC = $(EXAMPLES)/join.cpp $(SYSTEM_SRC_EX)
JOIN_OBJ = $(JOIN_SRC:.cpp=.o)

join:    $(JOIN_OBJ) libprotokit.a
	$(CC) $(CFLAGS) -o $@ $(JOIN_OBJ) $(LDFLAGS) $(LIBS) $(SYSTEM_LIB_EX) libprotokit.a
	mkdir -p ../bin
	cp $@ ../bin/$@

NET_SRC = $(EXAMPLES)/netExample.cpp
NET_OBJ = $(NET_SRC:.cpp=.o)

netExample:    $(NET_OBJ) libprotokit.a
	$(CC) $(CFLAGS) -o $@ $(NET_OBJ) $(LDFLAGS) $(LIBS) libprotokit.a
	mkdir -p ../bin
	cp $@ ../bin/$@

CAP_SRC = $(EXAMPLES)/protoCapExample.cpp $(SYSTEM_SRC_EX)
CAP_OBJ = $(CAP_SRC:.cpp=.o)
protoCapExample:    $(CAP_OBJ) libprotokit.a
	$(CC) $(CFLAGS) -o $@ $(CAP_OBJ) $(LDFLAGS) $(LIBS) $(SYSTEM_LIB_EX) libprotokit.a
	mkdir -p ../bin
	cp $@ ../bin/$@

REPLAY_SRC = $(EXAMPLES)/pcapReplay.cpp $(SYSTEM_SRC_EX)
REPLAY_OBJ = $(REPLAY_SRC:.cpp=.o)
pcapReplay:    $(REPLAY_OBJ) libprotokit.a
	$(CC) $(CFLAGS) -o $@ $(REPLAY_OBJ) $(LDFLAGS) $(LIBS) $(SYSTEM_LIB_EX) -lpcap libprotokit.a
	mkdir -p ../bin
	cp $@ ../bin/$@

      
DETOUR_SRC = $(EXAMPLES)/detourExample.cpp $(SYSTEM_SRC_EX)
DETOUR_OBJ = $(DETOUR_SRC:.cpp=.o)
detourExample:    $(DETOUR_OBJ) libprotokit.a
	$(CC) $(CFLAGS) -o $@ $(DETOUR_OBJ) $(LDFLAGS) $(LIBS) $(SYSTEM_LIB_EX) libprotokit.a
	mkdir -p ../bin
	cp $@ ../bin/$@


SERIAL_EXAMPLE_SRC = $(EXAMPLES)/serialExample.cpp $(COMMON)/protoSerial.cpp $(SYSTEM_SRC_EX)
SERIAL_EXAMPLE_OBJ = $(SERIAL_EXAMPLE_SRC:.cpp=.o)

serialExample:    $(SERIAL_EXAMPLE_OBJ) libprotokit.a
	$(CC) $(CFLAGS) -o $@ $(SERIAL_EXAMPLE_OBJ) $(LDFLAGS) $(LIBS) $(SYSTEM_LIB_EX) libprotokit.a 
	mkdir -p ../bin
	cp $@ ../bin/$@
    
LFSR_EXAMPLE_SRC = $(EXAMPLES)/lfsrExample.cpp
LFSR_EXAMPLE_OBJ = $(LFSR_EXAMPLE_SRC:.cpp=.o)

lfsrExample:    $(LFSR_EXAMPLE_OBJ) libprotokit.a
	$(CC) $(CFLAGS) -o $@ $(LFSR_EXAMPLE_OBJ) $(LDFLAGS) $(LIBS) libprotokit.a 
	mkdir -p ../bin
	cp $@ ../bin/$@
        
BASE64_EXAMPLE_SRC = $(EXAMPLES)/base64Example.cpp
BASE64_EXAMPLE_OBJ = $(BASE64_EXAMPLE_SRC:.cpp=.o)

base64Example:    $(BASE64_EXAMPLE_OBJ) libprotokit.a
	$(CC) $(CFLAGS) -o $@ $(BASE64_EXAMPLE_OBJ) $(LDFLAGS) $(LIBS) libprotokit.a 
	mkdir -p ../bin
	cp $@ ../bin/$@
    
QUEUE_SRC = $(EXAMPLES)/queueExample.cpp
QUEUE_OBJ = $(QUEUE_SRC:.cpp=.o)
queueExample:    $(QUEUE_OBJ) libprotokit.a
	$(CC) $(CFLAGS) -o $@ $(QUEUE_OBJ) $(LDFLAGS) $(LIBS) libprotokit.a
	mkdir -p ../bin
	cp $@ ../bin/$@
    
AVERAGE_SRC = $(EXAMPLES)/averageExample.cpp $(COMMON)/protoAverage.cpp
AVERAGE_OBJ = $(AVERAGE_SRC:.cpp=.o)
averageExample:    $(AVERAGE_OBJ) libprotokit.a
	$(CC) $(CFLAGS) -o $@ $(AVERAGE_OBJ) $(LDFLAGS) $(LIBS) libprotokit.a
	mkdir -p ../bin
	cp $@ ../bin/$@
            
GRAPH_SRC = $(EXAMPLES)/graphExample.cpp $(MANET)/manetGraph.cpp  \
          $(COMMON)/protoGraph.cpp $(COMMON)/protoSpace.cpp
GRAPH_OBJ = $(GRAPH_SRC:.cpp=.o)
graphExample:    $(GRAPH_OBJ) libprotokit.a
	$(CC) $(CFLAGS) -o $@ $(GRAPH_OBJ) $(LDFLAGS) $(LIBS) libprotokit.a
	mkdir -p ../bin
	cp $@ ../bin/$@

GRAPH_RIDER_SRC = $(EXAMPLES)/graphRider.cpp $(COMMON)/protoGraph.cpp \
		$(MANET)/manetGraph.cpp $(MANET)/manetGraphML.cpp
GRAPH_RIDER_OBJ = $(GRAPH_RIDER_SRC:.cpp=.o)
graphRider:    $(GRAPH_RIDER_OBJ) libprotokit.a
	$(CC) $(CFLAGS) -o $@ $(GRAPH_RIDER_OBJ) $(LDFLAGS) $(LIBS) libprotokit.a
	mkdir -p ../bin
	cp $@ ../bin/$@


GRAPHXML_SRC = $(MANET)/manetGraphML.cpp $(EXAMPLES)/graphXMLExample.cpp $(MANET)/manetGraph.cpp  \
          $(COMMON)/protoGraph.cpp $(COMMON)/protoSpace.cpp
GRAPHXML_OBJ = $(GRAPHXML_SRC:.cpp=.o)
graphXMLExample:    $(GRAPHXML_OBJ) libprotokit.a
	$(CC) $(CFLAGS) -o $@ $(GRAPHXML_OBJ) $(LDFLAGS) $(LIBS) libprotokit.a
	mkdir -p ../bin
	cp $@ ../bin/$@

# GraphRider (gr) tool
GR_SRC = $(EXAMPLES)/graphRider.cpp $(MANET)/manetGraph.cpp  \
          $(COMMON)/protoGraph.cpp $(COMMON)/protoSpace.cpp
GR_OBJ = $(GR_SRC:.cpp=.o)
gr:    $(GR_OBJ) libprotokit.a
	$(CC) $(CFLAGS) -o $@ $(GR_OBJ) $(LDFLAGS) $(LIBS) libprotokit.a
	mkdir -p ../bin
	cp $@ ../bin/$@
                
MSG_SRC = $(EXAMPLES)/msgExample.cpp $(EXAMPLES)/testFuncs.cpp $(MANET)/manetMsg.cpp 
MSG_OBJ = $(MSG_SRC:.cpp=.o)
msgExample:    $(MSG_OBJ) libprotokit.a
	$(CC) $(CFLAGS) -o $@ $(MSG_SRC) $(LDFLAGS) $(LIBS) libprotokit.a
	mkdir -p ../bin
	cp $@ ../bin/$@
    
    
FILE_SRC = $(EXAMPLES)/fileTest.cpp
FILE_OBJ = $(FILE_SRC:.cpp=.o)
fileTest:    $(FILE_OBJ) libprotokit.a
	$(CC) $(CFLAGS) -o $@ $(FILE_SRC) $(LDFLAGS) $(LIBS) libprotokit.a
	mkdir -p ../bin
	cp $@ ../bin/$@    
    
JSON_SRC = $(EXAMPLES)/jsonExample.cpp 
JSON_OBJ = $(JSON_SRC:.cpp=.o)
jsonExample:    $(JSON_OBJ) libprotokit.a
	$(CC) $(CFLAGS) -o $@ $(JSON_SRC) $(LDFLAGS) $(LIBS) libprotokit.a
	mkdir -p ../bin
	cp $@ ../bin/$@

ZMQ_SRC = $(EXAMPLES)/zmqExample.cpp $(COMMON)/protoZMQ.cpp $(COMMON)/protoThread.cpp
ZMQ_OBJ = $(ZMQ_SRC:.cpp=.o)
zmqExample:    $(ZMQ_OBJ) libprotokit.a
	$(CC) $(CFLAGS) -o $@ $(ZMQ_SRC) $(LDFLAGS) $(LIBS) libprotokit.a -lzmq
	mkdir -p ../bin
	cp $@ ../bin/$@

STREE_SRC = $(EXAMPLES)/sortedTreeExample.cpp
STREE_OBJ = $(STREE_SRC:.cpp=.o)
stree:    $(STREE_OBJ) libprotokit.a
	$(CC) $(CFLAGS) -o $@ $(STREE_SRC) $(LDFLAGS) $(LIBS) libprotokit.a
	mkdir -p ../bin
	cp $@ ../bin/$@
            
clean:	
	rm -f *.o $(COMMON)/*.o $(MANET)/*.o $(NS)/*.o ../src/*/*.o ../examples/*.o \
        *.a *.$(SYSTEM_SOEXT) ../lib/*.a ../lib/* ../bin/* $(SYSTEM_SOEXT) \
        arposer averageExample base64Example detourExample graphExample graphRider graphXMLExample jsonExample lfsrExample msg2MsgExample msgExample netExample pcmd pipe2SockExample pipeExample protoCapExample protoApp protoExample protoFileExample queueExample riposer serialExample simpleTcpExample sock2PipeExample threadExample timerTest ting vifExample vifLan gr
	rm -rf ../build/* ../protokit.egg-info
    

# DO NOT DELETE THIS LINE -- mkdep uses it.
# DO NOT PUT ANYTHING AFTER THIS LINE, IT WILL GO AWAY.



