#!/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

configure: config-stamp
config-stamp:
	dh_testdir
	# Configure
	cd perl && perl Makefile.PL PREFIX="/usr" INSTALLDIRS=vendor
	cd python && CXXFLAGS="$(CXXFLAGS)" ./configure --prefix="/usr"
	cd ruby && ruby extconf.rb
	#
	touch $@

#Architecture
build: build-stamp
build-stamp: configure
	# Build
	cd perl && make all OPTIMIZE="$(CXXFLAGS)"
	cd python && make all
	cd ruby && make all
	# Test
	test "$(DO_TEST)" = true && (cd tests && ./test.pl)
	test "$(DO_TEST)" = true && (cd tests && ./test.py)
	test "$(DO_TEST)" = true && (cd tests && ./test.rb)
	#
	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 build-stamp config-stamp
	#
	dh_clean 

install: install-stamp
install-stamp:
	dh_testdir
	dh_testroot
	dh_clean -k -i -s
	dh_installdirs -i -s
	# Install
	cd perl && make install DESTDIR=$(DEB_INSTALL_PREFIX)
	cd python && 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
	#
	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


# 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: release
	git tag -s -m "Tagged version $(RELEASE_VERSION)" v$(RELEASE_VERSION)
