###############################################################################
#                                                                             #
#   Copyright 2005 University of Cambridge Computer Laboratory.               #
#                                                                             #
#   This file is part of Nprobe.                                              #
#                                                                             #
#   Nprobe 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 of the License, or         #
#   (at your option) any later version.                                       #
#                                                                             #
#   Nprobe 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 Nprobe; if not, write to the Free Software                     #
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA #
#                                                                             #
###############################################################################

#
# Where to find various stuff
#

# Archive root
NPROBE_ROOT = ../..

# Import all relative paths defined at top level
include $(NPROBE_ROOT)/Makefile.paths

###############################################################################
#
# Defines for pcap module compilation
#

SWIG_DEFINES = -DWREAD -DSWIG

DEFINES = $(SWIG_DEFINES) $(NPROBE_DEFINES)

SWIG_ONLY_DEFINES = -python -shadow -DSWIG_ONLY $(SWIG_ARCH_DEF)

###############################################################################

#
# cc options and include directives
#
CCOPT = -Dmain=oldmain

INCLUDES = $(PYTHON_INCLUDES) -I $(LIBPCAP_DIR) -I$(COMMON_HDR_DIR)

###############################################################################
#
# Standard CFLAGS
#
CFLAGS = $(CCOPT) $(DEFINES) $(INCLUDES)

CC = gcc -g -Wall -fpic

###############################################################################

AS = as
MAKE = make

###############################################################################

# 
# Where to find source not in this directory
#
vpath %.c $(COMMON_SRC_DIR)
vpath %.h $(COMMON_HDR_DIR)

###############################################################################

PCAP_LIB = dmux.py DMuxer.py FlowDmuxerDmux.py pcap.py pcapc.so

PCAP_SCRIPTS = dmux.py

###############################################################################

LIBPCAP_OBJ = $(addprefix $(LIBPCAP_DIR)/, \
	pcap-linux.o pcap.o inet.o gencode.o optimize.o nametoaddr.o \
	etherent.o savefile.o bpf_filter.o bpf_image.o scanner.o grammar.o \
	version.o) zflag.o

###############################################################################

CSRC = $(addprefix $(COMMON_SRC_DIR)/, errstring.c)
PY_PCAP_OBJ = pcap_wrap.o errstring.o

###############################################################################

# Explicit dependency rule for *.o where *.c resides in other dir
%.o : %.c
	rm -f $@;
	$(CC) $(CFLAGS) -c $< -o $@

###############################################################################

all: libpcap pcap.py pcapc.so

libpcap:
	$(MAKE) -C $(TD_ROOT) $(MFLAGS) libpcap

pcap.py pcap_wrap.o: pcap.i
	$(SWIG) $(SWIG_ONLY_DEFINES) pcap.i
	$(CC) $(CFLAGS) -c pcap_wrap.c

#pcapc.so: pcap_wrap.o $(LIBPCAP_OBJ)
pcapc.so: $(PY_PCAP_OBJ) $(LIBPCAP_OBJ)
	$(MAKE) -C $(LIBPCAP_DIR) $(MFLAGS) all
	gcc -shared -o pcapc.so $(PY_PCAP_OBJ) $(LIBPCAP_OBJ)
	#gcc -shared -o pcapc.so pcap_wrap.o $(LIBPCAP_OBJ)

install update_install: all install_dirs install_lib install_bin

install_lib: $(PCAP_LIB)
	@cp -ufv $(PCAP_LIB) $(INSTALL_LIB_DIR)

install_bin: $(PCAP_SCRIPTS)
	@cp -ufv $(PCAP_SCRIPTS) $(INSTALL_BIN_DIR)

# Create install dirs if necessary
install_dirs: 
	@if [ ! -d $(INSTALL_LIB_DIR) ]; \
		then \
			mkdir -p $(INSTALL_LIB_DIR); \
			echo Created $(INSTALL_LIB_DIR); \
	fi
	@if [ ! -d $(INSTALL_BIN_DIR) ]; \
		then \
			mkdir -p $(INSTALL_BIN_DIR); \
			echo Created $(INSTALL_BIN_DIR); \
	fi

tidy:
	rm -f *.o 

clean: tidy
	 rm -f pcap.py pcapc.so pcap_wrap.c pcap_wrap.doc *.pyc

very_clean test_clean: clean
	rm -vf $(DEPENDENCY_FILE) $(DEPENDENCY_FILE).bak

clobber: very_clean 
	@rm -vf $(addprefix $(INSTALL_LIB_DIR)/, $(PCAP_LIB)) 
	@rm -vf $(addprefix $(INSTALL_LIB_DIR)/, $(addsuffix .pyc, $(basename $(PCAP_LIB))))
	@rm -vf $(addprefix $(INSTALL_BIN_DIR)/, $(PCAP_SCRIPTS))
	@rm -vf $(addprefix $(INSTALL_BIN_DIR)/, $(addsuffix .pyc, $(basename $(PCAP_SCRIPTS))))

###############################################################################

#
# Build dependencies in three stages:
# (i) As normal for C src
# (ii) Append for interface hdrs - maintain .h suffix
# (iii) Remove unwanted path prefixes
#

DEPENDENCY_FILE = py_pcap.d

$(DEPENDENCY_FILE) :
	if [ ! -f $(DEPENDENCY_FILE) ]; then touch $(DEPENDENCY_FILE); fi

depend: $(DEPENDENCY_FILE)
	makedepend -f$(DEPENDENCY_FILE) -- $(DEFINES) -- $(INCLUDES) $(CSRC)
	makedepend -f$(DEPENDENCY_FILE) -o .h -a -- $(DEFINES) -- $(INCLUDES) $(HDR)
	sed -e 's/$(subst /,\/,$(COMMON_SRC_DIR))\///' $(DEPENDENCY_FILE) > deps.tmp
	mv deps.tmp $(DEPENDENCY_FILE)

include $(DEPENDENCY_FILE)
