#! /usr/bin/make -f
# Based on the sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

include /usr/share/hardening-includes/hardening.make

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

CFLAGS += $(HARDENING_CFLAGS)
LDFLAGS += $(HARDENING_LDFLAGS)

# Ensure the build aborts when there are still references to undefined
# symbols.
LDFLAGS += -Wl,-z,defs

# Make the linker work a bit harder so dynamic loading can be done faster
LDFLAGS += -Wl,-O1

# Make the linker only include actual dependencies on libraries, rather than
# for all libraries specified in the link line.
LDFLAGS += -Wl,--as-needed

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

upstreamversion=$(shell dpkg-parsechangelog | grep Version | head -1 | sed -e 's/Version: //g' -e 's/-[A-Za-z0-9\.]*$$//g')

# Only build the runtime packages? Note: be sure to keep this consistent
# with the packages listed in debian/control.
oldlibs_build=0

# These values must match their configure.in counterparts
gsf_version_major=1
gsf_version_minor=14

# This goes into the package names; make sure the debian/* files match
so_version_major=$(shell expr $(gsf_version_major) '*' 100 '+' $(gsf_version_minor))

# Package names
p_libgsf=libgsf-1-$(so_version_major)
p_libgsf_gnome=libgsf-gnome-1-$(so_version_major)
p_libgsf_common=libgsf-1-common
p_libgsf_bin=libgsf-bin
p_libgsf_dev=libgsf-1-dev
p_libgsf_gnome_dev=libgsf-gnome-1-dev
p_libgsf_dbg=libgsf-1-$(so_version_major)-dbg
p_libgsf_gnome_dbg=libgsf-gnome-1-$(so_version_major)-dbg

# Build up the options for "./configure"
confflags :=	-v

# FHS locations
confflags +=	--prefix=/usr \
		--sysconfdir=/etc \
		--mandir=\$${prefix}/share/man \
		--infodir=\$${prefix}/share/info \
		--with-html-dir=\$${prefix}/share/doc/$(p_libgsf_dev)/html

# Compiler, language dialect
confflags +=	--enable-compile-warnings=maximum

# Features
confflags +=	--with-bz2 --enable-gtk-doc --without-python \
		--with-gio --with-gnome-vfs

ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
	confflags += --build $(DEB_HOST_GNU_TYPE)
else
	confflags += --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
endif

DPKG_GENSYMBOLS_CHECK_LEVEL ?= 4
export DPKG_GENSYMBOLS_CHECK_LEVEL

source-updates:
	# Update files/links generated by autotools
	env srcdir=`pwd` NOCONFIGURE=1 sh autogen.sh
	# Replace links by copies of their targets
	for file in `find -type l` ; do \
		tf=`tempfile` && \
		install -p $$file $$tf && \
		rm -f $$file && \
		install -p $$tf $$file && \
		rm -f $$tf ; \
	done
	rm -rf autom4te.cache

configure: config-stamp
config-stamp:
	dh_testdir
	dh_prep

	dh_autotools-dev_updateconfig

	rm -rf build && mkdir build
	# With current libtool (1.5.24-1), LDFLAGS ends up on the actual
	# link line after some "-l" parameters. This is unfortunate, as it
	# means that passing "-Wl,--as-needed" through LDFLAGS will not drop
	# references to all unneeded libraries.
	#
	# To work around this, we pass LDFLAGS through CC (which is also
	# used for the link rules). The price we pay is some "linker input
	# file unused because linking not done" warnings.
	cd build && env "CC=$(CC) $(LDFLAGS)" "CFLAGS=$(CFLAGS)" ../configure $(confflags)

	touch config-stamp

build: build-stamp
build-stamp:  config-stamp
	dh_testdir
	cd po; intltool-update -p

	$(MAKE) -C build

	touch build-stamp

clean:
	dh_testdir
	dh_testroot

	if test -d build && test -f build/Makefile ; then \
		$(MAKE) -C build distclean; \
	fi
	rm -rf build

	-find -type f -name 'Makefile.am' | sed -e 's/\.am$$//' | xargs rm -f

	dh_autotools-dev_restoreconfig

	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_installdirs

	# Add here commands to install the package into debian/tmp
	$(MAKE) -C build \
		GCONF_DISABLE_MAKEFILE_SCHEMA_INSTALL=1 \
		DESTDIR=$(CURDIR)/debian/tmp \
		install

	@if ! test -r $(CURDIR)/debian/tmp/usr/lib/libgsf-gnome-1.so ; then \
		echo "" ; \
		echo "libgsf-gnome-1 didn't get installed properly" ; \
		echo "Perhaps ltmain.sh needs to be regenerated using a libtool package patched" ; \
		echo "with the \"inst-prefix\" patch?" ; \
		exit 1; \
	fi

	rm -f $(CURDIR)/debian/tmp/usr/lib/libgsf-*.la

	mkdir -p $(CURDIR)/debian/tmp/usr/lib/debug
	for l in `find $(CURDIR)/debian/tmp/usr/lib -name '*.so*'`; do \
		cp -vdf $$l $(CURDIR)/debian/tmp/usr/lib/debug ; \
	done

ifeq ($(oldlibs_build),1)
	# Delete files and directories that belong to packages we don't build
	cat debian/$(p_libgsf_common).install debian/$(p_libgsf_bin).install debian/$(p_libgsf_dev).install debian/$(p_libgsf_gnome_dev).install debian/$(p_libgsf_dbg).install debian/$(p_libgsf_gnome_dbg).install | while read pattern ; do \
		(cd debian/tmp && rm -vrf $$pattern) ; \
	done
	# Basically all remaining files and directories should belong to a
	# package.
else
	# Building not just runtime packages. Basically all installed files
	# and directories should belong to a package.
endif

	dh_install --sourcedir=debian/tmp --fail-missing -Xindex.sgml

# This single target is used to build all the packages, all at once, or
# one at a time. So keep in mind: any options passed to commands here will
# affect _all_ packages. Anything that should only affect one package
# should be put in another target (such as the install target) or be put
# into conditional constructs.
binary-common:
	dh_testdir
	dh_testroot
	dh_installchangelogs ChangeLog
ifeq ($(DH_OPTIONS),-s)
	# The -dbg packages don't have separate documentation; link to the
	# corresponding runtime packages' documentation.
	rm -rvf $(CURDIR)/debian/$(p_libgsf_dbg)/usr/share/doc/$(p_libgsf_dbg)
	rm -rvf $(CURDIR)/debian/$(p_libgsf_gnome_dbg)/usr/share/doc/$(p_libgsf_gnome_dbg)

	# Unfortunately, "-s" overrides "-p" for dh_link messes things up.
	env -u DH_OPTIONS dh_link -p$(p_libgsf_dbg) /usr/share/doc/$(p_libgsf)/ /usr/share/doc/$(p_libgsf_dbg)
	env -u DH_OPTIONS dh_link -p$(p_libgsf_gnome_dbg) /usr/share/doc/$(p_libgsf)/ /usr/share/doc/$(p_libgsf_gnome_dbg)
	# Make the documentation accessible through devhelp.
	env -u DH_OPTIONS dh_link -p$(p_libgsf_dev) /usr/share/doc/$(p_libgsf_dev)/html/gsf/ /usr/share/gtk-doc/html/gsf
endif
	dh_installdocs
	dh_installexamples
	dh_installmenu
#	dh_installdebconf
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installcatalogs
#	dh_installpam
#	dh_installmime
#	dh_installinit
#	dh_installman
#	dh_installcron
#	dh_installinfo
#	dh_undocumented
	dh_gconf
#	dh_link
	dh_buildinfo
	dh_compress
#	dh_perl
#	dh_python
ifeq ($(DH_OPTIONS),-s)
	dh_strip -s -v

	# Unfortunately, "-s" overrides "-p" for dh_makeshlibs and by acting
	# on all architecture-dependent packages we would get wrong shlibs
	# here, so we need to undo the "-s".
	env -u DH_OPTIONS dh_makeshlibs -p$(p_libgsf) -V '$(p_libgsf) (>= $(upstreamversion))'
	env -u DH_OPTIONS dh_makeshlibs -p$(p_libgsf_gnome) -V '$(p_libgsf_gnome) (>= $(upstreamversion))'

	dh_shlibdeps -s -l debian/$(p_libgsf)/usr/lib/ -L $(p_libgsf) \
		-l debian/$(p_libgsf_gnome)/usr/lib/ -L $(p_libgsf_gnome)
endif
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture independant packages using the common target.
binary-indep: build install
	 $(MAKE) -f debian/rules DH_OPTIONS=-i binary-common

# Build architecture dependant packages using the common target.
binary-arch: build install
	$(MAKE) -f debian/rules DH_OPTIONS=-s binary-common

## Any other binary targets build just one binary package at a time.
#binary-%: build install
#	make -f debian/rules binary-common DH_OPTIONS=-p$*

binary: binary-indep binary-arch
.PHONY: source-updates configure build clean binary-indep binary-arch binary-common binary install
