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

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

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

# Objective-C version :
v_objc := 4:7.2

# GNUstep Make version :
v_make	:= 2.7.0-4

# GNUstep Base version :
v_base	:= $(shell . ./Version; echo $$VERSION)

# GNUstep Base SONAME version :
sov_base := $(shell . ./Version; echo $$MAJOR_VERSION.$$MINOR_VERSION)

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

### DIRS ##############################################
GS_DTDs_DEBIAN = usr/share/xml/gnustep
GS_BASE_RESOURCES_UPSTREAM = $(GNUSTEP_SYSTEM_LIBRARY)/Libraries/gnustep-base/Versions/$(sov_base)/Resources
GS_BASE_RESOURCES_DEBIAN = usr/share/GNUstep/Libraries/gnustep-base/Versions/$(sov_base)/Resources

#####################################################"#

%:
	dh $@

# Ensure that debian/control is always up-to-date.
override_dh_testdir:
	m4 \
	    -DV_OBJC='$(v_objc)' \
	    -DV_MAKE='$(v_make)' \
	    -DSOV_BASE='$(sov_base)' \
	    debian/templates/control.m4 > debian/control
	dh_testdir

override_dh_auto_configure:
# generate libgnustep-baseN lintian overrides file
	m4 \
	    -DSOV_BASE='$(sov_base)' \
	    debian/templates/libgnustep-baseN.lintian-overrides.m4 > debian/$(p_lib).lintian-overrides

# generate maintainer scripts
	b=$(p_run); \
	for ext in preinst postinst prerm postrm; do \
	    if [ -f debian/templates/$$b.$$ext.in ]; then \
		sed -e 's,@GDOMAP@,$(GNUSTEP_SYSTEM_TOOLS)/gdomap,g' \
		    debian/templates/$$b.$$ext.in > debian/$$b.$$ext; \
	    fi; \
	done

# generate gdomap.init file
		sed -e 's,@GDOMAP@,$(GNUSTEP_SYSTEM_TOOLS)/gdomap,g' \
		    -e 's,@GSMAKEFILES@,$(GNUSTEP_MAKEFILES),g' \
		    debian/templates/$(p_run).gdomap.init.in > debian/$(p_run).gdomap.init;

# generate gnustep-base-runtime.install file
	sed -e 's,@GS_TOOLS@,$(GNUSTEP_SYSTEM_TOOLS),g' \
	    debian/templates/$(p_run).install.in > debian/$(p_run).install; \
		chmod a+x debian/$(p_run).install

# generate gnustep-base-runtime.links file
	sed -e 's,@GS_MANPAGES@,$(GNUSTEP_SYSTEM_DOC_MAN),g' \
	    debian/templates/$(p_run).links.in > debian/$(p_run).links;

# generate gnustep-base-common.install file
	sed -e 's,@GS_BASE_RESOURCES_UPSTREAM@,$(GS_BASE_RESOURCES_UPSTREAM),g' \
	    -e 's,@GS_BASE_RESOURCES_DEBIAN@,$(GS_BASE_RESOURCES_DEBIAN),g' \
	    debian/templates/$(p_com).install.in > debian/$(p_com).install

# generate gnustep-base-common.links file
	sed -e 's,@GS_BASE_RESOURCES_DEBIAN@,$(GS_BASE_RESOURCES_DEBIAN),g' \
	    debian/templates/$(p_com).links.in > debian/$(p_com).links

# generate gnustep-base-common.xmlcatalogs file
	sed -e 's,@GS_DTDs_DEBIAN@,/$(GS_DTDs_DEBIAN),g' \
	    debian/templates/$(p_com).xmlcatalogs.in > debian/$(p_com).xmlcatalogs

# generate libgnustep-baseN.install file
	sed -e 's,@GS_LIBS@,$(GNUSTEP_SYSTEM_LIBRARIES),g' \
	    debian/templates/libgnustep-baseN.install.in > debian/$(p_lib).install

# generate libgnustep-base-dev.install file
	sed -e 's,@GS_HEADERS@,$(GNUSTEP_SYSTEM_HEADERS),g' \
	    -e 's,@GS_LIBS@,$(GNUSTEP_SYSTEM_LIBRARIES),g' \
	    -e 's,@GS_MAKEFILES@,$(GNUSTEP_MAKEFILES),g' \
	    debian/templates/$(p_dev).install.in > debian/$(p_dev).install

# generate gnustep-base-doc.install file
	sed -e 's,@GS_DOC@,$(GNUSTEP_SYSTEM_DOC),g' \
	    -e 's,@GS_INFO@,$(GNUSTEP_SYSTEM_DOC_INFO),g' \
	    debian/templates/$(p_doc).install.in > debian/$(p_doc).install; \
		chmod a+x debian/$(p_doc).install

# Override the test for libkvm to ensure that /proc is used on
# GNU/kFreeBSD even if libkvm-dev is installed (#593898).
	dh_auto_configure -- \
	    ac_cv_lib_kvm_kvm_getenvv=no \
	    --disable-bfd  || (cat config.log; exit 1)

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) \
	    $(optim) \
	    doc=no \
	    shared=no

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

# build main doc. Make sure autogsdoc can find the libraries.
	LD_LIBRARY_PATH=$(CURDIR)/Source/obj:$$LD_LIBRARY_PATH \
	dh_auto_build -- \
	    -C Documentation \
	    $(verbose) \
	    $(optim) \
	    doc=yes

override_dh_auto_test:
	dh_auto_test -- \
	    $(verbose)  || (cat Tests/tests.log; exit 0)
# Print the results from a failing test on armhf; see d/p/armhf-test.patch.
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
ifeq ($(DEB_BUILD_ARCH),armhf)
	-cat Tests/base/NSInvocation/armhf.log
endif
endif

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

# install doc
	dh_auto_install -- \
	    -C Documentation \
	    doc=yes
# Delete duplicate files already shipped at /usr/share/doc/.
	$(RM) -r \
	  debian/tmp$(GNUSTEP_SYSTEM_DOC)/Developer/Base/ReleaseNotes/1.*
# Delete README which triggers lintian I: gnustep-base-common:
# package-contains-documentation-outside-usr-share-doc.  These
# instructions are unnecessary as the Debian package uses tzdata.
	$(RM) debian/tmp/usr/lib/GNUstep/Libraries/gnustep-base/Versions/$(sov_base)/Resources/NSTimeZones/README

override_dh_auto_clean-indep:
	dh_auto_clean -- \
	    doc=yes


override_dh_installcatalogs-indep:
# Bug #637093
	dh_installxmlcatalogs -p$(p_com) -n
	dh_installcatalogs --remaining-packages

# Install the gdomap init script
override_dh_installinit:
	dh_installinit -p$(p_run) --name=gdomap --no-enable --no-start

# Install examples
override_dh_installexamples:
	dh_installexamples -p$(p_doc) -X.cvsignore -X.gdbinit -X.arch-ids
	dh_installexamples --remaining-packages

override_dh_shlibdeps:
	dh_shlibdeps
# Recalculate dependencies of the shared library to ensure that
# gnustep-base-runtime is added as additional dependency.
	dh_makeshlibs -p$(p_lib) \
	  -V '$(p_lib) (>= $(v_base)), $(p_run) (>= $(v_base))'
