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

# Required version of the Objective-C compiler and runtime (GCC).
# If set to the empty string, no versioned dependency is generated.
v_objc :=
ifeq (,$(v_objc))
m4_objc := -DV_OBJC=''
else
m4_objc := -DV_OBJC=' (>= $(v_objc))'
endif

# GNUstep Make version; as above to handle cases when the (old)stable
# version is sufficient enough.
v_make	:= 2.9.2-3
ifeq (,$(v_make))
m4_make := -DV_MAKE=''
else
m4_make := -DV_MAKE=' (>= $(v_make))'
endif

# 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_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 $(m4_objc) $(m4_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 gnustep-base-common.links and maintainer scripts.
	sed -e 's,@GS_BASE_RESOURCES_DEBIAN@,$(GS_BASE_RESOURCES_DEBIAN),g' \
	  debian/templates/$(p_com).links.in > debian/$(p_com).links
	sed -e 's,@GS_BASE_RESOURCES_DEBIAN@,$(GS_BASE_RESOURCES_DEBIAN),g' \
	  debian/templates/$(p_com).prerm.in > debian/$(p_com).prerm
	sed -e 's,@GS_BASE_RESOURCES_DEBIAN@,$(GS_BASE_RESOURCES_DEBIAN),g' \
	  debian/templates/$(p_com).postinst.in > debian/$(p_com).postinst

# Generate libgnustep-baseSONAME.install file.
	cp debian/templates/libgnustep-baseN.install.in \
	  debian/$(p_lib).install

# When "nodoc" is present in DEB_BUILD_OPTIONS, the gnustep-make-doc
# package is created nearly empty, as required by Policy.  If it is
# also present in DEB_BUILD_PROFILES, the package is not built at all.
ifdef docs
	cp debian/templates/$(p_doc).install.in debian/$(p_doc).install
	cp debian/templates/$(p_doc).links.in debian/$(p_doc).links
endif

# Workaround FTBFS with Autoconf/2.71 (#995007) related to
# AC_CONFIG_AUX_DIR; more investigation is needed for a proper fix.
# In principle it is not necessary to pass --with-installation-domain
# because it is derived by gnustep-config which in turn checks the
# GNUSTEP_INSTALLATION_DOMAIN variable (defined in config.mk which is
# included here).  However, for some reason 1.28.0-1 mysteriously
# failed to build on GNU/Hurd so this is an attempt to fix it.
	dh_auto_configure -- \
	  CURRENT_GNUSTEP_MAKEFILES=$(GNUSTEP_MAKEFILES) \
	  --with-installation-domain=SYSTEM \
	  --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

ifdef docs
# 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) \
	    $(docs)
endif

override_dh_auto_test:
	dh_auto_test -- \
	    $(verbose)  || (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/Base/ReleaseNotes/1.*
endif
# 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$(GS_BASE_RESOURCES_UPSTREAM)/NSTimeZones/README
# Delete timezones, we use tzdata.
	$(RM) -r debian/tmp$(GS_BASE_RESOURCES_UPSTREAM)/NSTimeZones/zones

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

override_dh_installsystemd:
	dh_installsystemd -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

execute_before_dh_link:
	dh_gnustep

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))'
