#!/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 Back version :
v_back := $(shell . ./Version; echo $$VERSION)

# GNUstep Back SONAME version :
sov_back := $(shell . ./Version; echo $$GNUSTEP_BACK_MAJOR_VERSION.$$GNUSTEP_BACK_MINOR_VERSION)

# GNUstep Back INTERFACE version :
ifv_back := $(shell . ./Version; echo $$INTERFACE_VERSION_NUMBER)

# GNUstep Gui version :
v_gui   := $(v_back)

# GNUstep Gui SONAME version :
sov_gui := $(sov_back)

#### PACKAGES NAMES #############################################

p_back          = gnustep-back$(sov_back)
p_art           = gnustep-back$(sov_back)-art
p_cairo         = gnustep-back$(sov_back)-cairo
p_common        = gnustep-back-common
p_dbg		= gnustep-back-dbg

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

# build dirs
b_art           = $(CURDIR)/build-art
b_cairo         = $(CURDIR)/build-cairo
b_tools		= $(CURDIR)/build-tools

# Debian GNUstep fonts directory
DEB_GNUSTEP_FONTSDIR = /var/lib/GNUstep/Fonts

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

%:
	dh $@
	#  --with autoreconf


override_dh_auto_configure:
	 # generate control file
	m4 \
	    -DV_GUI='$(v_gui)' \
	    -DSOV_GUI='$(sov_gui)' \
	    -DSOV_BACK='$(sov_back)' \
	        debian/templates/control.m4 > debian/control
	
	# generate gnsutep-back-common.install file
	sed -e 's,@GNUSTEP_SYSTEM_TOOLS@,$(GNUSTEP_SYSTEM_TOOLS),g' \
	    debian/templates/gnustep-back-common.install.in > debian/gnustep-back-common.install
	
	# generate gnsutep-back-common.postinst script
	sed -e 's,@DEB_GNUSTEP_FONTSDIR@,$(DEB_GNUSTEP_FONTSDIR),g' \
	    debian/templates/gnustep-back-common.postinst.in > debian/gnustep-back-common.postinst
	
	# generate gnsutep-back-common.preinst script
	sed -e 's,@DEB_GNUSTEP_FONTSDIR@,$(DEB_GNUSTEP_FONTSDIR),g' \
	    debian/templates/gnustep-back-common.preinst.in > debian/gnustep-back-common.preinst
	
	# generate gnsutep-back-common.prerm script
	sed -e 's,@DEB_GNUSTEP_FONTSDIR@,$(DEB_GNUSTEP_FONTSDIR),g' \
	    debian/templates/gnustep-back-common.prerm.in > debian/gnustep-back-common.prerm
	
	# generate gnustep-backN-art.postinst file
	sed -e 's:@GNUSTEP_LIBRARY@:$(GNUSTEP_SYSTEM_LIBRARY):g' \
	    -e 's:@IFVER@:$(ifv_back):g' \
	    -e 's:@BACKEND@:art:g' \
	    -e 's:@PRIORITY@:10:g' \
	    debian/templates/gnustep-backN-backend.postinst.in > debian/gnustep-back$(sov_back)-art.postinst
	
	# generate gnustep-backN-art.prerm file
	sed -e 's:@GNUSTEP_LIBRARY@:$(GNUSTEP_SYSTEM_LIBRARY):g' \
	    -e 's:@IFVER@:$(ifv_back):g' \
	    -e 's:@BACKEND@:art:g' \
	    -e 's:@PRIORITY@:10:g' \
	    debian/templates/gnustep-backN-backend.prerm.in > debian/gnustep-back$(sov_back)-art.prerm
	
	# generate gnustep-backN-cairo.postinst file
	sed -e 's:@GNUSTEP_LIBRARY@:$(GNUSTEP_SYSTEM_LIBRARY):g' \
	    -e 's:@IFVER@:$(ifv_back):g' \
	    -e 's:@BACKEND@:cairo:g' \
	    -e 's:@PRIORITY@:15:g' \
	    debian/templates/gnustep-backN-backend.postinst.in > debian/gnustep-back$(sov_back)-cairo.postinst
	
	# generate gnustep-backN-cairo.prerm file
	sed -e 's:@GNUSTEP_LIBRARY@:$(GNUSTEP_SYSTEM_LIBRARY):g' \
	    -e 's:@IFVER@:$(ifv_back):g' \
	    -e 's:@BACKEND@:cairo:g' \
	    -e 's:@PRIORITY@:15:g' \
	    debian/templates/gnustep-backN-backend.prerm.in > debian/gnustep-back$(sov_back)-cairo.prerm
	
	# generate gnustep-backN-art.install file
	sed -e 's:@GNUSTEP_LIBRARY@:$(GNUSTEP_SYSTEM_LIBRARY):g' \
	    -e 's:@IFVER@:$(ifv_back):g' \
	    -e 's:@BACKEND@:art:g' \
	    debian/templates/gnustep-backN-backend.install.in > debian/gnustep-back$(sov_back)-art.install
	
	# generate gnustep-backN-cairo.install file
	sed -e 's:@GNUSTEP_LIBRARY@:$(GNUSTEP_SYSTEM_LIBRARY):g' \
	    -e 's:@IFVER@:$(ifv_back):g' \
	    -e 's:@BACKEND@:cairo:g' \
	    debian/templates/gnustep-backN-backend.install.in > debian/gnustep-back$(sov_back)-cairo.install
	
	# generate gnustep-backN-art.links file
	sed -e 's:@GNUSTEP_LIBRARY@:$(GNUSTEP_SYSTEM_LIBRARY):g' \
	    -e 's:@IFVER@:$(ifv_back):g' \
	    -e 's:@BACKEND@:art:g' \
	    debian/templates/gnustep-backN-backend.links.in > debian/gnustep-back$(sov_back)-art.links
	
	 # generate gnustep-backN-cairo.links file
	sed -e 's:@GNUSTEP_LIBRARY@:$(GNUSTEP_SYSTEM_LIBRARY):g' \
	    -e 's:@IFVER@:$(ifv_back):g' \
	    -e 's:@BACKEND@:cairo:g' \
	    debian/templates/gnustep-backN-backend.links.in > debian/gnustep-back$(sov_back)-cairo.links
	
	 # generate gnustep-backN.docs file
	sed -e 's,@GS_LIBRARY@,$(GNUSTEP_SYSTEM_LIBRARY),g' \
	    debian/templates/gnustep-backN.docs.in > debian/$(p_back).docs
	
	
	# Configure cairo backend
	dh_auto_configure -- \
	    --enable-graphics=cairo --with-name=cairo \
	|| (cat config.log; exit 1)
	
	# Configure art backend in a separate build dir
	dh_auto_configure --builddirectory=$(b_art) -- \
	    --enable-graphics=art --with-name=art \
	|| (cat $(b_art)/config.log; exit 1)
	
	cp Source -r $(b_art)/Source
	ln -sf \
	    ../Version ../Headers  ../configure ../back.make.in \
	    ../GNUmakefile ../GNUmakefile.postamble \
	    $(b_art)
	
override_dh_auto_build:
	# Build cairo backend
	dh_auto_build -- \
	    messages=yes \
	    doc=yes \
	    GNUSTEP_BUILD_DIR=$(b_cairo)
	
	# Build art backend
	dh_auto_build --builddirectory=$(b_art) -- \
	   messages=yes \
	   doc=no \
	   fonts=no \
	   tools=no \
	   GNUSTEP_BUILD_DIR=$(b_art)
	
	# Rebuild Tools with hardening pie option
	$(MAKE) -C Tools clean
	dh_auto_build -- \
	    -C Tools \
	    messages=yes \
	    doc=no \
	    GNUSTEP_BUILD_DIR=$(b_tools) \
	    $(shell DEB_BUILD_MAINT_OPTIONS=hardening=+all dpkg-buildflags --export=cmdline)
	
override_dh_auto_install:		
	# Install art backend
	dh_auto_install --builddirectory=$(b_art) -- \
	    messages=yes \
	    doc=no \
	    fonts=no \
	    tools=no \
	    GNUSTEP_BUILD_DIR=$(b_art)
	
	# Install cairo backend
	dh_auto_install -- \
	   messages=yes \
	   doc=yes \
	   GNUSTEP_BUILD_DIR=$(b_cairo)
	
	# Install Tools
	dh_auto_install -- \
	    -C Tools \
	    messages=yes \
	    doc=no \
	    GNUSTEP_BUILD_DIR=$(b_tools)

override_dh_auto_clean:
	# Clean cairo backend
	dh_auto_clean -- \
	    GNUSTEP_BUILD_DIR=$(b_cairo)
	
	# Clean Tools
	dh_auto_clean -- \
	    GNUSTEP_BUILD_DIR=$(b_tools)
	
	# Clean art backend
	dh_auto_clean --builddirectory=$(b_art) -- 

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

