#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
#
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.
#
# Modified to make a template file for a multi-binary package with separated
# build-arch and build-indep targets  by Bill Allombert 2001

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

# This has to be exported to make some magic below work.
export DH_OPTIONS
export LC_ALL=C

CFLAGS	:= $(shell dpkg-buildflags --get CFLAGS)
DEB_HOST_MULTIARCH := $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

PYVERS	:= $(shell pyversions -vr debian/control)

include /usr/share/python/python.mk

p	= python-svn
d	= debian/$(p)

defver	= $(shell /usr/bin/python -c 'import sys; print sys.version[:3]')
pdef	= python$(defver)-svn

CXX	= g++

APR_INC	= $(strip $(shell apr-config --includes | sed 's/-I//'))

build: build-stamp
build-stamp: backport-stamp $(PYVERS:%=build-stamp%) $(PYVERS:%=dbg-build-stamp%)
	touch $@

backport-stamp:
ifneq (,$(filter 2.4 2.5, $(PYVERS)))
	rm -rf backport
	mkdir -p backport
	tar cf - Builder Docs Examples INSTALL.html Import Kit LICENSE.txt Patches Source Tests \
	  | tar -C backport -x -f -
	cd backport/Source && python2.5 setup.py backport
endif
	touch $@

build-stamp%: backport-stamp
	dh_testdir
	rm -f $(if $(filter $*, 2.4 2.5),backport/)Source/*.o $(if $(filter $*, 2.4 2.5),backport/)Source/Makefile
	cd $(if $(filter $*, 2.4 2.5),backport/)Source && python$* setup.py configure \
		--pycxx-src-dir=/usr/share/python$*/CXX \
		--pycxx-dir=/usr/share/python$*/CXX \
		--svn-lib-dir=/usr/lib/$(DEB_HOST_MULTIARCH) \
		--apr-inc-dir=$(APR_INC) \

#		--norpath
	PYSVN_BUILD_REVISION=1 \
	  $(MAKE) -C $(if $(filter $*, 2.4 2.5),backport/)Source
	mv $(if $(filter $*, 2.4 2.5),backport/)Source/pysvn/_pysvn_$(subst .,_,$*).so _pysvn_$(subst .,_,$*).so
	mv $(if $(filter $*, 2.4 2.5),backport/)Source/pysvn/__init__.py __init__$(subst .,_,$*).py
	touch $@

dbg-build-stamp%: backport-stamp
	dh_testdir
	rm -f $(if $(filter $*, 2.4 2.5),backport/)Source/*.o $(if $(filter $*, 2.4 2.5),backport/)Source/Makefile
	cd $(if $(filter $*, 2.4 2.5),backport/)Source && python$*-dbg setup.py configure \
		--pycxx-src-dir=/usr/share/python$*/CXX \
		--pycxx-dir=/usr/share/python$*/CXX \
		--svn-lib-dir=/usr/lib/$(DEB_HOST_MULTIARCH) \
		--apr-inc-dir=$(APR_INC) \

#		--norpath
	PYSVN_BUILD_REVISION=1 \
	  $(MAKE) -C $(if $(filter $*, 2.4 2.5),backport/)Source
	mv $(if $(filter $*, 2.4 2.5),backport/)Source/pysvn/_pysvn_$(subst .,_,$*).so _pysvn_$(subst .,_,$*)_d.so
	mv $(if $(filter $*, 2.4 2.5),backport/)Source/pysvn/__init__.py __init__$(subst .,_,$*).py
	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp* dbg-build-stamp*
	rm -f Source/*.o Source/pysvn/*.so Source/pysvn_version.hpp
	rm -f Source/pysvn_docs.[ch]pp Source/Makefile Tests/Makefile
	rm -f Source/generate_svn_error_codes/generate_svn_error_codes Source/generate_svn_error_codes.hpp
	rm -f _pysvn*.so*
	rm -f __init__*.py
	-find -name '*.py[co]' | xargs rm -f
	rm -rf backport backport-stamp
	dh_clean 

install: install-stamp
install-pre:
	dh_testdir
	dh_testroot
	dh_clean -k -s 
install-stamp: build-stamp install-pre $(PYVERS:%=install-python%) $(PYVERS:%=dbg-install-python%)
install-python%:
	dh_installdirs -ppython-svn \
		$(call py_libdir,$*)/pysvn
	install -d $(d)/$(call py_libdir,$*)/pysvn/
	cp -p _pysvn_$(subst .,_,$*).so $(d)/$(call py_libdir,$*)/pysvn/
	cp -p __init__$(subst .,_,$*).py $(d)/$(call py_libdir,$*)/pysvn/__init__.py

	-find $(d) -name .svn | xargs rm -rf

dbg-install-python%:
	dh_installdirs -ppython-svn-dbg \
		$(call py_libdir,$*)/pysvn
	cp -p _pysvn_$(subst .,_,$*)_d.so $(d)-dbg/$(call py_libdir,$*)/pysvn/
	-find $(d)-dbg -name .svn | xargs rm -rf

# Build architecture independant packages using the common target.
binary-indep: build install
# nothing to do

# Build architecture dependant packages using the common target.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs -a
	dh_installdocs -a -A -X.svn Docs/*
	dh_installexamples -a -A Examples/*
	dh_strip -ppython-svn --dbg-package=python-svn-dbg
	rm -rf debian/python-svn-dbg/usr/share/doc/python-svn-dbg
	ln -s python-svn debian/python-svn-dbg/usr/share/doc/python-svn-dbg
	dh_compress -a -X.py -X.sxw
	dh_fixperms -a
	dh_python2 -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

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