#!/usr/bin/make -f
# See debhelper(7) (uncomment to enable)
# output every command that modifies files on the build system.
#export DH_VERBOSE = 1


# see FEATURE AREAS in dpkg-buildflags(1)
export DEB_BUILD_MAINT_OPTIONS = hardening=+all,-pie

# see ENVIRONMENT in dpkg-buildflags(1)
# package maintainers to append CFLAGS
#export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic
# package maintainers to append LDFLAGS
#export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

# gnustep-base throws and catches exceptions across different shared libraries.
# Thus we use the shared libgcc.
export DEB_LDFLAGS_MAINT_APPEND = -shared-libgcc

# Instal in a temp dir
export DESTDIR=$(CURDIR)/debian/tmp

%:
	dh $@ \
	  --with autoreconf \
	  --builddirectory=build-gnustep-make

## TODO: test --enable-strict-v2-mode
## TODO: test --enable-objc-garbage-collection
## TODO: check if we can comply with XDG  --with-user-defaults-dir=
override_dh_auto_configure:
	dh_auto_configure -- \
	    --with-layout=debian \
	    --disable-strict-v2-mode


# We don't have a gnustep-make installation to use, we create a temporary one
# in the subdirectory ./tmp-installation, then run make again passing
# GNUSTEP_MAKEFILES and PATH set to point to the new makefile installation
override_dh_auto_build-indep:
	$(MAKE) -C build-gnustep-make
	  mkdir $(CURDIR)/Documentation/build-gnustep-make-doc; \
	  cd $(CURDIR)/Documentation/build-gnustep-make-doc; \
	  ../../configure --prefix="$(CURDIR)/Documentation/temp-installation" \
			  --with-layout=debian \
			  --disable-strict-v2-mode ;\
	  $(MAKE); \
	  $(MAKE) install
	$(MAKE) -C Documentation \
	  messages=yes \
	  GNUSTEP_MAKEFILES=$(CURDIR)/Documentation/temp-installation/share/GNUstep/Makefiles \
	  PATH="$$PATH:$(CURDIR)/Documentation/temp-installation/bin"

override_dh_auto_install-indep:
	$(MAKE) -C build-gnustep-make install \
	    DESTDIR=$(CURDIR)/debian/tmp
	test -d Documentation/temp-installation && $(MAKE) -C Documentation install \
	    messages=yes \
	    GNUSTEP_MAKEFILES=$(CURDIR)/Documentation/temp-installation/share/GNUstep/Makefiles \
	    GNUSTEP_INSTALLATION_DOMAIN=SYSTEM \
	    PATH="$$PATH:$(CURDIR)/Documentation/temp-installation/bin" \
	    DESTDIR=""
