#!/usr/bin/make -f
#-*- makefile -*-
# Made with the aid of dh_make, by Craig Small
# Sample debian/rules that uses debhelper. GNU copyright 1997 by Joey Hess.
# This version is for a hypothetical package that builds an
# architecture-dependant package, as well as an architecture-independant
# package.

package:=rrdtool

top_srcdir:=$(shell pwd)
tmp:=$(top_srcdir)/debian/tmp

# TCL installation stuff (must match tcl/Makefile.am)
tclconfigdir = /usr/lib/tcl8.4

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

# Defaults
CFLAGS := -O2

# Handle DEB_BUILD_OPTIONS
ifneq "$(findstring debug,$(DEB_BUILD_OPTIONS))" ""
CFLAGS += -g
endif

CFLAGS += -I/usr/include/tcl8.4

PYVERS	:= $(shell pyversions -vr debian/control)
PYVER   := $(shell python -c 'import sys; print sys.version[:3]')
version	:= 1.2.11

configure: configure-stamp
configure-stamp:
	dh_testdir
	# Make sure we can find tcl stuff (configure won't fail)
	test -f $(tclconfigdir)/tclConfig.sh

	# Configure C and Tcl stuff
	CFLAGS="$(CFLAGS)" ./configure \
		--prefix=/usr \
		--mandir=/usr/share/man \
		--enable-shared=yes \
		--enable-static=yes \
		--with-tcllib=$(tclconfigdir) \
		--with-perl-options=INSTALLDIRS=vendor \
		--with-rrd-default-font=/usr/share/fonts/truetype/ttf-dejavu/DejaVuSansMono.ttf

#	# Configure Perl stuff
#	cd bindings/perl-piped && \
#		rm -f Makefile && \
#		perl Makefile.PL INSTALLDIRS=vendor
#
#	cd bindings/perl-shared && \
#		rm -f Makefile && \
#		perl Makefile.PL INSTALLDIRS=vendor

	touch configure-stamp

build: build-stamp $(PYVERS:%=build-python%)
build-stamp: configure-stamp
	dh_testdir

	# Build most stuff
	$(MAKE)

#	# We now build perl-piped and perl-shared separately
#	cd perl-piped && make OPTIMIZE="$(CFLAGS)"
#	cd perl-shared && make OPTIMIZE="$(CFLAGS)"

	touch build-stamp

build-python%:
	cd bindings/python && python$* setup.py build

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp

	# Add here commands to clean up after the build process.
	-$(MAKE) distclean

	-rm doc/RRDp.pm
	-rm doc/RRDs.pm
#	-rm perl-piped/Makefile.old perl-piped/Makefile
#	-rm perl-shared/Makefile.old perl-shared/Makefile

	rm -rf bindings/python/build
	rm -f bindings/tcl/*.so
	rm -f bindings/*/Makefile.old

	rm -f debian/shlibs.local
	dh_clean

install: build-stamp
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	# Add here commands to install the package into debian/tmp.
	$(MAKE) install DESTDIR=$(tmp)
	chmod -R u+w debian/tmp

	mv debian/tmp/usr/share/doc/rrdtool-$(version) \
		debian/tmp/usr/share/doc/rrdtool
	mv debian/tmp/usr/share/rrdtool/examples \
		debian/tmp/usr/share/doc/rrdtool/

#	# Things installed in the wrong places
#	rm -rf $(tmp)/usr/contrib

#	# trytime should be installed as source
#	make -C contrib/trytime clean

#	# Since you have to compile this stuff from source, I consider it
#	# examples
#	for example in $(top_srcdir)/contrib/[a-z]*; do \
#		cp -R $$example $(tmp)/usr/share/doc/rrdtool/examples; \
#	done

	find $(tmp)/usr/share/doc/rrdtool/examples \
		-name CVS -o -name .cvsignore \
		| xargs --no-run-if-empty rm -rf

	# Install our trytime manpage
#	install $(top_srcdir)/debian/trytime.1 $(tmp)/usr/share/man/man1

#	# Install Perl stuff manually
#	cd perl-piped && make install PREFIX=$(tmp)/usr
#	cd perl-shared && make install PREFIX=$(tmp)/usr
#	-find $(tmp) -name ntmake.pl | xargs --no-run-if-empty rm -f

	cd bindings/python; \
	for v in $(PYVERS); do \
	  python$$v setup.py install --root $(CURDIR)/debian/python-rrd; \
	done

	dh_install

# Build architecture-independent files here.
binary-indep: build install
#	dh_testversion
	dh_testdir -i
	dh_testroot -i
	dh_installdocs -i
#	dh_installexamples -i
	dh_installchangelogs -i CHANGES
	echo 'rrdtool example files can be found in the rrdtool package' \
	  > debian/librrdp-perl/usr/share/doc/librrdp-perl/README.examples
	dh_link -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_perl -i
	dh_python -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build install
#	dh_testversion
	dh_testdir -a
	dh_testroot -a
	dh_installdocs -a NEWS README THREADS TODO
	dh_installdocs -a -A NEWS
#	dh_installexamples -a
	echo 'rrdtool example files can be found in the rrdtool package' \
	  > debian/librrds-perl/usr/share/doc/librrds-perl/README.examples
	dh_installchangelogs -a CHANGES
ifeq "$(findstring nostrip,$(DEB_BUILD_OPTIONS))" ""
	dh_strip -a
endif
	dh_link -a
	dh_compress -a -Xexamples/
	dh_fixperms -a
	dh_makeshlibs -a
	dh_installdeb -a
	dh_perl -a
	dh_python -a
	sed 's/librrd2$$/librrd2 (>= $${Source-Version})/' \
		debian/librrd2/DEBIAN/shlibs > debian/shlibs.local
	dh_shlibdeps -a -ldebian/librrd2/usr/lib
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

source diff:                                                                  
	@echo >&2 'source and diff are obsolete - use dpkg-source -b'; false

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