#!/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 FEATURE AREAS in dpkg-buildflags(1)
export DEB_BUILD_MAINT_OPTIONS = hardening=+all,-pie

# 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,--as-needed


# gnustep-base throws and catches exceptions across different shared libraries.
# Thus we use the shared libgcc
export DEB_LDFLAGS_MAINT_APPEND = -shared-libgcc

# config.mk is provided by gnustep-make package
include /usr/share/GNUstep/debian/config.mk

# Install in debian/tmp
export DESTDIR=$(CURDIR)/debian/tmp

##### VERSION NUMBERS ###########################################

# GNUstep Base version :
v_base  := 1.24.9

# GNUstep Gui/Back version :
v_gui   := $(shell . ./Version; echo $$VERSION)

# GNUstep Gui/Back SONAME version :
sov_gui := $(shell . ./Version; echo $$GNUSTEP_GUI_MAJOR_VERSION.$$GNUSTEP_GUI_MINOR_VERSION)

#### PACKAGES NAMES ###################################
p_com           = gnustep-gui-common
p_run           = gnustep-gui-runtime
p_lib           = libgnustep-gui$(sov_gui)
p_dev           = libgnustep-gui-dev
p_dbg           = $(p_lib)-dbg
p_doc           = gnustep-gui-doc

### DIRS ##############################################

DEBIAN_GNUSTEP_PIXMAPS	= usr/share/pixmaps/GNUstep
DEBIAN_GNUSTEP_SOUNDS	= usr/share/sounds/GNUstep
DEBIAN_GNUSTEP_USRSHARE	= usr/share/GNUstep

######################################################

%:
	dh $@ \
	#  --with autoreconf

override_dh_auto_configure:
	# generate control file
	m4 \
	    -DV_BASE='$(v_base)' \
	    -DV_GUI='$(v_gui)' \
	    -DSOV_GUI='$(sov_gui)' \
	  debian/templates/control.m4 > debian/control
	
	# Generate the lintian overrides file automatically,
	# to avoid renames when the SONAME is bumped.
	m4 \
	    -DSOV_GUI='$(sov_gui)' \
	  debian/templates/libgnustep-guiN.overrides.m4 > debian/libgnustep-gui$(sov_gui).lintian-overrides
	
	 # generate gnustep-gui-common.install file
	sed -e 's,@GS_LIBRARY@,$(GNUSTEP_SYSTEM_LIBRARY),g' \
	    -e 's,@DEB_GS_PIXMAPS@,$(DEBIAN_GNUSTEP_PIXMAPS),g' \
	    -e 's,@DEB_GS_SOUNDS@,$(DEBIAN_GNUSTEP_SOUNDS),g' \
	    -e 's,@DEB_GS_USRSHARE@,$(DEBIAN_GNUSTEP_USRSHARE),g' \
	    debian/templates/$(p_com).install.in > debian/$(p_com).install
	
	# generate gnustep-gui-doc.install file
	sed -e 's,@GS_DOC@,$(GNUSTEP_SYSTEM_DOC),g' \
	    -e 's,@GS_INFO@,$(GNUSTEP_SYSTEM_DOC_INFO),g' \
	    debian/templates/$(p_doc).install.in > debian/$(p_doc).install
	#        chmod a+x debian/$(p_doc).install
	
	# generate gnustep-gui-runtime.install file
	sed -e 's,@GS_TOOLS@,$(GNUSTEP_SYSTEM_TOOLS),g' \
	    -e 's,@GS_APPS@,$(GNUSTEP_SYSTEM_APPS),g' \
	    -e 's,@GS_LIBRARY@,$(GNUSTEP_SYSTEM_LIBRARY),g' \
	    -e 's,@GS_USRSHARE@,$(DEBIAN_GNUSTEP_USRSHARE),g' \
	    debian/templates/$(p_run).install.in > debian/$(p_run).install
	
	# generate gnustep-gui-runtime.links file
	sed -e 's,@GS_LIBRARY@,$(GNUSTEP_SYSTEM_LIBRARY),g' \
	    -e 's,@GS_USRSHARE@,$(DEBIAN_GNUSTEP_USRSHARE),g' \
	    debian/templates/$(p_run).links.in > debian/$(p_run).links;
	
	# generate libgnustep-guiN.install file
	sed -e 's,@GS_LIBS@,$(GNUSTEP_SYSTEM_LIBRARIES),g' \
	    debian/templates/libgnustep-guiN.install.in > debian/$(p_lib).install
	
	# generate libgnustep-guiN.maintscript file
	sed -e 's,@SOV_GUI@,$(sov_gui),g' \
	    debian/templates/libgnustep-guiN.maintscript.in > debian/$(p_lib).maintscript
	
	# generate libgnustep-guiN.docs file
	sed -e 's,@GS_LIBS@,$(GNUSTEP_SYSTEM_LIBRARIES),g' \
	    debian/templates/libgnustep-guiN.docs.in > debian/$(p_lib).docs
	
	# generate libgnustep-gui-dev.install file
	sed -e 's,@GS_HEADERS@,$(GNUSTEP_SYSTEM_HEADERS),g' \
	    -e 's,@GS_LIBS@,$(GNUSTEP_SYSTEM_LIBRARIES),g' \
	    -e 's,@GS_MAKEFILES@,$(GNUSTEP_MAKEFILES),g' \
	    debian/templates/$(p_dev).install.in > debian/$(p_dev).install
	
	dh_auto_configure -- \
	    --enable-imagemagick
 
override_dh_auto_build-arch:
	# build shared libs
	dh_auto_build -- \
	    messages=yes \
	    doc=no
	
	# build dev/static lib
	dh_auto_build -- \
	    -C Source \
	    messages=yes \
	    doc=no \
	    shared=no
	
	# Rebuild Tools with hardening pie option
	$(MAKE) -C Tools clean SUBPROJECTS="speech say"
	dh_auto_build -- \
	    -C Tools \
	    message=yes \
	    doc=no \
	    $(shell DEB_BUILD_MAINT_OPTIONS=hardening=+all dpkg-buildflags --export=cmdline) \
	     SUBPROJECTS="speech say"
	
	# Rebuild Bundles with hardening options
	$(MAKE) -C Tools/sound clean
	$(MAKE) -C ColorPickers clean
	$(MAKE) -C TextConverters clean
	$(MAKE) -C Model clean
	dh_auto_build -- \
           -C Tools/sound \
           message=yes \
           doc=no \
           $(shell DEB_BUILD_MAINT_OPTIONS=hardening=+all,-pie dpkg-buildflags --export=cmdline)
	dh_auto_build -- \
           -C ColorPickers \
           message=yes \
           doc=no \
           $(shell DEB_BUILD_MAINT_OPTIONS=hardening=+all,-pie dpkg-buildflags --export=cmdline)
	dh_auto_build -- \
           -C TextConverters \
           message=yes \
           doc=no \
           $(shell DEB_BUILD_MAINT_OPTIONS=hardening=+all,-pie dpkg-buildflags --export=cmdline)
	dh_auto_build -- \
           -C Model \
           message=yes \
           doc=no \
           $(shell DEB_BUILD_MAINT_OPTIONS=hardening=+all,-pie dpkg-buildflags --export=cmdline)

override_dh_auto_build-indep:
	# build shared libs
	dh_auto_build -- \
	    messages=yes \
	    doc=no
	
	# build main doc. Make sure autogsdoc can find the libraries.
	dh_auto_build -- \
	    -C Documentation \
	    message=yes \
	    doc=yes

override_dh_auto_test:
	#dh_auto_test -- \
	#   messages=yes


override_dh_auto_install-arch:
	# install shared version of libraries
	dh_auto_install -- \
	    messages=yes \
	    doc=no
	
	# install dev/static lib
	dh_auto_install -- \
	    -C Source \
	    messages=yes \
	    doc=no \
	    shared=no

override_dh_auto_install-indep:
	# install shared version of libraries
	dh_auto_install -- \
	    messages=yes \
	    doc=no
	
	# install doc
	dh_auto_install -- \
	    -C Documentation \
	    message=yes \
	    doc=yes

override_dh_auto_clean-indep:
	dh_auto_clean -- \
	    messages=yes \
	    doc=yes

override_dh_install:
	# Don't install gsdoc files
	dh_install -p$(p_doc) -X.gsdoc -X.igsdoc
	
	# Don't install stamp.make file
	dh_install -p$(p_run) -Xstamp.make
	
	dh_install --remaining-packages

# Doc symlinks
override_dh_installdocs:
	dh_installdocs -p$(p_run) --link-doc=$(p_lib)
	dh_installdocs -p$(p_dev) --link-doc=$(p_lib)
	dh_installdocs -p$(p_dbg) --link-doc=$(p_lib)
	dh_installdocs --remaining-packages

# Save debug symbols in -dbg package
override_dh_strip:
	dh_strip --dbg-package=$(p_dbg)

override_dh_fixperms:
	# Fix .desktop files permissions
	find debian -name "*.desktop" -perm 0755 -exec chmod -v 0644 {} \;
	dh_fixperms

override_dh_makeshlibs:
	#  Calculate shlib dependencies for shared lib
	dh_makeshlibs -p$(p_lib) \
            -V '$(p_lib) , gnustep-gui-runtime, gnustep-back$(sov_gui)'
	# Calculate shlib dependencies for other packages
	dh_makeshlibs --remaining-packages

