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

include /usr/share/GNUstep/debian/config.mk
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_LDFLAGS_MAINT_APPEND = -Wl,--no-undefined

##################### 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)
v_gui := 0.31.1-7

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

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

p_back          = gnustep-back$(sov_back)
p_cairo         = gnustep-back$(sov_back)-cairo
p_headless      = gnustep-back$(sov_back)-headless
p_xlib          = gnustep-back$(sov_back)-xlib
p_common        = gnustep-back-common

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

# build dirs
b_cairo         = $(CURDIR)/build-cairo
b_headless      = $(CURDIR)/build-headless
b_xlib          = $(CURDIR)/build-xlib

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

%:
	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 gnustep-back-common.bug-script file.
	sed -e 's:@IFVER@:$(ifv_back):g' \
	  debian/templates/$(p_common).bug-script.in \
	  > debian/$(p_common).bug-script
# 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-headless.postinst file.
	sed -e 's:@GNUSTEP_LIBRARY@:$(GNUSTEP_SYSTEM_LIBRARY):g' \
	    -e 's:@IFVER@:$(ifv_back):g' \
	    -e 's:@BACKEND@:headless:g' \
	    -e 's:@PRIORITY@:1:g' \
	    debian/templates/gnustep-backN-backend.postinst.in > debian/$(p_headless).postinst
# Generate gnustep-backN-headless.prerm file.
	sed -e 's:@GNUSTEP_LIBRARY@:$(GNUSTEP_SYSTEM_LIBRARY):g' \
	    -e 's:@IFVER@:$(ifv_back):g' \
	    -e 's:@BACKEND@:headless:g' \
	    -e 's:@PRIORITY@:1:g' \
	    debian/templates/gnustep-backN-backend.prerm.in > debian/$(p_headless).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-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-headless.install file.
	sed -e 's:@GNUSTEP_LIBRARY@:$(GNUSTEP_SYSTEM_LIBRARY):g' \
	    -e 's:@IFVER@:$(ifv_back):g' \
	    -e 's:@BACKEND@:headless:g' \
	    debian/templates/gnustep-backN-backend.install.in > debian/$(p_headless).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-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-headless.links file.
	sed -e 's:@GNUSTEP_LIBRARY@:$(GNUSTEP_SYSTEM_LIBRARY):g' \
	    -e 's:@IFVER@:$(ifv_back):g' \
	    -e 's:@BACKEND@:headless:g' \
	    debian/templates/gnustep-backN-backend.links.in > debian/$(p_headless).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
# Configure the headless backend in a separate build dir.  Note the
# addition of --enable-server.
	dh_auto_configure --builddirectory=$(b_headless) -- \
	  --enable-server=headless  --enable-graphics=headless \
	  --with-name=headless
	cp Source -r $(b_headless)/Source
	ln -sf \
	  ../Version ../Headers  ../configure ../back.make.in \
	  ../GNUmakefile ../GNUmakefile.postamble \
	  $(b_headless)
# Configure xlib backend in a separate build dir.
	dh_auto_configure --builddirectory=$(b_xlib) -- \
	  --enable-graphics=xlib --with-name=xlib
	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 -- $(verbose) $(optim) SERIAL_SUBDIRECTORIES=Source \
	  $(shell dpkg-buildflags --export=cmdline)
# Build the headless backend.
	dh_auto_build --builddirectory=$(b_headless) -- \
	  $(verbose) $(optim) SERIAL_SUBDIRECTORIES=Source \
	  $(shell dpkg-buildflags --export=cmdline)
# Build xlib + tools
	dh_auto_build --builddirectory=$(b_xlib) -- \
	   $(verbose) $(optim) fonts=no \
	   $(shell dpkg-buildflags --export=cmdline)

override_dh_auto_install:
	dh_auto_install -- -C Documentation
# Install cairo backend
	dh_auto_install -- SERIAL_SUBDIRECTORIES=Source
# Install the headless backend.
	dh_auto_install --builddirectory=$(b_headless) -- \
	  SERIAL_SUBDIRECTORIES=Source
# Install the xlib backend along with the tools.
	dh_auto_install --builddirectory=$(b_xlib) -- fonts=no
# Delete duplicate files already shipped at /usr/share/doc/.
	$(RM) -r debian/tmp$(GNUSTEP_SYSTEM_DOC)/Developer/Back/ReleaseNotes

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

execute_before_dh_link:
	gsdh_gnustep

# 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
