CC=gcc
CXX=g++
BUILD_MINGW=
SRCHOME=../src
CFLAGS=-g -fPIC -DPIC -I$(SRCHOME)/include  -O2 -g -pipe -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fstack-protector --param=ssp-buffer-size=4 -fasynchronous-unwind-tables -I/usr/include/json-c 
LIBNDPI=$(SRCHOME)/lib/libndpi.a
LDFLAGS=$(LIBNDPI) -lpcap -lpthread  -lpthread -lm  -Wl,--as-needed -Wl,-z,relro -Wl,-O1 -Wl,--build-id -Wl,--enable-new-dtags
HEADERS=intrusion_detection.h reader_util.h $(SRCHOME)/include/ndpi_api.h \
        $(SRCHOME)/include/ndpi_typedefs.h $(SRCHOME)/include/ndpi_protocol_ids.h
OBJS=ndpiReader.o reader_util.o intrusion_detection.o
PREFIX?=/usr

ifneq ($(BUILD_MINGW),)
all:
	@echo 'Examples disabled due to mingw build.'

else

all: ndpiReader 

EXECUTABLE_SOURCES := ndpiReader.c ndpiSimpleIntegration.c
COMMON_SOURCES := $(filter-out $(EXECUTABLE_SOURCES),$(wildcard *.c ))

libndpiReader.a: $(COMMON_SOURCES:%.c=%.o) $(LIBNDPI)
	ar rsv libndpiReader.a $(COMMON_SOURCES:%.c=%.o)

ndpiReader: libndpiReader.a $(LIBNDPI) ndpiReader.o
	$(CC) $(CFLAGS) ndpiReader.o libndpiReader.a -o $@ $(LDFLAGS)

ndpiSimpleIntegration: ndpiSimpleIntegration.o
	$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)

%.o: %.c $(HEADERS) Makefile
	$(CC) $(CFLAGS) -c $< -o $@

install: ndpiReader
	mkdir -p $(DESTDIR)$(PREFIX)/bin/
	mkdir -p $(DESTDIR)$(PREFIX)/share/ndpi
	cp ndpiReader $(DESTDIR)$(PREFIX)/bin/
	cp protos.txt $(DESTDIR)$(PREFIX)/share/ndpi/ndpiProtos.txt
	cp mining_hosts.txt $(DESTDIR)$(PREFIX)/share/ndpi/ndpiCustomCategory.txt
	[ -f build/app/ndpiReader.dpdk ] && cp build/app/ndpiReader.dpdk $(DESTDIR)$(PREFIX)/bin/ || true
	[ -f ndpiReader.dpdk ] && cp ndpiReader.dpdk $(DESTDIR)$(PREFIX)/bin/ || true

dpdk:
	make -f Makefile.dpdk

check:
	 cppcheck --template='{file}:{line}:{severity}:{message}' --quiet --enable=all --force -I$(SRCHOME)/include *.c

clean:
	/bin/rm -f *.o ndpiReader ndpiReader.dpdk
	/bin/rm -f .*.dpdk.cmd .*.o.cmd *.dpdk.map .*.o.d
	/bin/rm -f _install _postbuild _postinstall _preinstall
	/bin/rm -rf build

distdir:
	cp categories.txt mining_hosts.txt protos.txt README.DPDK '$(distdir)/'
	find . -maxdepth 1 -type f -name '*.c' -o -name '*.h' -o -name '*.py' | xargs -I'{}' cp '{}' '$(distdir)/{}'

distclean: clean
	/bin/rm -f Makefile.dpdk
	/bin/rm -f Makefile

endif
