#! /usr/bin/make -f

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

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

CFLAGS += $(CPPFLAGS)

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

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

BUILD_DATE=$(shell LC_ALL=C date -u "+%d %B %Y" -d "@$(SOURCE_DATE_EPOCH)")

MANPAGES:=$(wildcard debian/man/*.*.xml)

BUILDDIR = $(CURDIR)/debian/build

CMAKE_OPTS = -DCMAKE_BUILD_TYPE=RelWithDebInfo \
             -DCMAKE_C_FLAGS="-fstrict-aliasing $(CFLAGS)" \
             -DCMAKE_INSTALL_PREFIX=/usr \
             -DDCW_ROOT=/usr/share/gmt-dcw \
             -DGSHHG_ROOT=/usr/share/gmt-gshhg \
             -DNETCDF_ROOT=/usr \
             -DFFTW3_ROOT=/usr \
             -DGDAL_ROOT=/usr \
             -DPCRE_ROOT=/usr \
             -DGMT_INSTALL_MODULE_LINKS=off \
             -DGMT_INSTALL_TRADITIONAL_FOLDERNAMES=off \
             -DLICENSE_RESTRICTED=LGPL

%:
	dh $@ --buildsystem cmake \
	      --sourcedirectory=$(CURDIR) --builddirectory=$(BUILDDIR) \
	      --parallel

override_dh_clean:
	dh_clean debian/man/*.1

override_dh_auto_configure:
	dh_auto_configure -- $(CMAKE_OPTS)

override_dh_auto_build-arch:
	dh_auto_build -a

override_dh_auto_build-indep:
	$(MAKE) -C $(BUILDDIR) docs_man docs_html

override_dh_auto_install:
	# Create man pages from DocBook XML
	for x in $(MANPAGES) ; do \
	  docbook2x-man --string-param header-3="$(BUILD_DATE)" $$x ; \
	  mv `basename $$x | sed 's/.xml$$//'` `dirname $$x` ; \
	done

	dh_auto_install

	# Uncompress examples stuff
	#find  $(CURDIR)/debian/gmt-examples/usr/share/doc/gmt-examples/examples -name "*.gz" -exec gunzip {} \;
	# Remove +x
	#find  $(CURDIR)/debian/gmt-examples/usr/share/doc/gmt-examples/examples -name "*.bat" -exec chmod -x {} \;

	# Remove executable bit from shell includes & examples
	chmod -x debian/tmp/usr/share/gmt/tools/gmt_aliases.csh \
	         debian/tmp/usr/share/doc/gmt/examples/ex41/my_color.cpt \
	         debian/tmp/usr/share/doc/gmt/examples/ex41/my_data.txt \
	         debian/tmp/usr/share/doc/gmt/examples/ex41/my_symbol.def \
	         debian/tmp/usr/share/doc/gmt/examples/ex41/my_table.txt

	# Remove embedded sphinx copies of JS libraries in favor of packaged ones
	rm -f debian/tmp/usr/share/doc/gmt/html/_static/jquery.js \
	      debian/tmp/usr/share/doc/gmt/html/_static/underscore.js

	# Remove empty directory
	rm -rfv debian/tmp/usr/share/doc/gmt/html/_images/math/

	# Remove extra license files
	rm -rf debian/tmp/usr/share/doc/gmt/copying/

	# Fix bash-completion installation
	mkdir -p debian/tmp/usr/share/bash-completion/completions
	mv debian/tmp/usr/etc/bash_completion.d/gmt_completion.bash debian/tmp/usr/share/bash-completion/completions
	rm -rf debian/tmp/usr/etc/bash_completion.d
	rmdir debian/tmp/usr/etc/

	# Strip RPATH
	chrpath --delete \
	        debian/tmp/usr/lib/*/gmt/plugins/supplements.so \
	        debian/tmp/usr/lib/*/lib*.so.*.* \
	        debian/tmp/usr/bin/gmt

override_dh_install:
	dh_install --list-missing

override_dh_installman:
	dh_installman

	# Fix gmt_shell_functions.sh man page installation
	if [ -e "debian/gmt-common/usr/share/man/sh/man1/gmt_shell_functions.1gmt" ]; then \
		mv debian/gmt-common/usr/share/man/sh/man1/gmt_shell_functions.1gmt \
		   debian/gmt-common/usr/share/man/man1/gmt_shell_functions.sh.1gmt ; \
		rm -rf debian/gmt-common/usr/share/man/sh/ ; \
	fi

override_dh_compress:
	# Don't compress the pdf files needed for the gmt-doc-pdf package, nor the examples
	dh_compress -X.pdf -Xusr/share/doc/gmt-examples/examples/

override_dh_strip:
	dh_strip --dbgsym-migration='gmt-dbg (<< 5.4.2+dfsg-2~)'

override_dh_makeshlibs:
	dh_makeshlibs -- -v$(UPSTREAM_VERSION)
