#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

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

include /usr/share/quilt/quilt.make

# Get build platform info
export DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
export DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

UPSTREAM_VERSION  = $(shell dpkg-parsechangelog | perl -ne 'print $$1 if /^Version: (.*?)-(.*)$$/')
SONAME            = 4

OCAMLABI          = $(shell ocamlc -version)
OCAML_PACKAGE     = $(CURDIR)/debian/libgv-ocaml

PHP_EXTENSION_DIR = $(shell php-config5 --extension-dir)
PHP_PACKAGE       = $(CURDIR)/debian/libgv-php5

LUA_VERSION       = 5.1
LUA_PACKAGE       = $(CURDIR)/debian/libgv-lua

DEV_PACKAGE       = $(CURDIR)/debian/libgraphviz-dev

PYTHON_VERSIONS   = $(shell pyversions -s)
PYTHON_PACKAGE    = $(CURDIR)/debian/libgv-python

RUBY_VERSION      = 1.8
RUBY_PACKAGE      = $(CURDIR)/debian/libgv-ruby

CONFIG_SCRIPTS=config/config.guess lib/gd/config/config.guess libltdl/config.guess \
	       config/config.sub   lib/gd/config/config.sub   libltdl/config.sub

configure: patch configure-stamp
configure-stamp:
	dh_testdir

	# Update the config scripts
	for i in $(CONFIG_SCRIPTS); do \
		cp /usr/share/misc/`basename $$i` $$i ; \
	done

	# Configure the package
	./configure --build=$(DEB_BUILD_GNU_TYPE) --host=$(DEB_HOST_GNU_TYPE) \
	--prefix=/usr \
	--datadir=\$${prefix}/share \
	--infodir=\$${prefix}/share/info \
	--mandir=\$${prefix}/share/man \
	--disable-static \
	--with-pangocairo \
	--with-x \
	--with-gd=no \
	--with-gdk-pixbuf \
	--without-ghostscript \
	--without-gtk \
	--without-gtkgl \
	--without-gtkglext \
	--without-glade \
	--with-ipsepcola \
	--with-lasi \
	--with-rsvg \
	--with-devil \
	--with-gts \
	--without-ortho \
	--without-sfdp \
	--without-smyrna \
	--disable-sharp \
	--disable-java \
	--disable-io \
	--disable-python \
	--disable-r \
	LDFLAGS="-Wl,--as-needed"
	touch configure-stamp

build: configure build-stamp
build-stamp:
	dh_testdir

	# Build
	$(MAKE)
	touch build-stamp

#	# Build the manpage from SONAME-independent perlpod
#	sed -e 's/##SONAME##/$(SONAME)/g' \
#		debian/libgraphviz-config-update.pod.in > \
#		debian/libgraphviz$(SONAME)-config-update.pod
#	pod2man --center "GRAPHVIZ CONFIGURATION MANAGEMENT" \
#		--release $(UPSTREAM_VERSION) \
#		debian/libgraphviz$(SONAME)-config-update.pod \
#		debian/libgraphviz$(SONAME)-config-update.1

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

	# Clean up if a build already happened
	[ ! -f Makefile ] || $(MAKE) -k distclean

#	# Clean up intermediate and generated manpages
#	rm -f debian/libgraphviz$(SONAME)-config-update.pod
#	rm -f debian/libgraphviz$(SONAME)-config-update.1

	dh_clean $(CONFIG_SCRIPTS) \
		tclpkg/gv/gv.R tclpkg/gv/php_gv.h

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	# Install the package into debian/tmp
	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp

	# Remove .la files
	rm -rf `find -name '*.la'`

	# Install lintian overrides
	install -d $(CURDIR)/debian/tmp/usr/share/lintian/overrides
	install -m 644 $(CURDIR)/debian/libgraphviz$(SONAME).lintian-overrides \
		$(CURDIR)/debian/tmp/usr/share/lintian/overrides/libgraphviz$(SONAME)

	# Compute the dependencies of the -dev package
	# NOTE: It is important to do that before splitting the files into
	# their respective packages, otherwise the symlinks are broken
	d-devlibdeps \
		--override s/libpathplan$(SONAME)-dev// \
		--override s/libcgraph$(SONAME)-dev//   \
		--override s/libgraph$(SONAME)-dev//    \
		--override s/libcdt$(SONAME)-dev//      \
		--override s/libgts-0.7-5-dev/libgts-dev/ \
		$(CURDIR)/debian/libgraphviz-dev.substvars \
		$(CURDIR)/debian/tmp/usr/lib/*.so

	# Move from debian/tmp to the appropriate packages, rename one binary
	dh_install --sourcedir=debian/tmp --list-missing
	mv $(CURDIR)/debian/libgraphviz$(SONAME)/usr/sbin/dot \
		$(CURDIR)/debian/libgraphviz$(SONAME)/usr/sbin/libgraphviz$(SONAME)-config-update

# Build architecture-independent files here.
binary-indep: build install
	dh_testdir -i
	dh_testroot -i
	dh_installdocs -i
	dh_installmenu -i
	dh_installchangelogs ChangeLog -i
	dh_perl -i
	dh_link -i
	dh_strip -i
	dh_compress -i -X.pdf
	dh_fixperms -i
	dh_makeshlibs -i
	dh_installdeb -i
	dh_shlibdeps -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
# Pass -a to all debhelper commands in this target to reduce clutter.
binary-arch: build install
	dh_testdir -a
	dh_testroot -a
	dh_installdocs -a -XMakefile
	dh_installexamples -a
	dh_installmenu -a
	dh_installman -a
	dh_installchangelogs ChangeLog -a
	dh_perl -a
	dh_link -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
	dh_makeshlibs -plibgraphviz$(SONAME) -V 'libgraphviz4 (>= 2.18)'
	dh_installdeb -a
	dh_shlibdeps -a -L libgraphviz$(SONAME) $(CURDIR)/debian/libgraphviz$(SONAME)/usr/lib
	dh_gencontrol -a -- -VF:OCamlABI="$(OCAMLABI)"
	dh_md5sums -a
	dh_builddeb -a

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