VERSION = 0.2
RELEASE = 1

# If your kernel includes do not point to your 2.4 source tree you need
# to tell us where the kernel tree is.
KERNEL_DIR = /usr/src/linux4/linux

# If you did not install libipq when you installed netfilter you need to tell
# us where your netfilter sources are.
NF_DIR = /usr/src/linux4/netfilter

# remark the next line to remove mail capability
MAILDEF = -DMAIL

ifdef MAILDEF
# max number of packets to queue before mailing packet dumps (runtime flag)
MAILDEF += -DMAX_Q=30
# max seconds to wait before mailing (time from first packet arrival)
MAILDEF += -DQ_TIME=120
endif

# default number of times to rotate dump file (runtime flag)
DEFS += -DMAX_ROTATE=10
# NF_DROP || NF_ACCEPT for default verdict (runtime flag)
DEFS += -DPOLICY=NF_DROP

# DLT_RAW is defined as 101 on some versions of libpcap. If your decoder was
# built with this definition you may need to change below.
DEFS += -DDLT_RAW=12

ifeq ($(shell [ -f /usr/include/linux/netfilter.h ] || echo NO), NO)
CFLAGS += -I$(KERNEL_DIR)/include
endif

ifeq ($(shell [ -f /usr/local/include/libipq.h ] || echo NO), NO)
CFLAGS += -I$(NF_DIR)/userspace/include/libipq
LDLIBS += -L$(NF_DIR)/userspace/libipq
endif

DEFS += -DVERSION=\"$(VERSION)-$(RELEASE)\"
APP = pdumpq
MAN = pdumpq.8
SRCDIR = .
LIBIPQD = /usr/local
CC  = gcc -O2
CFLAGS += -I$(SRCDIR) -I$(LIBIPQD)/include $(DEFS)
LDLIBS += -L$(SRCDIR) -L$(LIBIPQD)/lib -lipq
INSTALL = install -c -m 0550
INSTALL_MAN = install -c -m 0644
INIT = /etc/rc.d/init.d
instbase = /usr/local
instbin = /sbin
instman = /man/man8

HDR := app.h pcap-ish.h
SRC := main.c cmdl.c func.c pcap-ish.c
OBJ := main.o cmdl.o func.o pcap-ish.o
ifdef MAILDEF
DEFS += $(MAILDEF)
HDR += mail.h
SRC += mail.c
OBJ += mail.o
endif

all:	$(APP)

$(APP):	$(OBJ)
	$(CC) -s -o $@ $(OBJ) $(LDLIBS)

$(OBJ):	$(HDR) $(SRC)

install:
	$(INSTALL) $(APP) $(instbase)$(instbin)
	$(INSTALL) $(APP).init $(INIT)/$(APP) || echo "Sys-V init script not successfully installed."
	$(INSTALL_MAN) $(MAN) $(instbase)$(instman)
clean:
	rm -f $(OBJ) $(APP) core *~ *.tgz *.rpm

oclean:
	rm -f $(OBJ) core

indent:
	indent *.c
	rm -f *.c~

rpm:
	@vim Makefile
	@echo -n Making RPM packages ...
	@make -s
	@echo -n .
	@make -s oclean
	@echo -n .
	@make -s indent
	@echo -n .
	@rm -f tags
	@echo -n .
	@rm -f *.tgz *.rpm
	@echo -n .
	@echo $(instbase)$(instbin)/$(APP) > FILES
	@echo -n .
	@echo $(instbase)$(instman)/$(MAN) >> FILES
	@echo -n .
	@echo $(INIT)/$(APP) >> FILES
	@echo -n .
	@echo "%define version $(VERSION)" > /usr/src/redhat/SPECS/pdumpq.spec
	@echo -n .
	@echo "%define release $(RELEASE)" >> /usr/src/redhat/SPECS/pdumpq.spec
	@echo -n .
	@cat pdumpq.spec >> /usr/src/redhat/SPECS/pdumpq.spec
	@echo -n .
	@cd ..; \
	cp -R pdumpq pdumpq-$(VERSION)-$(RELEASE); \
	tar -c --exclude pdumpq-$(VERSION)-$(RELEASE)/CVS -zf pdumpq-$(VERSION)-$(RELEASE).tgz pdumpq-$(VERSION)-$(RELEASE); \
	mv -f pdumpq-$(VERSION)-$(RELEASE).tgz /usr/src/redhat/SOURCES; \
	rm -Rf pdumpq-$(VERSION)-$(RELEASE)
	@echo -n .
	@rm -f FILES
	@echo -n .
	@rpm --quiet -ba /usr/src/redhat/SPECS/pdumpq.spec
	@echo -n .
	@rm -f /usr/src/redhat/SPECS/pdumpq.spec
	@echo -n .
	@rm -Rf /usr/src/redhat/BUILD/pdumpq-$(VERSION)-$(RELEASE)
	@echo -n .
	@mv /usr/src/redhat/SOURCES/pdumpq-$(VERSION)-$(RELEASE).tgz .
	@echo -n .
	@mv /usr/src/redhat/RPMS/i386/pdumpq-$(VERSION)-$(RELEASE).i386.rpm .
	@echo -n .
	@mv /usr/src/redhat/SRPMS/pdumpq-$(VERSION)-$(RELEASE).src.rpm .
	@echo -n .
	@chgrp cvs *.rpm *.tgz pdumpq
	@echo -n .
	@chmod g+rw *.rpm *.tgz pdumpq
	@echo "Done!"

