#!/usr/bin/make -f
# Originally made with the aid of dh_make, by Craig Small
# Sample debian/rules that uses debhelper. GNU copyright 1997 by Joey Hess.
# Some lines taken from debmake, by Cristoph Lameter.
# Rewritten to use dh, by Balint Reczey

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

distrelease  := $(shell lsb_release -cs)
ifeq ($(distrelease),n/a)
  distrelease     := sid
endif

# This has to be exported to make some magic below work.
export DH_OPTIONS

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	MAKEFLAGS += -j$(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
endif

ifeq ($(MAKE),ninja)
	DH_BUILDSYSTEM = cmake+ninja
else
	DH_BUILDSYSTEM = cmake
endif

%:
	dh $@ --with python3 --buildsystem $(DH_BUILDSYSTEM) --with quilt

override_dh_auto_configure:
	dh_auto_configure -- -DBUILD_xxx2deb=ON -DBUILD_corbaidl2wrs=ON -DUSE_qt6=OFF

override_dh_auto_build:
	# Ignore warnings from asn2wrs.py about duplicate field names.
	PYTHONWARNINGS='ignore:The same:UserWarning::0' \
	$(MAKE) -C $(CURDIR)/obj-* asn1
	dh_auto_build
	$(MAKE) -C $(CURDIR)/obj-* user_guide_html developer_guide_html
	# fix links in documentation
	sed -i "s|$(CURDIR)/docbook|..|" obj-*/docbook/ws*g_html_chunked/*.html
ifeq (,$(filter $(DEB_BUILD_OPTIONS),nocheck))
	# Required for the "unittests" suite.
	$(MAKE) -C $(CURDIR)/obj-* test-programs
endif

override_dh_dwz:
	# run dh_dwz only with debhelper (>= 12.6) to work around https://bugs.debian.org/939164
	dpkg -l debhelper | awk '/debhelper/ {print $$3}' | xargs dpkg --compare-versions 12.6 gt || dh_dwz

override_dh_strip:
	dh_strip --dbg-package=wireshark-dbg

override_dh_auto_install:
	dh_auto_install
ifeq ($(MAKE),ninja)
	DESTDIR=$(CURDIR)/debian/tmp $(MAKE) -C $(CURDIR)/obj-* install-headers
else
	$(MAKE) DESTDIR=$(CURDIR)/debian/tmp -C $(CURDIR)/obj-* install-headers
endif
	rm -f debian/*.shlibs
	rm -rf $(CURDIR)/debian/tmp/usr/share/wireshark/COPYING

override_dh_install:
	dh_install
	# check all necessary headers are included
	$(CC) -c debian/headers-check.c $(shell pkg-config --cflags glib-2.0) $(shell dpkg-buildflags --get CPPFLAGS) $(shell dpkg-buildflags --get CFLAGS) -Idebian/libwireshark-dev/usr/include -Idebian/libwireshark-dev/usr/include/wireshark -Idebian/libwiretap-dev/usr/include/wireshark -Idebian/libwsutil-dev/usr/include/wireshark -o /dev/null

override_dh_fixperms:
	dh_fixperms
	chmod 644 debian/wireshark-dev/usr/share/pyshared/make-plugin-reg.py \
		debian/wireshark-dev/usr/share/pyshared/wireshark_be.py \
		debian/wireshark-dev/usr/share/pyshared/wireshark_gen.py

override_dh_auto_test:
ifeq (,$(filter $(DEB_BUILD_OPTIONS),nocheck))
	# XXX Add -- --verbose?
	dh_auto_test
else
	@echo '"DEB_BUILD_OPTIONS" has "nocheck". Skipping tests'
endif
