#!/usr/bin/make -f
## ----------------------------------------------------------------------
## debian/rules : package script for libxslt
## ----------------------------------------------------------------------

## ----------------------------------------------------------------------
## uncomment this to turn on verbose mode
#export DH_VERBOSE=1

# The versions of python currently supported
PYVERS=$(shell pyversions -s)
# The current default version of python
PYVER=$(shell pyversions -d)

WORKAROUND_REMOVED_FILES=libexslt/exsltconfig.h libxslt/xsltconfig.h libxslt/xsltwin32config.h libxslt.spec
WORKAROUND_MODIFIED_FILES=python/libxslt-py.c

## ----------------------------------------------------------------------
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)

## ----------------------------------------------------------------------
CONFFLAGS =
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

## ----------------------------------------------------------------------
CFLAGS = -Wall -g

## ----------------------------------------------------------------------
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif

## ----------------------------------------------------------------------
TMP_DIR = debian/tmp

## ----------------------------------------------------------------------
## targets

configure: configure-stamp
configure-stamp:
	dh_testdir

	# Workaround backups because of non-idempotent upstream:
	$(foreach file, $(WORKAROUND_REMOVED_FILES) $(WORKAROUND_MODIFIED_FILES), \
		cp -f -a $(file) $(file)-tmp;)

	# ./configure
	CFLAGS="$(CFLAGS)" \
	./configure $(CONFFLAGS) --prefix=/usr

	touch configure-stamp

build: build-stamp
build-stamp: configure-stamp
	dh_testdir

	mv python/Makefile python/Makefile.bak
	echo all: > python/Makefile
	# Build libxslt
	$(MAKE)
	mv python/Makefile.bak python/Makefile

	touch build-stamp 

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp install*-stamp

	-$(MAKE) distclean
	
	# Restore workaround backups of removed and modified files
	-$(foreach file, $(WORKAROUND_REMOVED_FILES) $(WORKAROUND_MODIFIED_FILES), \
		test -r $(file)-tmp && \
		mv -f $(file)-tmp $(file);)

	-test -r /usr/share/misc/config.sub && \
	cp -f /usr/share/misc/config.sub config.sub
	-test -r /usr/share/misc/config.guess && \
	cp -f /usr/share/misc/config.guess config.guess

	dh_clean -X.orig

install: install-stamp install-python-stamp
install-stamp: build-stamp
	dh_testdir
	dh_testroot
	dh_clean -k -X.orig
	dh_installdirs

	mv -f python/Makefile python/Makefile.bak
	echo install: > python/Makefile
	$(MAKE) DESTDIR=$(CURDIR)/$(TMP_DIR) install
	mv -f python/Makefile.bak python/Makefile

	# Properly install documentation in /usr/share/doc/libxslt1-dev
	install -d debian/tmp/usr/share/doc/libxslt1-dev/EXSLT
	install -m 644 \
		doc/*.html \
		doc/*.gif \
		doc/libxslt-*.xml debian/tmp/usr/share/doc/libxslt1-dev
	install -m 644 \
		doc/EXSLT/*.html \
		doc/EXSLT/libexslt-*.xml debian/tmp/usr/share/doc/libxslt1-dev
	cp -a \
		doc/html \
		doc/tutorial \
		doc/tutorial2 debian/tmp/usr/share/doc/libxslt1-dev
	cp -a \
		doc/EXSLT/html debian/tmp/usr/share/doc/libxslt1-dev/EXSLT

	# Special workaround for svn-buildpackage:
	# Restore workaround backups of modified files.
	-$(foreach file, $(WORKAROUND_MODIFIED_FILES), \
		test -r $(file)-tmp && \
		cat $(file)-tmp > $(file);)

	touch $@

build-python%-stamp: build-stamp
	cd python && $(MAKE) clean && $(MAKE) PYTHON=/usr/bin/python$* PYTHON_VERSION=$* PYTHON_INCLUDES=/usr/include/python$*
	
	touch $@

install-python%-stamp: build-python%-stamp
	rm build-python$*-stamp
	cd python && $(MAKE) DESTDIR=`pwd`/../debian/tmp PYTHON=/usr/bin/python$* PYTHON_VERSION=$* PYTHON_INCLUDES=/usr/include/python$* install

	touch $@

install-python-stamp: $(PYVERS:%=install-%-stamp)
	touch $@

binary-indep:
	dh_testdir
	dh_testroot
	dh_installdocs -i
	dh_installchangelogs -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_install -a --sourcedir=$(TMP_DIR)
	sed "/dependency_libs/ s/'.*'/''/" debian/tmp/usr/lib/libexslt.la > debian/libxslt1-dev/usr/lib/libexslt.la
	dh_installman -a
	dh_installdocs -a -A AUTHORS FEATURES NEWS README TODO
	dh_installexamples -a
	dh_installchangelogs -a ChangeLog
	dh_pysupport -a
	dh_python -a
	dh_link -a
	dh_strip -a --dbg-package=libxslt1-dbg -Npython-libxslt1
	dh_strip -ppython-libxslt1
	$(foreach python, $(filter-out $(PYVER), $(PYVERS)),\
		cd $(CURDIR)/debian/python-libxslt1/usr/lib/python-support/python-libxslt1; \
		if diff $(python)/libxsltmod.so $(PYVER)/libxsltmod.so > /dev/null 2>&1; then \
			rm -f $(python)/libxsltmod.so; \
			ln -s ../$(PYVER)/libxsltmod.so $(python)/libxsltmod.so; \
		fi;)
	dh_compress -a -Xexamples/
	dh_fixperms -a
	dh_makeshlibs -a -V 'libxslt1.1 (>= 1.1.18)'
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

source diff:
	@echo >&2 'source and diff are obsolete - use dpkg-source -b'; false

binary: binary-indep binary-arch
.PHONY: clean build install binary-indep binary-arch binary configure
