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

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

export GNUSTEP_MAKEFILES := $(GS_MAKE_DIR)
export GNUSTEP_INSTALLATION_DOMAIN := SYSTEM

d_app := $(CURDIR)/debian/gnumail.app
LDFLAGS := -Wl,-z,defs -Wl,--as-needed -Wl,-rpath,/usr/lib/gnumail.app

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

build: build-stamp

build-stamp:
	dh_testdir
	$(MAKE) messages=yes $(optim) LDFLAGS="$(LDFLAGS)"
	touch $@

clean:
	dh_testdir
	dh_testroot
	$(MAKE) distclean

install: build
	dh_testdir
	dh_testroot
	dh_prep
# Cheat GNUstep Make to install the library at /usr/lib/gnumail.app.
	dh_installdirs usr/lib/gnumail.app
	$(MAKE) install GNUSTEP_SYSTEM_LIBRARIES=/usr/lib/gnumail.app \
	  DESTDIR=$(d_app)
	dh_install debian/GNUMail.xpm usr/share/pixmaps/
	dh_install debian/*.desktop usr/share/applications/
	rm $(d_app)$(GNUSTEP_SYSTEM_APPS)/*.app/Resources/*.desktop

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs ChangeLog
	dh_installdocs
	dh_installmenu
	dh_installman debian/GNUMail.1
# Move all arch-independent Resources to /usr/share for FHS compliance.
	gsdh_gnustep
	dh_installdirs usr/share/GNUstep/ApplicationSupport/GNUMail
	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)/usr/share/GNUstep/ApplicationSupport/GNUMail/`basename $$d` \
	  && dh_link usr/share/GNUstep/ApplicationSupport/GNUMail/`basename $$d` \
	    $(GNUSTEP_SYSTEM_APPLICATION_SUPPORT)/GNUMail/`basename $$d`/Resources; \
	done
	dh_strip --dbg-package=gnumail.app-dbg
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install
