#!/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)

DESTDIR := $(CURDIR)/debian/tmp

#defined LDFLAGS to prevent dpkg-shlibdeps warnings
LDFLAGS := -Wl,-z,defs -Wl,--as-needed

configure: configure-stamp
configure-stamp:
	dh_testdir
	. ${GNUSTEP_MAKEFILES}/GNUstep.sh ; \
	./configure --prefix=/usr
	touch configure-stamp

build: build-stamp

build-stamp: configure-stamp
	dh_testdir
	#define ADDITIONAL_OBJCFLAGS to prevent FTBFS (thanks Yavor Doganov) 
	$(MAKE) LDFLAGS="$(LDFLAGS)" ADDITIONAL_OBJCFLAGS=-DDEBUG doc=yes messages=yes

	# Relink only DBModeler and GDL2Palette to avoid binary-or-shlib-defines-rpath.
	for d in DBModeler GDL2Palette; do \
	  $(RM) $$d/$$d.app/$$d $$d/GDL2.palette/GDL2; \
	  $(MAKE) -C $$d ADDITIONAL_OBJCFLAGS=-DDEBUG messages=yes \
	  LDFLAGS="$(LDFLAGS) -Wl,-rpath,/usr/lib/gnustep-dl2"; \
	done
	#generate application icon
	convert DBModeler/Resources/DBModeler.tiff -resize 32x32 DBModeler.xpm

	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp
	# Add here commands to clean up after the build process.
	[ ! -f GNUmakefile ] || $(MAKE) clean doc=yes
	rm -rf EOAdaptors/GNUmakefile EOAdaptors/PostgreSQLAdaptor/GNUmakefile EOAdaptors/PostgreSQLAdaptor/config.mak \
                EOAdaptors/SQLiteAdaptor/GNUmakefile EOAdaptors/SQLiteAdaptor/config.mak \
		EOControl/config.h GNUmakefile config.log config.status gdl2.make 

	rm -f DBModeler.xpm
	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	# Add here commands to install the package

	$(MAKE) install GNUSTEP_INSTALLATION_DOMAIN=SYSTEM DESTDIR=$(DESTDIR) doc=yes

	#install libEOModeler as private library
	$(RM) $(DESTDIR)/usr/lib/libEOModeler*
	$(MAKE) -C EOModeler install DESTDIR=$(DESTDIR) \
	  GNUSTEP_SYSTEM_LIBRARIES=/usr/lib/gnustep-dl2 \
	  GNUSTEP_INSTALLATION_DOMAIN=SYSTEM

	rm -f $(DESTDIR)/usr/lib/GNUstep/Applications/*.app/Resources/*.desktop

	#remove PostgreSQLAdaptor headers as adivised by upstream (David Ayers)
	rm -rf $(DESTDIR)/usr/lib/GNUstep/Frameworks/PostgreSQLEOAdaptor.framework/Headers

	#remove empty directory
	rm -rf $(DESTDIR)/usr/lib/GNUstep/Frameworks/SQLite3EOAdaptor.framework/Versions/0/Headers

	#remove gsdoc files generated for libgnustep-dl2-dev
	rm -rf $(DESTDIR)/usr/share/GNUstep/Documentation/EO*/*gsdoc
	rm -rf $(DESTDIR)/usr/share/GNUstep/Documentation/GDL2/*gsdoc

	#move resources to /usr/share/GNUstep and add a symlink if the installation is following the FHS
ifeq ($(GS_USE_FHS),yes)
	dh_installdirs usr/share/GNUstep
	mv $(DESTDIR)$(GNUSTEP_SYSTEM_APPS)/DBModeler.app/Resources \
	  $(DESTDIR)/usr/share/GNUstep/DBModeler.app
	dh_link usr/share/GNUstep/DBModeler.app \
	  $(GNUSTEP_SYSTEM_APPS)/DBModeler.app/Resources

	mv $(DESTDIR)$(GNUSTEP_SYSTEM_APPLICATION_SUPPORT)/Palettes/GDL2.palette/Resources \
	  $(DESTDIR)/usr/share/GNUstep/GDL2
	dh_link usr/share/GNUstep/GDL2 \
	 $(GNUSTEP_SYSTEM_APPLICATION_SUPPORT)/Palettes/GDL2.palette/Resources
endif
	#install the XPM icon for DBModeler
	 install -D -m 644 DBModeler.xpm \
           debian/gnustep-dl2/usr/share/pixmaps/DBModeler.xpm

# Build architecture-independent files here.
binary-indep: build install

# Build architecture-dependent files here.
binary-arch: build install 
	dh_testdir
	dh_testroot
	dh_installdirs 
	dh_installchangelogs ChangeLog
	dh_installdocs 
	dh_installman 
	dh_installmenu
	dh_installexamples Examples/*
	dh_install
	dh_link 
	dh_strip 
	dh_compress -Xexamples
	dh_fixperms 
	dh_makeshlibs -V -plibgnustep-dl2-0d
	dh_shlibdeps 
# Calculate the adaptor packages' shlibs:Depends separately, to
# avoid circular dependencies (#574949).
	dh_shlibdeps -Ngnustep-dl2-postgresql-adaptor \
	  -Ngnustep-dl2-sqlite-adaptor
	dh_shlibdeps -pgnustep-dl2-postgresql-adaptor \
	  -pgnustep-dl2-sqlite-adaptor -- -xlibgnustep-dl2-0d
	dh_installdeb 
	dh_gencontrol 
	dh_md5sums 
	dh_builddeb 

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