#!/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 EXAMPLES in dpkg-buildflags(1) and read /usr/share/dpkg/*
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/default.mk

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

# 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,--no-undefined -Wl,--as-needed \
#				    -Wl,-rpath,/usr/lib/gworkspace


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

g_app	 := $(CURDIR)/debian/gworkspace.app
m_app	 := $(CURDIR)/debian/mdfinder.app
g_wrap	 := $(CURDIR)/debian/gworkspace-apps-wrappers
usrshare := /usr/share/GNUstep


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

%:
	dh $@ --parallel --with autotools-dev

override_dh_auto_configure:
	dh_auto_configure -- \
	  --enable-gwmetadata \
	  --enable-debug-log \
	  --with-unzip=unzip

override_dh_auto_build:
	$(MAKE) $(optim) \
	  messages=yes \
	$(shell dpkg-buildflags --export=cmdline)
	convert GWorkspace/Resources/Icons/FileManager.tiff \
	  -resize 32x32 GWorkspace.xpm
	convert GWMetadata/MDFinder/Resources/Images/MDFinder.tiff \
	  -resize 32x32 MDFinder.xpm

override_dh_clean:
# FIXME: Remember to fix upstream's build system.
	find \( -name config.log -o -name config.status \) -delete
	find -name GNUmakefile -delete
	dh_clean *.xpm -X.m.orig

override_dh_auto_install:
# Cheat gnustep-make to install the libraries as private.
	$(MAKE) install \
	  GNUSTEP_SYSTEM_LIBRARIES=/usr/lib/gworkspace \
	  DESTDIR=$(CURDIR)/debian/tmp

override_dh_installman:
	dh_installman debian/GWorkspace.1
# Create symlinks for the manpages.  Deliberately do not derive the
# programs' list from /usr/bin -- if a tool is added upstream; the
# manpage has to be updated.
	for p in ddbd fswatcher lsfupdater resizer searchtool wopen \
	    Recycler; do \
	  dh_link usr/share/man/man1/GWorkspace.1 usr/share/man/man1/$$p.1; \
	done
	for p in gmds mdextractor; do \
	  dh_link -pmdfinder.app usr/share/man/man1/MDFinder.1 \
	    usr/share/man/man1/$$p.1; \
	done

override_dh_link:
	gsdh_gnustep
	dh_installdirs -pmdfinder.app $(usrshare)
	rm $(g_app)$(GNUSTEP_SYSTEM_APPS)/*.app/Resources/*.desktop
	rm $(m_app)$(GNUSTEP_SYSTEM_APPS)/*.app/Resources/*.desktop
# Move all Resources containing images to /usr/share/GNUstep.
	cd $(g_app)$(GNUSTEP_SYSTEM_APPS); \
	  for i in *.app/Resources; do \
	    mv $$i $(g_app)$(usrshare)/$${i%Resources}; \
	  done
	for i in $(g_app)$(usrshare)/*.app; do \
	  dh_link $(usrshare)/$${i##*/} \
	    $(GNUSTEP_SYSTEM_APPS)/$${i##*/}/Resources; \
	done
	cd $(g_app)$(GNUSTEP_SYSTEM_BUNDLES); \
	  for i in *.inspector/Resources; do \
	    mv $$i $(g_app)$(usrshare)/$${i%Resources}; \
	  done
	for i in $(g_app)$(usrshare)/*.inspector; do \
	  dh_link $(usrshare)/$${i##*/} \
	    $(GNUSTEP_SYSTEM_BUNDLES)/$${i##*/}/Resources; \
	done
# Handle mdfinder.app's stuff as well.
	mv $(m_app)$(GNUSTEP_SYSTEM_APPS)/*.app/Resources \
	  $(m_app)$(usrshare)/MDFinder.app
	dh_link -pmdfinder.app $(usrshare)/MDFinder.app \
	  $(GNUSTEP_SYSTEM_APPS)/MDFinder.app/Resources
	mv $(m_app)$(GNUSTEP_SYSTEM_BUNDLES)/*.prefPane/Resources \
	  $(m_app)$(usrshare)/MDIndexing.prefPane
	dh_link -pmdfinder.app $(usrshare)/MDIndexing.prefPane \
	  $(GNUSTEP_SYSTEM_BUNDLES)/MDIndexing.prefPane/Resources
# Now a dirty hack to move arch-indep data to the -common package.
# Cannot be done in the install-* targets as $(usrshare) has been just
# populated by this recipe.
	mkdir -p $(CURDIR)/debian/gworkspace-common/usr/share
	mv $(g_app)$(usrshare) $(CURDIR)/debian/gworkspace-common/usr/share

override_dh_fixperms:
	dh_fixperms
	chmod -x $(m_app)$(usrshare)/MDIndexing.*/*.tiff
	-chmod -x $(g_wrap)$(GNUSTEP_SYSTEM_APPS)/vi.app/vi.tiff

override_dh_strip:
	dh_strip --dbg-package=gworkspace-dbg

# Avoid lintian warnings for private libraries in /usr/lib/gworkspace.
override_dh_makeshlibs:
	dh_makeshlibs -n
