#!/usr/bin/make -f

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

include /usr/share/quilt/quilt.make

# 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)

# FOR AUTOCONF 2.52 AND NEWER ONLY
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

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

CFLAGS = -Wall -g -mthumb

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

ifeq ($(DEB_BUILD_ARCH), armel)
	CFLAGS += -mfpu=neon -mfloat-abi=softfp

ifneq (,$(findstring thumb,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -mthumb
endif
endif

build: patch build-stamp
build-stamp:
	dh_testdir
	./autogen.sh

	# ./configure
	LDFLAGS="-Wl,--as-needed" CFLAGS="$(CFLAGS)" SBOX_REDIRECT_IGNORE=/usr/bin/python ./configure $(confflags) PYTHON=/usr/bin/python --prefix=/usr --with-history
	echo "all:" > example/Makefile
	echo "install:" >> example/Makefile
	echo "distclean:" >> example/Makefile
	echo "	rm -rf .deps" >> example/Makefile

	mv -f python/Makefile python/Makefile.bak
	echo all: > python/Makefile
	# Build libxml2
	SBOX_REDIRECT_IGNORE=/usr/bin/python $(MAKE)
	mv -f python/Makefile.bak python/Makefile

	touch build-stamp

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

	[ ! -f Makefile ] || $(MAKE) distclean
	-rm -f example/Makefile
	rm COPYING && cp Copyright COPYING

	-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

install: install-stamp install-python-stamp
install-stamp: build-stamp
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	mv -f python/Makefile python/Makefile.bak
	echo install: > python/Makefile
	$(MAKE) DESTDIR=`pwd`/debian/tmp install
	mv -f python/Makefile.bak python/Makefile

	mv debian/tmp/usr/share/aclocal/libxml.m4 debian/tmp/usr/share/aclocal/libxml2.m4

	# Properly install documentation in /usr/share/doc/libxml2-doc
	install -d debian/tmp/usr/share/doc/libxml2-doc/examples
	install -m 644 \
		doc/*.html \
		doc/*.gif \
		doc/*.png \
		doc/libxml2-api.xml debian/tmp/usr/share/doc/libxml2-doc
	install -m 644 \
		doc/examples/*.c \
		doc/examples/*.res \
		doc/examples/index.html \
		doc/examples/[tw]*.xml debian/tmp/usr/share/doc/libxml2-doc/examples
	cp -a \
		doc/html \
		doc/tutorial debian/tmp/usr/share/doc/libxml2-doc


	# 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 install-stamp

build-python%-stamp: build-stamp
	SBOX_REDIRECT_IGNORE=/usr/bin/python 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
	SBOX_REDIRECT_IGNORE=/usr/bin/python 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 $@

# Build architecture-independent files here.
binary-indep: build install
	dh_testdir -i
	dh_testroot -i
	dh_installdocs -i -A AUTHORS TODO README NEWS
	dh_installchangelogs
	dh_install -i --sourcedir=debian/tmp
	dh_link -i
	dh_strip -i
	dh_compress -i -Xexamples/ -Xtutorial/ -Xhtml/ -X.html
	dh_fixperms -i
	dh_installdeb -i
	dh_shlibdeps -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir -a
	dh_testroot -a
	dh_installexamples -a
	dh_installdocs -plibxml2 -A AUTHORS TODO README NEWS
	dh_installdocs -ppython-libxml2 python/TODO
	ln -sf $(foreach file, AUTHORS README README.Debian changelog.Debian.gz changelog.gz,../libxml2/$(file)) debian/python-libxml2/usr/share/doc/python-libxml2/$(file)
	ln -sf libxml2 debian/libxml2-dev/usr/share/doc/libxml2-dev
	ln -sf libxml2 debian/libxml2-utils/usr/share/doc/libxml2-utils
	ln -sf libxml2 debian/libxml2-dbg/usr/share/doc/libxml2-dbg
	dh_installman -a
	dh_installchangelogs -plibxml2 ChangeLog
	dh_install -a --sourcedir=debian/tmp
	sed "/dependency_libs/ s/'.*'/''/" debian/tmp/usr/lib/libxml2.la > debian/libxml2-dev/usr/lib/libxml2.la
	dh_pysupport -a
	dh_python -a
	dh_link -a
	dh_strip -a --dbg-package=libxml2-dbg -Npython-libxml2
	dh_strip -ppython-libxml2
	$(foreach python, $(filter-out $(PYVER), $(PYVERS)),\
		cd $(CURDIR)/debian/python-libxml2/usr/lib/python-support/python-libxml2; \
		if diff $(python)/libxml2mod.so $(PYVER)/libxml2mod.so > /dev/null 2>&1; then \
			rm -f $(python)/libxml2mod.so; \
			ln -s ../$(PYVER)/libxml2mod.so $(python)/libxml2mod.so; \
		fi;)
	dh_compress -a -Xexamples/
	dh_fixperms -a
	dh_makeshlibs -a -V 'libxml2 (>= 2.6.32)' -- -c4
	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: build clean binary-indep binary-arch binary install
