#!/usr/bin/make -f

RELEASE_VERSION=$(shell head -1 debian/changelog | sed -re 's/[^(]+\(([^)]+)\).+/\1/')
RELEASE_PACKAGE=libbuffy-bindings

DEB_INSTALL_PREFIX=$(CURDIR)/debian/tmp

# 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

ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
  DO_TEST := true
endif

CXXFLAGS += -g -Wall
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
  CXXFLAGS += -O0
else
  CXXFLAGS += -O2
endif

export PYVERS=$(shell pyversions -vr)

configure: config-stamp
config-stamp:
	dh_testdir
	# Configure
	cd perl && perl Makefile.PL PREFIX="/usr" INSTALLDIRS=vendor
	cd ruby && ruby extconf.rb
	set -e; for pyver in $$PYVERS; do \
		mkdir build-py$$pyver; \
		(cd build-py$$pyver; \
		 PYTHON=python$$pyver CXXFLAGS="$(CXXFLAGS)" ../python/configure --prefix="/usr"); \
	done
	#
	touch $@

#Architecture
build: build-stamp
build-stamp: config-stamp
	# Build
	cd perl && make all OPTIMIZE="$(CXXFLAGS)"
	cd ruby && make all
	set -e; for pyver in $$PYVERS; do \
		(cd build-py$$pyver; \
		 PYTHON=python$$pyver make all); \
	done
	# Test
	test "$(DO_TEST)" = true && (cd tests && ./test.pl)
	test "$(DO_TEST)" = true && (cd tests && ./test.rb)
	set -e; cd tests; for pyver in $$PYVERS; do \
		echo "Testing Python $$pyver:"; \
		PYTHONPATH=../build-py$$pyver:../build-py$$pyver/.libs python$$pyver test.py; \
	done
	#
	touch $@

clean: 
	dh_testdir
	dh_testroot
	# Clean
	-cd perl && make realclean
	-cd python && make distclean
	-cd ruby && make clean
	rm -f perl/Makefile.old
	rm -f ruby/Makefile
	rm -f python/*.pyc
	rm -rf build-py*
	#
	dh_clean

install: install-stamp
install-stamp:
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs -i -s
	# Install
	cd perl && make install DESTDIR=$(DEB_INSTALL_PREFIX)
	cd ruby && make install DESTDIR=$(DEB_INSTALL_PREFIX)
	mv debian/tmp/usr/local/lib/site_ruby debian/tmp/usr/lib/ruby
	set -e; for pyver in $$PYVERS; do \
		(cd build-py$$pyver; \
		 make install DESTDIR=$(DEB_INSTALL_PREFIX)); \
		(cd debian/tmp/usr/lib/python$$pyver/*-packages; \
		 mv buffy-python/Buffy.py .; \
		 mv _Buffy.so.0.0.0 _Buffy.so; \
		 rm _Buffy.so.*); \
	done
	#
	dh_install -i -s
	touch $@

# Must not depend on anything. This is to be called by
# binary-arch/binary-indep
# in another 'make' thread.
binary-common:
	dh_testdir
	dh_testroot
	dh_installchangelogs 
	dh_installdocs
	#dh_installexamples
	#dh_installman
	dh_pycentral -ppython-buffy
	dh_perl -plibbuffy-perl
	dh_buildinfo
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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

binary-indep:

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


# Personal convenience rules
gitclean: clean
	test -z "`git-ls-files --exclude-per-directory=.gitignore -d -o -m -u -s`" || (echo "There are uncommitted changes"; /bin/false)
	test -z "`git diff --cached`" || (echo "There are uncommitted changes in the index"; /bin/false)

debsource: gitclean
	rm -rf _source
	mkdir _source
	git archive --format=tar --prefix=$(RELEASE_PACKAGE)-$(RELEASE_VERSION)/ master | tar -C _source -xf -
	( cd _source/$(RELEASE_PACKAGE)-$(RELEASE_VERSION)/python && autoreconf -if )
	( cd _source/$(RELEASE_PACKAGE)-$(RELEASE_VERSION) && dpkg-buildpackage -us -uc -S )
	mv _source/$(RELEASE_PACKAGE)_$(RELEASE_VERSION).tar.gz ..
	mv _source/$(RELEASE_PACKAGE)_$(RELEASE_VERSION).dsc ..
	rm -rf _source

release: debsource
	( cd .. && FAKEROOTKEY= LD_PRELOAD= sbuild -c sid -A --nolog -s $(RELEASE_PACKAGE)_$(RELEASE_VERSION).dsc )
	( cd .. && FAKEROOTKEY= LD_PRELOAD= lintian $(RELEASE_PACKAGE)_$(RELEASE_VERSION)_*.changes )

tag:
	git tag -s -m "Tagged version $(RELEASE_VERSION)" v$(RELEASE_VERSION)
