#!/usr/bin/make -f

DEB_HOST_ARCH_OS := $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)
DEB_HOST_MULTIARCH := $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
PCAP_VERSION := $(shell cat VERSION)

ifeq ($(DEB_HOST_ARCH_OS),linux)
export with_pcap=linux
endif
ifeq ($(DEB_HOST_ARCH_OS),kfreebsd)
export with_pcap=bpf
endif
ifeq ($(DEB_HOST_ARCH_OS),hurd)
export with_pcap=hurd
endif

%:
	dh $@ --with autoreconf

# configure's libnl V_INCLS accumulator ends up empty in the generated
# Makefile despite nl-3 detection succeeding (an upstream autoconf ordering
# quirk), and the generated Makefile's CPPFLAGS is a plain make variable
# that configure never substitutes a value into -- so the include path has
# to be injected at build time on the make command line, not via configure.
#
# It has to go into INCLS, not CPPFLAGS: the compile rule is
#
#     .c.o:
#             $(CC) $(FULL_CFLAGS) -c $(srcdir)/$*.c
#     FULL_CFLAGS = $(CCOPT) $(INCLS) $(DEFS) $(CFLAGS)
#
# so CPPFLAGS is never read and the -I never reached gcc:
#
#     ./pcap-linux.c:248:10: fatal error: netlink/genl/genl.h: No such file
#       or directory
#
# INCLS is "-I. @V_INCLS@", and V_INCLS is the accumulator that ends up empty,
# so restating -I. and adding the libnl3 path loses nothing. Overriding CFLAGS
# instead would discard the hardening flags dh passes in.
override_dh_auto_build:
	dh_auto_build -- INCLS="-I. -I/usr/include/libnl3"

# This 1.8.1-era libpcap predates pkg-config support entirely (only the
# pcap-config shell script existed then); modern meson-based consumers
# (DPDK 24.11, used by vyatta-dataplane) look libpcap up via pkg-config only
# and fail outright without a libpcap.pc: "Package libpcap was not found in
# the pkg-config search path." Generate one from the template rather than
# backporting pkg-config support into the actual build.
override_dh_auto_install:
	dh_auto_install
	mkdir -p debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig
	sed -e 's/@DEB_HOST_MULTIARCH@/$(DEB_HOST_MULTIARCH)/' \
	    -e 's/@VERSION@/$(PCAP_VERSION)/' \
	    debian/libpcap.pc.in \
	    > debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig/libpcap.pc

override_dh_install-arch:
	dh_install --fail-missing
	# pcap-filter man page goes into libpcap0.8 (#527599)
	rm debian/libpcap0.8-dev/usr/share/man/man7/pcap-filter.7

override_dh_strip-arch:
	dh_strip --dbg-package=libpcap0.8-dbg

override_dh_makeshlibs-arch:
	dh_makeshlibs -- -c4
