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

include /usr/share/GNUstep/debian/config.mk

d_app := $(CURDIR)/debian/gnumail.app
sprt  := /usr/share/GNUstep/ApplicationSupport/GNUMail
export DEB_LDFLAGS_MAINT_APPEND := -Wl,--no-undefined -Wl,--as-needed \
				     -Wl,-rpath,/usr/lib/gnumail.app

ifneq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
optim := debug=yes
endif

%:
	dh $@

override_dh_auto_build:
	$(MAKE) messages=yes $(optim) $(shell dpkg-buildflags --export=cmdline)

override_dh_auto_install:
# Cheat GNUstep Make to install the library at /usr/lib/gnumail.app.
	$(MAKE) install GNUSTEP_SYSTEM_LIBRARIES=/usr/lib/gnumail.app \
	  DESTDIR=$(d_app)
	rm $(d_app)$(GNUSTEP_SYSTEM_APPS)/*.app/Resources/*.desktop
	find $(d_app) -type d -empty -delete

override_dh_link:
# Move all arch-independent Resources to /usr/share for FHS compliance.
	gsdh_gnustep
	dh_installdirs $(sprt)
	mv $(d_app)$(GNUSTEP_SYSTEM_APPS)/GNUMail.app/Resources \
	  $(d_app)/usr/share/GNUstep/GNUMail.app
	dh_link usr/share/GNUstep/GNUMail.app \
	  $(GNUSTEP_SYSTEM_APPS)/GNUMail.app/Resources
	for d in $(d_app)$(GNUSTEP_SYSTEM_APPLICATION_SUPPORT)/GNUMail/*; do \
	  mv $$d/Resources $(d_app)$(sprt)/$${d##*/} \
	  && dh_link $(sprt)/$${d##*/} \
	   $(GNUSTEP_SYSTEM_APPLICATION_SUPPORT)/GNUMail/$${d##*/}/Resources; \
	done
# A dirty hack to move arch-indep data to the -common package.  Cannot
# be done in the install-* targets as /usr/share/GNUstep has been just
# populated by this recipe.
	mkdir -p $(CURDIR)/debian/gnumail.app-common/usr/share
	mv $(d_app)/usr/share/GNUstep \
	  $(CURDIR)/debian/gnumail.app-common/usr/share

override_dh_strip:
	dh_strip --dbg-package=gnumail.app-dbg

override_dh_makeshlibs:
# Avoid lintian warnings for private library in /usr/lib/gnumail.app.
	dh_makeshlibs -n
