#!/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

include /usr/share/dpkg/pkg-info.mk

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 $(distrelease),wheezy))
export DEB_LDFLAGS_MAINT_APPEND = -Wl,-Bsymbolic
endif

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

export docdir = /usr/share/doc/wireshark-doc
%:
	dh $@ --with python2 --buildsystem cmake --parallel

override_dh_auto_configure-arch:
	test -f version.conf.bak || \
		(mv version.conf version.conf.bak && echo "git_description: Git v"$(DEB_VERSION_UPSTREAM)" packaged as "$(DEB_VERSION) > version.conf)
	dh_auto_configure -- -DBUILD_wireshark_gtk=ON \
		-DCMAKE_INSTALL_LIBDIR=/usr/lib/$(DEB_HOST_MULTIARCH) \
	# -DENABLE_QT5=OFF

override_dh_auto_configure-indep:
	test -f version.conf.bak || \
		(mv version.conf version.conf.bak && echo "git_description: Git v"$(DEB_VERSION_UPSTREAM)" packaged as "$(DEB_VERSION) > version.conf)
	dh_auto_configure -- -DBUILD_wireshark_gtk=ON -DBUILD_xxx2deb=ON \
		-DCMAKE_INSTALL_LIBDIR=/usr/lib/$(DEB_HOST_MULTIARCH) \
	# -DENABLE_QT5=OFF

override_dh_auto_build-arch:
	# regenerate ASN.1 dissectors
	$(MAKE) -C $(CURDIR)/obj-* asn1
	dh_auto_build

override_dh_auto_build-indep:
	$(MAKE) -C $(CURDIR)/obj-* asn1
	dh_auto_build
ifeq (,$(filter $(distrelease),trusty))
	$(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
else
	# skip building documentation, asciidoctor is too old
	(cd obj-* && mkdir wsdg_html_chunked wsug_html_chunked)
endif

override_dh_strip:
	dh_strip --ddeb-migration="wireshark-dbg (<< 2.0.1+g59ea380-12.0.1+g59ea380-2~)" || dh_strip

override_dh_auto_install-arch:
	dh_auto_install
	rm -f debian/*.shlibs
	mkdir -p $(CURDIR)/debian/tmp/etc/wireshark/

override_dh_auto_install-indep:
	dh_auto_install
	rm -rf $(CURDIR)/debian/tmp/usr/share/wireshark/COPYING
	cp debian/license-text-about-dialog $(CURDIR)/debian/tmp/usr/share/wireshark/ABOUT.GPL
	mkdir -p $(CURDIR)/debian/tmp/etc/wireshark/
	mv $(CURDIR)/debian/tmp/usr/share/wireshark/init.lua \
		$(CURDIR)/debian/tmp/etc/wireshark/
	ln -s /etc/wireshark/init.lua \
		$(CURDIR)/debian/tmp/usr/share/wireshark/init.lua

override_dh_install-arch:
	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-arch:
	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:
	$(MAKE) -C obj-* test-programs
	-dh_auto_test

override_dh_clean:
	test ! -f version.conf.bak || mv version.conf.bak version.conf
	dh_clean
