#!/usr/bin/make -f
#
# Copyright (C) 2008 Francesco Paolo Lovergine <frankie@debian.org>
# Released under GPL. See /usr/share/common-licenses/GPL for
# more information about the license.
#

# Enable hardening build flags
export DEB_BUILD_MAINT_OPTIONS=hardening=+all

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

UPSTREAM_VERSION = $(shell echo $(DEB_VERSION_UPSTREAM) | sed -e 's/\+.*//')

DEB_BUILD_ARCH ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH)

export DEB_CPPFLAGS_MAINT_APPEND = -I/usr/include/tirpc/
export DEB_LDFLAGS_MAINT_APPEND = -ltirpc

ifneq (,$(findstring verbose,$(DEB_BUILD_OPTIONS)))
   DH_VERBOSE=1
   export DH_VERBOSE
endif

%:
	dh $@

override_dh_auto_configure:
	# configure the netcdf compatible flavor, without fortran functions.
	dh_auto_configure -- --includedir=/usr/include/hdf \
	                     --enable-shared \
	                     --disable-fortran \
	                     --disable-netcdf \
	                     --with-szlib=yes

override_dh_auto_test:
ifneq (,$(filter $(DEB_BUILD_ARCH),ppc64el s390x sparc64))
	dh_auto_test --no-parallel || echo "Ignoring test failures"
else
	dh_auto_test --no-parallel
endif

execute_after_dh_auto_install:
	# strip rpath from binaries
	-find $(CURDIR)/debian/tmp/usr/bin -type f -not -name h4cc -not -name h4redeploy -exec chrpath --delete {} \;

	# strip rpath from libraries
	-find $(CURDIR)/debian/tmp/usr/lib -name "*.so*" -type f -exec chrpath --delete {} \;

	# rename programs also provided by netcdf-bin
	mv $(CURDIR)/debian/tmp/usr/bin/ncdump $(CURDIR)/debian/tmp/usr/bin/ncdump-hdf
	mv $(CURDIR)/debian/tmp/usr/bin/ncgen $(CURDIR)/debian/tmp/usr/bin/ncgen-hdf

	# rename manuals also provided by netcdf-bin
	mv $(CURDIR)/debian/tmp/usr/share/man/man1/ncdump.1 $(CURDIR)/debian/tmp/usr/share/man/man1/ncdump-hdf.1
	sed -i -e 's/ncdump/ncdump-hdf/g' $(CURDIR)/debian/tmp/usr/share/man/man1/ncdump-hdf.1

	mv $(CURDIR)/debian/tmp/usr/share/man/man1/ncgen.1 $(CURDIR)/debian/tmp/usr/share/man/man1/ncgen-hdf.1
	sed -i -e 's/ncgen/ncgen-hdf/g' $(CURDIR)/debian/tmp/usr/share/man/man1/ncgen-hdf.1

execute_after_dh_installman:
	$(RM) $(CURDIR)/debian/hdf4-tools/usr/share/man/man1/hdf.1

override_dh_makeshlibs:
	dh_makeshlibs -- -v$(UPSTREAM_VERSION)
