#!/usr/bin/make -f
# -*- makefile -*-

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

#needed to build with Python 2.7
-include /usr/share/python/python.mk
ifeq (,$(py_sitename))
  py_sitename = site-packages
  py_libdir = /usr/lib/python$(subst python,,$(1))/site-packages
  py_sitename_sh = $(py_sitename)
  py_libdir_sh = $(py_libdir)
  py_setup_install_args = 
endif


#tcl version we're building for
TCL_VERS := $(shell dpkg -s tcl-dev | grep '^Depends' \
	| sed 's,.*\<tcl\([0-9.]*\)-dev\>.*,\1,')

DEB_HOST_MULTIARCH  ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

# Use the date of the changelog as a build date:
BUILD_DATETIME := $(shell dpkg-parsechangelog -S Date)
BUILD_DATE := $(shell date -u --date="$(BUILD_DATETIME)" +%F)
BUILD_TIME := $(shell date -u --date="$(BUILD_DATETIME)" +%T)
export DEB_CPPFLAGS_MAINT_APPEND=-DBUILD_DATE='"$(BUILD_DATE)"' -DBUILD_TIME='"$(BUILD_TIME)"'
export BUILD_DATE BUILD_TIME

export ABS_TOP_BUILDDIR=$(CURDIR)

%:
	dh $@ --with autoreconf,python2,ruby

override_dh_auto_configure:
	dh_auto_configure -- \
	 --disable-silent-rules \
	 --disable-rpath \
	 --disable-python \
	 --disable-ruby \
	 --with-perl-options="INSTALLDIRS=vendor INSTALL_BASE=" \
	 --with-tcllib=/usr/lib/$(DEB_HOST_MULTIARCH)/tcl$(TCL_VERS) \
	 RRDDOCDIR=/usr/share/doc/rrdtool

override_dh_clean:
	dh_clean
	rm -rf bindings/perl-piped/blib/ bindings/perl-shared/blib/
	set -e ;\
	 cd bindings/python ;\
	 python setup.py clean
	dh_ruby --clean

override_dh_auto_build:
	dh_auto_build
	set -e ;\
	 cd bindings/python ;\
	 BUILDLIBDIR=../../src/.libs LIBDIR=../../src/.libs python setup.py build
	#Fix library path to tcl bindings
	sed -i -e 's|lib/|lib/tcltk/|' -e 's|/tclrrd|/rrdtool-tcl/tclrrd|' bindings/tcl/pkgIndex.tcl
	#sanity check if debian/control lists the right package name
	LIB_major=`grep '^dlname=' src/librrd.la \
			| sed -e "s/^dlname='librrd\.so\.\\([0-9]\+\\)'\$$/\\1/"` \
		&& test -n "$$LIB_major" \
		&& grep -q "^Package: librrd$$LIB_major\\>" debian/control
	dh_ruby --build

override_dh_install:
	dh_install -X.la -X.txt -X.pod -Xsystemd --fail-missing
	# remove duplicates
	rm debian/rrdtool/usr/bin/rrdcached
	rm debian/rrdtool/usr/share/man/man1/rrdcached.1
	rm debian/rrdtool/usr/share/doc/rrdtool/html/rrdcached.html
	set -e ;\
	 cd bindings/python ;\
	 python setup.py install --root=$(CURDIR)/debian/python-rrdtool \
		$(py_setup_install_args) ;\
	 python-dbg setup.py install --root=$(CURDIR)/debian/rrdtool-dbg \
		$(py_setup_install_args)

	find $(CURDIR)/debian/rrdtool-dbg -type f -name '*.egg-info' -print0 |\
		 xargs -0r rm -f
	dh_ruby --install

override_dh_installdocs:
	dh_installdocs -A CONTRIBUTORS NEWS

override_dh_installchangelogs:
	dh_installchangelogs -A CHANGES

override_dh_fixperms:
	dh_fixperms
	chmod 644 debian/rrdcached/usr/share/doc/rrdcached/examples/RRDCached.pm

override_dh_strip:
	dh_strip --dbg-package=rrdtool-dbg

override_dh_makeshlibs:
	dh_makeshlibs -Nliblua5.1-rrd0 -Nliblua5.1-rrd-dev
	dh_makeshlibs -pliblua5.1-rrd0 -pliblua5.1-rrd-dev -n

override_dh_gencontrol:
	echo 'tcl:Depends=tcl$(TCL_VERS) | tclsh' >> debian/rrdtool-tcl.substvars
	dh_gencontrol
