#!/usr/bin/make -f

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

export DEB_BUILD_MAINT_OPTIONS=hardening=+all
export DEB_CPPFLAGS_MAINT_APPEND = -DUNDEBUG
export DPKG_GENSYMBOLS_CHECK_LEVEL = 4
export COMPILED_BY=Debian

include ./version.mk
MAJOR=$(NETPBM_MAJOR_RELEASE)
MINOR=$(NETPBM_MINOR_RELEASE)
POINT=$(NETPBM_POINT_RELEASE)

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

%:
	dh $@ --no-parallel

override_dh_auto_configure:
	if [ `printf '%02d.%02d.%02d' $(NETPBM_MAJOR_RELEASE) $(NETPBM_MINOR_RELEASE) $(NETPBM_POINT_RELEASE)` != $(DEB_VERSION_UPSTREAM) ] ; then \
		echo debian infrastructure for version $(DEB_VERSION_UPSTREAM) used with upstream version $(NETPBM_MAJOR_RELEASE).$(NETPBM_MINOR_RELEASE).$(NETPBM_POINT_RELEASE) ; exit 1 ; fi
	cp config.mk.in config.mk
	cat debian/config.mk.debian >> config.mk
	echo "CFLAGS = `dpkg-buildflags --get CFLAGS`" >> config.mk
ifeq ($(DEB_HOST_ARCH),amd64)
	echo 'WANT_SSE = Y' >> config.mk
endif
	# Let blhc ignore build utilities using *_FOR_BUILD_*FLAGS
	@echo 'blhc: ignore-line-regexp: .* -o typegen\.o .*'
	@echo 'blhc: ignore-line-regexp: .* -o endiangen\.o .*'
	@echo 'blhc: ignore-line-regexp: .* -o libopt\.o .*'

override_dh_auto_build:
	dh_auto_build
# build the manpages
	mkdir netpbmdoc
	cd netpbmdoc; \
		$(MAKE) USERGUIDE=../userguide/ -f ../buildtools/manpage.mk manpages
	# Fix manual section
	sed -i 's/^\(\.TH "[^"]\+" \)0 /\11 /' netpbmdoc/*.1

override_dh_auto_clean:
	[ ! -f config.mk ] || $(MAKE) distclean
	rm -rf debian/netpbm-test debian/netpbm-test.bak
	rm -rf netpbmdoc

override_dh_auto_install:
	mkdir -p debian/tmp/
	$(MAKE) package PKGDIR=$(CURDIR)/debian/tmp/usr
# install the manpages
	cd netpbmdoc; \
		$(MAKE) USERGUIDE=../userguide/ -f ../buildtools/manpage.mk report
# install pkgconfig
	install -m 0755 -d debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig
	sed -e 's|@INCLUDEDIR@|/usr/include/netpbm|' \
		-e 's|@LINKDIR@|/usr/lib/$(DEB_HOST_MULTIARCH)|' \
		-e 's|@VERSION@|Netpbm $(MAJOR).$(MINOR).$(POINT)|' \
		debian/tmp/usr/pkgconfig_template > debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig/netpbm.pc
	mv debian/tmp/usr/lib/libnetpbm.so* \
		debian/tmp/usr/staticlink/libnetpbm.a \
		debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/
	ln -s libnetpbm.so.$(MAJOR) \
		debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libnetpbm.so
	find debian -name "lib*.la" -delete
	rm -rf debian/tmp/usr/sharedlink

execute_after_dh_installman:
	# tool manweb does not work due to missing /etc/manweb.conf
	find debian -name "manweb*" -delete

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	$(MAKE) check-tree RESULTDIR=$(CURDIR)/debian/netpbm-test
endif
