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

include /usr/share/GNUstep/debian/config.mk
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
# Support for --GNU-Debug option.
export DEB_CPPFLAGS_MAINT_APPEND = -DDEBUG
# EOModeler is installed as private library; EOModelEditor and the
# GDL2 palette link with it.  The adaptors are also installed as
# private; their login panels link with the corresponding framework.
private_dir := $(GNUSTEP_SYSTEM_LIBRARIES)/gnustep-dl2
export DEB_LDFLAGS_MAINT_APPEND = -Wl,-rpath,$(private_dir)

# Reset the docs variable if it's an arch:any build.
ifeq (,$(filter gnustep-dl2-doc,$(shell dh_listpackages)))
docs :=
palette := debian/gnustep-dl2/usr/share/GNUstep/Palettes/GDL2.palette
endif

# Define Debian-specific SONAME due to an ABI break.
soname := EOControl_INTERFACE_VERSION=0deb \
	  EOAccess_INTERFACE_VERSION=0deb \
	  EOInterface_INTERFACE_VERSION=0deb

%:
	dh $@

override_dh_auto_build:
	dh_auto_build -- $(verbose) $(optim) $(docs) $(soname) \
	  $(shell dpkg-buildflags --export=cmdline)

override_dh_auto_install:
	dh_auto_install -- $(verbose) $(docs) $(soname) \
	  DESTDIR=$(CURDIR)/debian/tmp
# Reinstall EOModeler and the adaptors as private libraries.
	dh_auto_install -- -C Apps/EOModeler $(verbose) \
	  GNUSTEP_SYSTEM_LIBRARIES=$(private_dir)
	dh_auto_install -- -C EOAdaptors $(verbose) \
	  GNUSTEP_SYSTEM_LIBRARIES=$(private_dir)
	rm debian/tmp$(GNUSTEP_SYSTEM_LIBRARIES)/libEOModeler*
	rm debian/tmp$(GNUSTEP_SYSTEM_LIBRARIES)/libPostgreSQLEOAdaptor.so*
	rm debian/tmp$(GNUSTEP_SYSTEM_LIBRARIES)/libSQLite3EOAdaptor.so*
	rm debian/tmp$(GNUSTEP_SYSTEM_HEADERS)/SQLite3EOAdaptor
	rm debian/tmp$(GNUSTEP_SYSTEM_FRAMEWORKS)/SQLite3EOAdaptor.framework/Headers
	$(RM) -r debian/tmp$(GNUSTEP_SYSTEM_FRAMEWORKS)/SQLite3EOAdaptor.framework/Versions/0/Headers
# Convert the app icon to PNG as AppStream does not recognise TIFF.
	convert Apps/EOModelEditor/Resources/EOModelEditor.tiff \
	  debian/tmp$(GNUSTEP_SYSTEM_APPS)/EOModelEditor.app/Resources/app-icon.png
ifneq (,$(docs))
	cp debian/gnustep-dl2-doc.install.in debian/gnustep-dl2-doc.install
	cp debian/gnustep-dl2-doc.links.in debian/gnustep-dl2-doc.links
endif

override_dh_auto_clean:
	dh_auto_clean -- $(verbose) $(docs)
	$(RM) debian/gnustep-dl2-doc.install debian/gnustep-dl2-doc.links

override_dh_auto_test:
	dh_auto_install -- $(soname) DESTDIR=$(CURDIR)/test
	./debian/tests/tools
	$(RM) test.* example.db library.eomodel Examples/library.gsdoc
	$(RM) -r test
# Cleaning is absolutely mandatory otherwise build artifacts sneak
# into the -doc package.
	$(MAKE) -C Examples clean

execute_before_dh_link:
	gsdh_gnustep -pgnustep-dl2 --app --appsupport --bug-script
	gsdh_gnustep -pgnustep-dl2-postgresql-adaptor --no-move --bug-script
	gsdh_gnustep -pgnustep-dl2-sqlite-adaptor --no-move --bug-script
	gsdh_gnustep --remaining-packages

override_dh_fixperms-arch:
# Fix three lintian warnings executable-not-elf-or-script and
# I: image-file-has-conflicting-name.
	find debian/gnustep-dl2/usr/share/GNUstep/EOModelEditor.app \
	  -type f -executable -exec chmod -x '{}' \;
	mv $(palette)/GDL2Palette.tiff $(palette)/GDL2Palette.png
	dh_fixperms

# Don't add ldconfig triggers for adaptors and EOModeler.
override_dh_makeshlibs:
	dh_makeshlibs -n -pgnustep-dl2
	dh_makeshlibs -n -pgnustep-dl2-sqlite-adaptor
	dh_makeshlibs -n -pgnustep-dl2-postgresql-adaptor
	dh_makeshlibs --remaining-packages