#
# $Id: Makefile.in,v 1.23 2006/11/28 13:35:37 jpr5 Exp $
#
# Copyright (c) 2006  Jordan Ritter <jpr5@darkridge.com>
#
# Please refer to the LICENSE file for more information.

CC=@CC@

CFLAGS_LIBNET := $(shell libnet-config --defines ; libnet-config --cflags)
CFLAGS=@CFLAGS@ -D@OS@ @DEFS@ @EXTRA_DEFINES@  $(CFLAGS_LIBNET)
INCLUDES=-I@srcdir@ @PCAP_INCLUDE@ @EXTRA_INCLUDES@

LDFLAGS=@LDFLAGS@ @PCAP_LINK@
LIBNET := $(shell libnet-config --libs)
LIBS=-lpcap @EXTRA_LIBS@ $(LIBNET)

STRIPFLAG=@STRIPFLAG@

SRC=ngrep.c tcpkill.c
OBJS=ngrep.o tcpkill.o
TARGET=ngrep
MANPAGE=ngrep.8

prefix      = @prefix@
exec_prefix = @exec_prefix@

bindir      = $(prefix)/bin
datadir     = $(prefix)/share
mandir      = $(datadir)/man

BINDIR_INSTALL = $(prefix)/bin
MANDIR_INSTALL = $(mandir)/man8

INSTALL = ./install-sh

REGEX_DIR=@REGEX_DIR@
REGEX_OBJS=@REGEX_OBJS@


all: $(TARGET) 

$(TARGET): $(REGEX_OBJS) $(OBJS) 
	$(CC) $(CFLAGS) $(LDFLAGS) $(STRIPFLAG) -o $(TARGET) $(OBJS) $(REGEX_OBJS) $(LIBS)

debug: $(REGEX_OBJS) $(OBJS)
	$(CC) $(CFLAGS) $(LDFLAGS) -g -o $(TARGET) $(OBJS) $(REGEX_OBJS) $(LIBS) 

static: $(REGEX_OBJS) $(OBJS)
	$(CC) $(CFLAGS) $(LDFLAGS) $(STRIPFLAG) -o $(TARGET).static -static $(OBJS) $(REGEX_OBJS) $(LIBS)

install: $(TARGET)
	$(INSTALL) -c -m 0755 $(TARGET)  $(DESTDIR)/$(BINDIR_INSTALL)/$(TARGET)
	$(INSTALL) -c -m 0644 $(MANPAGE) $(DESTDIR)/$(MANDIR_INSTALL)/$(MANPAGE)

.c.o:	
	$(CC) $(CFLAGS) $(INCLUDES) -g -c $<

clean:
	make -C $(REGEX_DIR) clean
	rm -f *~ $(OBJS) $(REGEX_OBJS) $(TARGET) $(TARGET).static

distclean: clean
	make -C $(REGEX_DIR) distclean
	rm -f config.status config.cache config.log config.h Makefile 

$(REGEX_OBJS): $(REGEX_OBJS:.o=.c) $(REGEX_DIR)/*.h
	$(MAKE) $(MAKEFLAGS) -C $(REGEX_DIR) $(notdir $(REGEX_OBJS))

$(OBJS): Makefile ngrep.c ngrep.h

tardist:
	@( VERSION=`perl -ne '/VERSION\s+"(.*)"/ && print "$$1\n"' ngrep.h` ; \
	   PKG="ngrep-$$VERSION"                                            ; \
	   TMPDIR="/tmp"                                                    ; \
	   DESTDIR="$$TMPDIR/$$PKG"                                         ; \
	   echo                                                             ; \
	   echo "Building package $$PKG ... "                               ; \
	   echo                                                             ; \
	   sleep 2                                                          ; \
	   rm -rf $$DESTDIR && mkdir $$DESTDIR                             && \
	   make distclean                                                  && \
	   tar cf - . --exclude "CVS" | tar xf - -C $$DESTDIR              && \
	   find $$DESTDIR -name "*~" -o -name ".*#*" | xargs rm -f         && \
	   cd $$TMPDIR && tar jcf $$PKG.tar.bz2 $$PKG                       ; \
           rm -rf $$DESTDIR                                                 ; \
           cd $$TMPDIR && gpg -ba $$PKG.tar.bz2                             ; \
	   echo                                                             ; \
	   ls -l $$TMPDIR/$$PKG.tar.bz2 $$TMPDIR/$$PKG.tar.bz2.asc          ; \
	   echo                                                             ; \
	)
