# Extract the version string from nping.h.
export NPING_VERSION := $(shell grep '^\#[ \t]*define[ \t]\+NPING_VERSION' nping.h | sed -e 's/.*"\(.*\)".*/\1/' -e 'q')
NPING_NAME= Nping
NPING_URL= https://nmap.org/nping
NPING_PLATFORM=@host@
prefix = @prefix@
datarootdir = @datarootdir@
exec_prefix = @exec_prefix@
bindir = @bindir@
sbindir = @sbindir@
mandir = @mandir@
srcdir = @srcdir@
npingdatadir = @datadir@/nping
NPINGDEVDIR=~/nping-private-dev
WIN32STUFFDIR=mswin32
NPINGWEBDIR=../../../web/nmap/nping
NPINGWININSTDIR=mswin32/nsis
export RELEASE_DATE := $(shell date +"%b %d, %Y")


export NBASEDIR=../nbase
export NSOCKDIR=../nsock

export NDIR=$(shell pwd)


CC = @CC@
CXX = @CXX@
CCOPT =
DBGFLAGS =
STRIP = @STRIP@
LIBPCAPDIR = @libpcapdir@
export LIBDNETDIR = @LIBDNETDIR@


DEFS = @DEFS@ -DNPING_NAME=\"$(NPING_NAME)\" -DNPING_URL=\"$(NPING_URL)\" -DNPING_PLATFORM=\"$(NPING_PLATFORM)\"
# With GCC, add extra security checks to source code.
# http://gcc.gnu.org/ml/gcc-patches/2004-09/msg02055.html
# Level 1 only makes changes that don't affect "conforming" programs,
# while level 2 enforces additional restrictions.
DEFS += -D_FORTIFY_SOURCE=2
# For mtrace debugging -- see MTRACE define in main.cc for instructions
# Should only be enabled during debugging and not in any real release.
# DEFS += -DMTRACE=1
CXXFLAGS = @CXXFLAGS@ $(DBGFLAGS) $(CCOPT) $(DEFS)
CPPFLAGS = @CPPFLAGS@ -I$(NBASEDIR) -I$(NSOCKDIR)/include
export CFLAGS = $(CXXFLAGS)
# CFLAGS = $(DEFS) $(INCLS)
STATIC =
LDFLAGS = @LDFLAGS@ $(DBGFLAGS) $(STATIC)
OPENSSL_LIBS = @OPENSSL_LIBS@
LIBS =  $(NSOCKDIR)/src/libnsock.a ../libnetutil/libnetutil.a $(NBASEDIR)/libnbase.a $(OPENSSL_LIBS) @LIBPCAP_LIBS@ @LIBDNET_LIBS@ @LIBS@
# LIBS =  -lefence @LIBS@
# LIBS =  -lrmalloc @LIBS@
INSTALL = @INSTALL@
# MAKEDEPEND = @MAKEDEPEND@
export RPMTDIR=$(HOME)/rpm


# DESTDIR is used by some package maintainers to install Nping under
# its usual directory structure into a different tree.  See the
# CHANGELOG for more info.
DESTDIR =

TARGET = nping


export SRCS = ArgParser.cc common_modified.cc nping.cc NpingOps.cc utils.cc utils_net.cc output.cc stats.cc NpingTargets.cc NpingTarget.cc EchoHeader.cc EchoServer.cc EchoClient.cc ProbeMode.cc NEPContext.cc Crypto.cc

export HDRS = ArgParser.h nping_config.h common_modified.h nping.h NpingOps.h output.h utils.h utils_net.h stats.h NpingTargets.h NpingTarget.h EchoHeader.h EchoServer.h EchoClient.h ProbeMode.h NEPContext.h Crypto.h

OBJS = ArgParser.o common_modified.o nping.o NpingOps.o utils.o utils_net.o output.o stats.o NpingTargets.o NpingTarget.o EchoHeader.o EchoServer.o EchoClient.o ProbeMode.o NEPContext.o Crypto.o

export DOCS2DIST = leet-nping-ascii-art.txt nping.1 nping-man.html

export MISC2DIST = config.guess config.sub configure configure.ac Makefile.in TODO nping_config.h.in CHANGELOG LICENSE


# %.o : %.cc -- nope this is a GNU extension
.cc.o:
	$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $< -o $@

all:
	$(MAKE) $(TARGET)

$(TARGET): $(NBASEDIR)/libnbase.a $(NSOCKDIR)/src/libnsock.a ../libnetutil/libnetutil.a $(OBJS)
	@echo Compiling nping
	rm -f $@
	$(CXX) $(LDFLAGS) -o $@ $(OBJS) $(LIBS)
	@echo Nping compiled successfully!


# Make a statically compiled binary for portability between distributions
static:
	$(MAKE) STATIC=-static

debug:
	$(MAKE) DBGFLAGS="-O0 -g -pg -ftest-coverage -fprofile-arcs"

# CALL THIS FROM ONE COMPUTER AND CHECK IN RESULTS BEFORE DOING ANYONE
# DOES A MAKE RELEASE
prerelease:
	cd $(NPINGDEVDIR) && $(MAKE) prerelease

# Generate the Nping distribution tarball
generate-tarball:
	cd nping-priv && ./create_tarballs.sh

# Update the web site. WARNING: "make generate-tarball" must have been called first.
web:
	cd nping-priv && ./update_web.sh

clean:
	rm -f $(OBJS) $(TARGET)
# Who generates dependencies.mk? If it is generated by ./configure and
# not by make it should be moved to distclean
	rm -f dependencies.mk

debugclean:
	rm -f *.gcov *.gcda *.gcno gmon.out

distclean: clean
	rm -f Makefile Makefile.bak makefile.dep nping_config.h stamp-h \
	stamp-h.in config.cache config.log config.status

install-nping: $(TARGET)
	mkdir -p $(DESTDIR)$(bindir) $(DESTDIR)$(mandir)/man1
	$(INSTALL) -c -m 755 nping $(DESTDIR)$(bindir)/nping
# Use strip -x to avoid stripping dynamically loaded NSE functions. See
# http://seclists.org/nmap-dev/2007/q4/0272.html.
	$(STRIP) -x $(DESTDIR)$(bindir)/nping
	$(INSTALL) -c -m 644 docs/$(TARGET).1 $(DESTDIR)$(mandir)/man1/



install: install-nping
	@echo "NPING SUCCESSFULLY INSTALLED"

uninstall: uninstall-nping

uninstall-nping:
	rm -f $(DESTDIR)$(bindir)/$(TARGET)
	rm -f $(DESTDIR)$(mandir)/man1/$(TARGET).1

${srcdir}/configure: configure.ac
	cd ${srcdir} && autoconf

## autoheader might not change config.h.in, so touch a stamp file.
#${srcdir}/config.h.in: stamp-h.in
#${srcdir}/stamp-h.in: configure.ac acconfig.h \
#	config.h.top config.h.bot
#	cd ${srcdir} && autoheader
#	echo timestamp > ${srcdir}/stamp-h.in
#
#config.h: stamp-h
#stamp-h: config.h.in config.status
#	./config.status

Makefile: Makefile.in config.status
	./config.status

config.status: configure
	./config.status --recheck

makefile.dep:
	$(CXX) -MM $(CPPFLAGS) $(SRCS) > $@
-include makefile.dep
