#!/usr/bin/make -f
#export DH_VERBOSE = 1

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_LDFLAGS_MAINT_APPEND = -Wl,--no-undefined

# config.mk is provided by gnustep-make package
include /usr/share/GNUstep/debian/config.mk

# Install in debian/tmp
export DESTDIR=$(CURDIR)/debian/tmp

##### VERSION NUMBERS ###########################################

# GNUstep Base version :
v_base  := 1.30.0-3

# GNUstep Gui/Back version :
v_gui   := $(shell . ./Version; echo $$VERSION)

# GNUstep Gui/Back SONAME version :
sov_gui := $(shell . ./Version; echo $$GNUSTEP_GUI_MAJOR_VERSION.$$GNUSTEP_GUI_MINOR_VERSION)

#### PACKAGES NAMES ###################################
p_com           = gnustep-gui-common
p_run           = gnustep-gui-runtime
p_lib           = libgnustep-gui$(sov_gui)
p_dev           = libgnustep-gui-dev
p_doc           = gnustep-gui-doc
#######################################################

%:
	dh $@

# Ensure that debian/control and debian/tests/control are always
# up-to-date.
override_dh_testdir:
	m4 \
	    -DV_BASE='$(v_base)' \
	    -DV_GUI='$(v_gui)' \
	    -DSOV_GUI='$(sov_gui)' \
	    debian/templates/control.m4 > debian/control
	m4 -DSOV_GUI='$(sov_gui)' debian/templates/tests-control.m4 \
	  > debian/tests/control
	dh_testdir

override_dh_auto_configure:
# Generate the lintian overrides file automatically, to avoid renames
# when the SONAME is bumped.
	m4 -DSOV_GUI='$(sov_gui)' \
	  debian/templates/libgnustep-guiN.overrides.m4 \
	  > debian/libgnustep-gui$(sov_gui).lintian-overrides
# If "nodoc" is present in DEB_BUILD_OPTIONS, the -doc package is
# created nearly empty.  If it is also in DEB_BUILD_PROFILES,
# gnustep-gui-doc is not built at all.  Manpages shipped in the
# -common package are also not installed if "nodoc" is set.
ifndef docs
	sed -i '$$d' debian/$(p_com).install
else
	cp debian/templates/$(p_doc).install.in debian/$(p_doc).install
	cp debian/templates/$(p_doc).links.in debian/$(p_doc).links
endif
	cp debian/templates/libgnustep-guiN.install.in debian/$(p_lib).install
	cp debian/templates/libgnustep-guiN.docs.in debian/$(p_lib).docs

	dh_auto_configure -- \
	  --disable-icu-config \
	  --enable-imagemagick

override_dh_auto_build-arch:
# build shared libs
	dh_auto_build -- \
	    $(verbose) \
	    $(optim) \
	    doc=no \
	    $(shell dpkg-buildflags --export=cmdline)

# build dev/static lib
	dh_auto_build -- \
	    -C Source \
	    $(verbose) \
	    doc=no \
	    shared=no

override_dh_auto_build-indep:
# build shared libs
	dh_auto_build -- \
	    $(verbose) \
	    doc=no

ifdef docs
# build main doc. Make sure autogsdoc can find the libraries.
	dh_auto_build -- \
	    -C Documentation \
	    $(verbose) \
	    $(docs)
endif

override_dh_auto_test:
	dh_auto_test || (cat Tests/tests.log; exit 1)

override_dh_auto_install-arch:
# install shared version of libraries
	dh_auto_install -- \
	    doc=no

# install dev/static lib
	dh_auto_install -- \
	    -C Source \
	    doc=no \
	    shared=no

override_dh_auto_install-indep:
# install shared version of libraries
	dh_auto_install -- \
	    doc=no

ifdef docs
# install doc
	dh_auto_install -- \
	    -C Documentation \
	    $(docs)
# Delete duplicate files already shipped at /usr/share/doc/.
	$(RM) -r debian/tmp$(GNUSTEP_SYSTEM_DOC)/Developer/Gui/ReleaseNotes/0.*
endif

override_dh_auto_clean-indep:
	dh_auto_clean -- \
	    doc=yes

# Doc symlinks
override_dh_installdocs:
	dh_installdocs -p$(p_run) --link-doc=$(p_lib)
	dh_installdocs -p$(p_dev) --link-doc=$(p_lib)
	dh_installdocs --remaining-packages

execute_before_dh_link:
	gsdh_gnustep --app --bundle-dir=ColorPickers --move-to=$(p_com)

override_dh_shlibdeps:
# First calculate shlib dependencies of all -gui packages
	dh_shlibdeps

# Then recalculate dependencies of shared lib
	dh_makeshlibs -p$(p_lib) \
	    -V '$(p_lib) (>= $(v_gui)), gnustep-gui-runtime (>= $(v_gui)), gnustep-back$(sov_gui)'
