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

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_LDFLAGS_MAINT_APPEND = -Wl,--no-undefined -Wl,--as-needed

# 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_xlib          = gnustep-back$(sov_back)-xlib
p_common        = gnustep-back-common

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

# build dirs
b_art           = $(CURDIR)/build-art
b_cairo         = $(CURDIR)/build-cairo
b_xlib          = $(CURDIR)/build-xlib

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

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

%:
	dh $@

# Ensure that debian/control is always up-to-date.
override_dh_testdir:
	m4 \
	    -DV_GUI='$(v_gui)' \
	    -DSOV_GUI='$(sov_gui)' \
	    -DSOV_BACK='$(sov_back)' \
		debian/templates/control.m4 > debian/control

override_dh_auto_configure:
# generate gnsutep-back-common.install file
	sed -e 's,@GNUSTEP_SYSTEM_TOOLS@,$(GNUSTEP_SYSTEM_TOOLS),g' \
	    -e 's,@GNUSTEP_SYSTEM_DOC@,$(GNUSTEP_SYSTEM_DOC),g' \
	    debian/templates/$(p_common).install.in > debian/$(p_common).install

# generate gnsutep-back-common.postinst script
	sed -e 's,@DEB_GNUSTEP_FONTSDIR@,$(DEB_GNUSTEP_FONTSDIR),g' \
	    debian/templates/$(p_common).postinst.in > debian/$(p_common).postinst

# generate gnsutep-back-common.preinst script
	sed -e 's,@DEB_GNUSTEP_FONTSDIR@,$(DEB_GNUSTEP_FONTSDIR),g' \
	    debian/templates/$(p_common).preinst.in > debian/$(p_common).preinst

# generate gnsutep-back-common.prerm script
	sed -e 's,@DEB_GNUSTEP_FONTSDIR@,$(DEB_GNUSTEP_FONTSDIR),g' \
	    debian/templates/$(p_common).prerm.in > debian/$(p_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/$(p_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/$(p_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/$(p_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/$(p_cairo).prerm

# generate gnustep-backN-xlib.postinst file
	sed -e 's:@GNUSTEP_LIBRARY@:$(GNUSTEP_SYSTEM_LIBRARY):g' \
	    -e 's:@IFVER@:$(ifv_back):g' \
	    -e 's:@BACKEND@:xlib:g' \
	    -e 's:@PRIORITY@:5:g' \
	    debian/templates/gnustep-backN-backend.postinst.in > debian/$(p_xlib).postinst

# generate gnustep-backN-xlib.prerm file
	sed -e 's:@GNUSTEP_LIBRARY@:$(GNUSTEP_SYSTEM_LIBRARY):g' \
	    -e 's:@IFVER@:$(ifv_back):g' \
	    -e 's:@BACKEND@:xlib:g' \
	    -e 's:@PRIORITY@:5:g' \
	    debian/templates/gnustep-backN-backend.prerm.in > debian/$(p_xlib).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/$(p_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/$(p_cairo).install

# generate gnustep-backN-xlib.install file
	sed -e 's:@GNUSTEP_LIBRARY@:$(GNUSTEP_SYSTEM_LIBRARY):g' \
	    -e 's:@IFVER@:$(ifv_back):g' \
	    -e 's:@BACKEND@:xlib:g' \
	    debian/templates/gnustep-backN-backend.install.in > debian/$(p_xlib).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/$(p_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/$(p_cairo).links

# generate gnustep-backN-xlib.links file
	sed -e 's:@GNUSTEP_LIBRARY@:$(GNUSTEP_SYSTEM_LIBRARY):g' \
	    -e 's:@IFVER@:$(ifv_back):g' \
	    -e 's:@BACKEND@:xlib:g' \
	    debian/templates/gnustep-backN-backend.links.in > debian/$(p_xlib).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)

# Likewise for xlib.
	dh_auto_configure --builddirectory=$(b_xlib) -- \
	    --enable-graphics=xlib --with-name=xlib \
	|| (cat $(b_xlib)/config.log; exit 1)

	cp Source -r $(b_xlib)/Source
	cp Tools -r $(b_xlib)/Tools
	ln -sf \
	    ../Version ../Headers  ../configure ../back.make.in \
	    ../GNUmakefile ../GNUmakefile.postamble \
	    $(b_xlib)

# dpkg-buildflags must be supplied on the command line because
# gnustep-make resets OBJCFLAGS and CFLAGS.
override_dh_auto_build:
	dh_auto_build -- -C Documentation
# Build cairo backend
	dh_auto_build -- \
	    messages=yes \
	    $(optim) \
	    SERIAL_SUBDIRECTORIES=Source \
	    GNUSTEP_BUILD_DIR=$(b_cairo) \
	    $(shell dpkg-buildflags --export=cmdline)

# Build art backend
	dh_auto_build --builddirectory=$(b_art) -- \
	   messages=yes \
	   $(optim) \
	   SERIAL_SUBDIRECTORIES=Source \
	   GNUSTEP_BUILD_DIR=$(b_art) \
	   $(shell dpkg-buildflags --export=cmdline)

# Build xlib + tools
	dh_auto_build --builddirectory=$(b_xlib) -- \
	   messages=yes \
	   $(optim) \
	   fonts=no \
	   GNUSTEP_BUILD_DIR=$(b_xlib) \
	   $(shell dpkg-buildflags --export=cmdline)

override_dh_auto_install:
	dh_auto_install -- -C Documentation
# Install cairo backend
	dh_auto_install -- \
	   SERIAL_SUBDIRECTORIES=Source \
	   GNUSTEP_BUILD_DIR=$(b_cairo)

# Install art backend
	dh_auto_install --builddirectory=$(b_art) -- \
	    SERIAL_SUBDIRECTORIES=Source \
	    GNUSTEP_BUILD_DIR=$(b_art)

# Install the xlib backend along with the tools.
	dh_auto_install --builddirectory=$(b_xlib) -- \
	    fonts=no \
	    GNUSTEP_BUILD_DIR=$(b_xlib)

override_dh_auto_clean:
	-dh_auto_clean -- -C Documentation
	dh_auto_clean
	rm -rf $(b_cairo) $(b_art) $(b_xlib)

# gnustep-gui's shlibs *must* be overridden, otherwise there are
# circular dependencies (#882078).
override_dh_shlibdeps:
	echo \
	  "libgnustep-gui $(sov_back) libgnustep-gui$(sov_back) (>= $(v_gui)), gnustep-gui-runtime (>= $(v_gui))" \
	  > debian/shlibs.local
	dh_shlibdeps
	rm debian/shlibs.local
