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

include /usr/share/GNUstep/debian/config.mk
d_app    := $(CURDIR)/debian/talksoup.app
bundles  := $(GNUSTEP_SYSTEM_APPLICATION_SUPPORT)/TalkSoup
usrshare := /usr/share/GNUstep/TalkSoup.app
export DEB_LDFLAGS_MAINT_APPEND = -Wl,--no-undefined -Wl,--as-needed \
				    -Wl,-rpath,/usr/lib/talksoup.app

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

%:
	dh $@

override_dh_auto_build:
	$(MAKE) $(optim) messages=yes $(shell dpkg-buildflags --export=cmdline)
	convert Source/Images/TalkSoup.tiff -resize 32x32 TalkSoup.xpm

override_dh_clean:
	dh_clean TalkSoup.xpm

override_dh_auto_install:
# Cheat gnustep-make not to install the library in /usr/lib.
	$(MAKE) install DESTDIR=$(d_app) \
	  GNUSTEP_SYSTEM_LIBRARIES=/usr/lib/talksoup.app

override_dh_link:
	gsdh_gnustep
	dh_installdirs $(usrshare)
	rm $(d_app)$(GNUSTEP_SYSTEM_APPS)/*.app/Resources/*.desktop
# Move all images to /usr/share in compliance with the FHS.
# Avoid moving the app's Resources dir (as usually done for GNUstep
# packages), because the next upstream release will have all the
# bundles installed there.  Images are moved and symlinked separately,
# because each bundle is independent and may not be loaded at all.
# Furthermore, some bundles have arch-dep files in their Resources.
	mv $(d_app)$(GNUSTEP_SYSTEM_APPS)/TalkSoup.app/Resources/*.tiff \
	  $(d_app)$(usrshare) && dh_link $(usrshare)/TalkSoup.tiff \
	  $(GNUSTEP_SYSTEM_APPS)/TalkSoup.app/Resources/TalkSoup.tiff
	for i in $(d_app)$(bundles)/InFilters/Emoticon/Resources/*.tiff; do \
	  mv $$i $(d_app)$(usrshare) && dh_link $(usrshare)/$${i##*/} \
	  $(bundles)/InFilters/Emoticon/Resources/$${i##*/}; \
	done
	for i in $(d_app)$(bundles)/Output/GNUstepOutput/Resources/*.tiff; do \
	  mv $$i $(d_app)$(usrshare) && dh_link $(usrshare)/$${i##*/} \
	  $(bundles)/Output/GNUstepOutput/Resources/$${i##*/}; \
	done
	mv $(d_app)$(bundles)/InFilters/DCCSupport/Resources/*.tiff \
	  $(d_app)$(usrshare) && dh_link $(usrshare)/dccsupport_prefs.tiff \
	  $(bundles)/InFilters/DCCSupport/Resources/dccsupport_prefs.tiff
	mv $(d_app)$(bundles)/InFilters/Highlighting/Resources/*.tiff \
	  $(d_app)$(usrshare) && dh_link $(usrshare)/highlighting_prefs.tiff \
	  $(bundles)/InFilters/Highlighting/Resources/highlighting_prefs.tiff

override_dh_makeshlibs:
# The library is installed in /usr/lib/talksoup.app; no need for ldconfig.
	dh_makeshlibs -n
