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

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

#we use quilt
include /usr/share/quilt/quilt.make

#needed to build with Python 2.6
-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



PY_VERS := $(shell pyversions -vr)

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

#let's help configure to figure out our host/build system
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_ARCH_CPU   ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_CPU)
DEB_HOST_MULTIARCH  ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

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

CONFFLAGS += --disable-silent-rules

#we build the bindings manually from debian/rules
CONFFLAGS += --disable-python --disable-ruby

#set the right paths
CONFFLAGS += --prefix=/usr --docdir=/usr/share/doc/rrdtool

#install perl libs in vendor mode
CONFFLAGS += --with-perl-options="INSTALLDIRS=vendor INSTALL_BASE="

#path to tcl
CONFFLAGS += --with-tcllib=/usr/lib/$(DEB_HOST_MULTIARCH)/tcl$(TCL_VERS)

#let's take care of optimization + debug builds
CFLAGS += -Wall -g
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
        CFLAGS += -O0
else
#don't optimize on arm for now - #447041
ifneq ($(DEB_HOST_ARCH_CPU), arm)
        CFLAGS += -O2
endif
endif

# work-around for a broken dpkg - do not rename this to LDFLAGS :-/
LINKER_FLAGS=-Wl,-z,defs

configure-stamp: $(QUILT_STAMPFN)

	dh_testdir
	dh_autoreconf

	LDFLAGS="$(LINKER_FLAGS)" \
	CFLAGS="$(CFLAGS)" \
	RRDDOCDIR=/usr/share/doc/rrdtool \
	  ./configure $(CONFFLAGS)

	touch $@

build: build-arch build-indep 

build-arch: build-arch-stamp $(PY_VERS:%=build-python%) $(PY_VERS:%=build-python%-dbg)
build-arch-stamp: configure-stamp
	make
	#Fix library path to tcl bindings
	sed -i -e 's|lib/|lib/tcltk/rrdtool-tcl/|' 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
	touch $@

build-python%: build-arch-stamp
	set -e ;\
	 cd bindings/python ;\
	 BUILDLIBDIR=../../src/.libs LIBDIR=../../src/.libs python$* setup.py build ;\
	 touch $@

build-indep: build-arch-stamp
	#indep stuff is built in build-arch-stamp, too.

clean: clean-patched
clean-patched: unpatch $(PY_VERS:%=clean-python%) $(PY_VERS:%=clean-python%-dbg)
	dh_testdir
	dh_testroot

	[ ! -f Makefile ] || make distclean

	rm -f build-arch-stamp build-indep-stamp config.status install-common-stamp configure-stamp
	find . -name \*.pyc | xargs rm -f
	rm -rf bindings/python/build

	# clean what the Makefiles do not clean ...
	rm -rf bindings/perl-piped/blib bindings/perl-shared/blib \
	       bindings/perl-piped/Makefile.old bindings/perl-shared/Makefile.old
	rm -f bindings/perl-shared/MYMETA.yml bindings/perl-shared/MYMETA.json
	rm -f bindings/tcl/pkgIndex.tcl bindings/tcl/tclrrd*.so
	rm -f examples/cgi-demo.cgi
	rm -rf src/.libs src/.deps doc/*.html doc/*.1 doc/*.txt
	rm -f intltool-extract intltool-merge intltool-update
	rm -f config.h

	dh_autoreconf_clean
	dh_clean

clean-python%: unpatch
	set -e ;\
	 cd bindings/python ;\
	 python$* setup.py clean ;\
	 rm -f build-python$*


install: install-arch install-indep

install-indep: build-indep install-common-stamp
	#nothing to do

install-common-stamp: build-arch-stamp
	dh_testdir
	dh_testroot
	dh_clean -k

	make install DESTDIR=$(CURDIR)/debian/tmp
	cd $(CURDIR)/debian/tmp/usr/lib && \
		for file in librrd*; do \
			ln -s "$$file" "`echo $$file | sed -e 's,librrd,librrd_th,g'`"; \
		done; \
	cd -
	# we don't want .txt and .pod documentation in the package
	rm -rf debian/tmp/usr/share/doc/rrdtool/txt
	chmod a+x debian/librrds-perl.install
	dh_install --sourcedir=$(CURDIR)/debian/tmp -X.la --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

	touch $@

install-arch: install-common-stamp $(PY_VERS:%=install-python%)
	#build and install ruby
	dh_ruby --install
	
	dh_installexamples -s
	chmod 644 debian/rrdcached/usr/share/doc/rrdcached/examples/RRDCached.pm

install-python%: install-common-stamp
	dh_testdir
	dh_testroot
	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


binary-indep: build-indep install-indep
	dh_testdir
	dh_testroot
	dh_installchangelogs -i CHANGES
	dh_installdocs -i -A CONTRIBUTORS NEWS
	dh_link -i
	dh_perl -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i
	

binary-arch: build-arch install-arch
	dh_testdir
	dh_testroot
	dh_installchangelogs -a CHANGES
	dh_installdocs -a -A CONTRIBUTORS NEWS
	dh_installdocs -a
	echo 'tcl:Depends=tcl$(TCL_VERS) | tclsh' >> debian/rrdtool-tcl.substvars
	dh_python2 -ppython-rrdtool
	dh_python2 -prrdtool-dbg
	dh_perl -a
	dh_installman -a
	dh_installinit -prrdcached
	dh_link -a
	dh_strip -a --dbg-package=rrdtool-dbg
	dh_compress -a
	dh_fixperms -a
	dh_makeshlibs -a -Nliblua5.2-rrd0 -Nliblua5.2-rrd-dev
	dh_makeshlibs -pliblua5.2-rrd0 -pliblua5.2-rrd-dev -n
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a


binary: binary-arch binary-indep
.PHONY: build clean configure \
        binary-indep binary-arch binary \
        install install-arch \
        install-python% clean-python% unpatch
