#!/usr/bin/make -f
# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

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

export GNUSTEP_MAKEFILES := $(GS_MAKE_DIR)

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

# Avoid building the bundled StepTalk framework.
export external := no

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

# Workaround for gcc-4.4 bug 519006.
ifneq (,$(findstring mips,$(DEB_BUILD_ARCH)))
mipsen-flags := ADDITIONAL_OBJCFLAGS=-g0 ADDITIONAL_CFLAGS=-g0
endif

%:
	dh $@

override_dh_auto_build:
# Upstream's makefiles have conditionals based on $(MAKELEVEL) so that
# building separate components and the whole package from toplevel
# works.  They all evaluate to false when building the Debian package,
# because debian/rules is itself a makefile -- resetting MAKELEVEL
# does the trick.
# GNUstep Make 2.2.0 gained support for parallel (-jN) building, which
# is implemented by delegating the actual compilation to sub-makes
# which run in parallel (i.e. the build system invokes
# $(MAKE) -f GNUmakefile ...).  This is the default mode of operation,
# even if no -j option is used.  GNUSTEP_MAKE_PARALLEL_BUILDING=no
# prevents messing the MAKELEVEL again.
	MAKELEVEL=0 $(MAKE) GNUSTEP_MAKE_PARALLEL_BUILDING=no $(optim) \
	  messages=yes ADDITIONAL_CFLAGS="$(CFLAGS)" \
	  LDFLAGS="$(LDFLAGS)" \
          $(mipsen-flags)
	convert UL/Resources/icon.tiff -resize 32x32 adun.xpm

## tried for hardening but does not help
## if all four variables below are set the build even fails
#	  ADDITIONAL_LDFLAGS="$(LDFLAGS)" \
#	  ADDITIONAL_OBJC_LIBS="$(LDFLAGS)" \
#	  ADDITIONAL_TOOL_LIBS="$(LDFLAGS)" \
#	  ADDITIONAL_GUI_LIBS="$(LDFLAGS)" \

override_dh_auto_clean:
ifdef GS_MAKE_DIR
# This conditional and the ignored config.mk include at the top is to
# make life easier for the usual debian-med contributors who do not
# have gnustep-make installed.  Otherwise, the package will fail to
# build before pbuilder/etc. even manage to install the build-deps
# because the clean rule is doomed to fail.
	$(MAKE) distclean
endif

override_dh_clean:
	dh_clean adun.xpm

override_dh_auto_install:
	$(MAKE) install messages=yes DESTDIR=$(d_app) \
	  GNUSTEP_SYSTEM_LIBRARIES=/usr/lib/adun.app \
	  GNUSTEP_INSTALLATION_DOMAIN=SYSTEM

override_dh_install:
	dh_install
# Delete empty directories (fixes a lintian warning).
	rm -r $(d_app)/usr/include/GNUstep/Framework \
	      $(d_app)/usr/lib/GNUstep/Tools
# Delete the invalid .desktop file and install ours.
	rm $(d_app)/usr/lib/GNUstep/Applications/*.app/Resources/*.desktop

override_dh_fixperms:
	dh_fixperms
	find $(d_app)/usr/share/GNUstep/UL.app -type f -exec chmod -x '{}' \;
	find $(d_app)/usr/lib/GNUstep/Applications/UL.app/Resources -type f -exec chmod -x '{}' \;
	chmod -x $(d_app)/usr/lib/GNUstep/Frameworks/ULFramework.framework/Versions/0/Resources/ForceFields/Enzymix/*.ffml

# hack: no idea when and how to call dh_gnustep - just enforce it after dh_links
override_dh_link:
	dh_link
	dh_gnustep
ifeq ($(GS_USE_FHS),yes)
# On (official) Debian systems, this conditional is always true --
# arch-independent Resources are moved to /usr/share, and the
# Resources dir in the app bundle is a symlink (this approach was
# explicitly approved by the Debian Release Team back in 2005).
# The GNUstep LiveCD is based on Debian and its maintainer (who as a
# coincident :-) is also a member of the Debian GNUstep team) doesn't
# like FHS very much.  Setting GS_USE_FHS=no allows to rebuild the
# package(s) with more relaxed (and not Debian Policy-compliant)
# layout.
	dh_installdirs usr/share/GNUstep
	mv $(d_app)$(GNUSTEP_SYSTEM_APPS)/UL.app/Resources \
	   $(d_app)/usr/share/GNUstep/UL.app
	dh_link usr/share/GNUstep/UL.app \
	  $(GNUSTEP_SYSTEM_APPS)/UL.app/Resources
endif

# lintian claims that postinst + postrm have useless call to ldconfig
# the following code would remove these files totally but than update-menus
# would be left out ... leaving the lintian warning for somebody else
#override_dh_installdeb:
#	dh_installdeb
#	find debian \( -name postinst -o -name postrm \) -delete

get-orig-source:
	mkdir -p ../tarballs
	uscan --verbose --force-download --destdir=../tarballs

