#!/usr/bin/make -f

# Using dpkg-buildflags caused the package to fail to compile, and
# this is an old package that's not worth much effort to fix.  We'll
# use hardening wrapper instead.
export DEB_BUILD_HARDENING=1

# To distinguish variables that are truly local to this file (rather
# than for use by cdbs), we adopt the convention of starting local
# variables' names with l_.

l_LIBMAJOR=28
l_PKGNAME=libxerces$(l_LIBMAJOR)
l_LIBNAME1=libxerces-c
l_SHORTSONAME1=$(l_LIBNAME1).so.$(l_LIBMAJOR)
l_LONGSONAME1=$(l_SHORTSONAME1).0
l_LIBNAME2=libxerces-depdom
l_SHORTSONAME2=$(l_LIBNAME2).so.$(l_LIBMAJOR)
l_LONGSONAME2=$(l_SHORTSONAME2).0

l_PWD := $(shell pwd)
l_INSTALL = $(l_PWD)/debian

# Variables used by cdbs
DEB_TAR_SRCDIR = xerces-c-src_2_8_0
DEB_INSTALL_DIRS_libxerces-c28 = /usr/lib
DEB_INSTALL_DIRS_libxerces-c2-dev = /usr/include /usr/lib/$(l_PKGNAME)
DEB_INSTALL_EXAMPLES_libxerces-c2-dev = samples/*
DEB_INSTALL_DOCS_libxerces-c2-doc = \
	doc/*
DEB_INSTALL_DOCS_libxerces-c2-dev = \
	credits.txt \
	Readme.html
DEB_COMPRESS_EXCLUDE = examples sample html

# Exclude deprecrated dom library from shlib dependency calculation
# since it depends upon the other library which may in turn result in
# a circular dependency.  It is harmless for this to be passed to all
# dh_shlibdeps invocations for this package, so we won't worry about
# the fact that cdbs (as of 0.4.25) doesn't give us any good way to do
# this override on only one package.
override DEB_DH_SHLIBDEPS_ARGS = -X libxerces-depdom.so

# Include cdbs rules files.
include /usr/share/cdbs/1/rules/debhelper.mk

# Environment variables used by the build
export USELIBWWW=0
export ICUROOT=/usr

configure/libxerces-c28 configure/libxerces-c2-dev::
	chmod a+x src/xercesc/runConfigure
	cd src/xercesc \
		&& XERCESCROOT=$(l_PWD) \
		sh runConfigure -plinux  -cgcc -xg++ -minmem -ticu \
		                   -z -g \
		                   -C --prefix=/usr \
				   -C --mandir=\$${prefix}/share/man \
				   -C --infodir=\$${prefix}/share/info

build/libxerces-c28::
	XERCESCROOT=$(l_PWD) \
		$(MAKE) -C src/xercesc

# The xerces install target installs development files as well as
# shared libraries, and it doesn't necessarily install the right
# shared library file names.  Here we use make install to create the
# development package, and we install the shared libraries for the
# runtime packages manually.  After running make install, remove all
# installed shared library files, create a versionless dangling
# symbolic link to the real shared library file that will be installed
# in the runtime package, install config.status, which is needed to
# build libxml-xerces-perl, and clean up some lintian warnings about
# executable files (that aren't fixed by dh_fixperms).
install/libxerces-c2-dev::
	XERCESCROOT=$(l_PWD) \
		$(MAKE) -C src/xercesc \
		install PREFIX=$(l_INSTALL)/$(cdbs_curpkg)/usr \
		prefix=$(l_INSTALL)/$(cdbs_curpkg)/usr
	cd $(l_INSTALL)/$(cdbs_curpkg)/usr/lib \
		&& rm -f $(l_LIBNAME1).so* \
		&& ln -sf $(l_SHORTSONAME1) $(l_LIBNAME1).so \
		&& rm -f $(l_LIBNAME2).so* \
		&& ln -sf $(l_SHORTSONAME2) $(l_LIBNAME2).so
	cp src/xercesc/config.status \
		$(l_INSTALL)/$(cdbs_curpkg)/usr/lib/$(l_PKGNAME)
	find $(l_INSTALL)/$(cdbs_curpkg)/usr/include -type f -print | \
		xargs chmod -x
	find $(DEB_INSTALL_EXAMPLES_libxerces-c2-dev) \
		-type f \( -name '*.in' -o -name '*.cmd' \) -print | \
		xargs chmod -x
	find $(DEB_INSTALL_EXAMPLES_libxerces-c2-dev) \
		-name '*.cdbs-orig' -exec rm {} \;

install/libxerces-c28::
	cp lib/$(l_LIBNAME1).so \
		$(l_INSTALL)/$(cdbs_curpkg)/usr/lib/$(l_LONGSONAME1)
	ln -sf $(l_LONGSONAME1) \
		$(l_INSTALL)/$(cdbs_curpkg)/usr/lib/$(l_SHORTSONAME1)
	cp lib/$(l_LIBNAME2).so \
		$(l_INSTALL)/$(cdbs_curpkg)/usr/lib/$(l_LONGSONAME2)
	ln -sf $(l_LONGSONAME2) \
		$(l_INSTALL)/$(cdbs_curpkg)/usr/lib/$(l_SHORTSONAME2)
