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

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

DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

AUTOGENERATED = libgvc6.postrm

ifeq ($(DEB_HOST_ARCH_OS),hurd)
CONFIGURE_LIBS    = LIBS="-Wl,--no-as-needed -Wl,-lpthread -Wl,--as-needed"
endif

ifneq (,$(filter $(DEB_HOST_ARCH), mipsel mips))
DEB_CFLAGS_MAINT_APPEND += "-fno-ipa-sra"
endif

PYTHON_VERSIONS  = $(shell pyversions -r)
PYTHON3_VERSIONS = $(shell py3versions -r)

# sonames for various libraries
SO_CGRAPH	= 6
SO_CDT		= 5
SO_GVC		= 6
SO_PATHPLAN	= 4
SO_XDOT		= 4
SO_GVPR		= 2
SO_LAB-GAMUT	= 1

PHP_EXTENSION_DIR = $(shell php-config --extension-dir)
PHP_PACKAGE       = $(CURDIR)/debian/libgv-php7

override_dh_clean:
	dh_clean
	rm -f $(patsubst %, debian/%, ${AUTOGENERATED})
	rm -f $(CURDIR)/tclpkg/gv/*.pdf $(CURDIR)/cmd/dot/*.pdf
	rm -f $(CURDIR)/configure $(CURDIR)/version.m4
	find $(CURDIR) -name Makefile.in -delete

override_dh_autoreconf:
	$(CURDIR)/autogen.sh NOCONFIG
	dh_autoreconf

override_dh_auto_configure:
	for f in ${AUTOGENERATED} ; do \
		sed -e 's%@TRIPLET@%${DEB_HOST_MULTIARCH}%g' \
			< debian/$$f.in \
			> debian/$$f ; \
	done
	dh_auto_configure -- \
	--disable-silent-rules \
	--prefix=/usr \
	--datadir=\$${prefix}/share \
	--infodir=\$${prefix}/share/info \
	--mandir=\$${prefix}/share/man \
	--disable-static \
	--without-included-ltdl \
	--with-pangocairo \
	--with-x \
	--with-gdk-pixbuf \
	--without-glut \
	--with-gnomeui \
	--with-gtk \
	--without-poppler \
	--with-gts \
	--with-ipsepcola \
	--without-qt \
	--with-rsvg \
	--with-ann \
	--with-webp \
	--disable-go \
	--enable-guile \
	--enable-lua \
	--enable-php \
	--enable-ruby \
	--enable-tcl \
	--disable-java \
	--disable-ocaml \
	--disable-r \
	--disable-sharp \
	--disable-python2 \
	--enable-python3 \
	$(CONFIGURE_LIBS) \
	$(shell $(dpkg_buildflags) --export=configure)

override_dh_auto_test:
	echo Self tests are buggy

override_dh_auto_install:
	dh_auto_install
	find $(CURDIR)/debian/tmp/ -name \*.la -delete
	# Duplicate library
#	rm $(CURDIR)/debian/tmp/usr/lib/tcl*/graphviz/libgv_tcl.so

override_dh_install-arch:
	dh_install --arch
	mv $(CURDIR)/debian/libgvc$(SO_GVC)/usr/sbin/dot \
		$(CURDIR)/debian/libgvc$(SO_GVC)/usr/sbin/libgvc$(SO_GVC)-config-update
	# Symlink dot due to #868546 (debug packages conflict)
	rm $(CURDIR)/debian/graphviz/usr/bin/dot
	ln -s /usr/sbin/libgvc$(SO_GVC)-config-update \
		$(CURDIR)/debian/graphviz/usr/bin/dot
	# NFU
	rm -rf $(CURDIR)/debian/tmp/usr/lib64/

override_dh_install-indep:
	dh_install --indep
	# Remove duplicate changelog
	rm -f $(CURDIR)/debian/graphviz-doc/usr/share/doc/graphviz/ChangeLog

%:
	dh $@ --with python3

.PHONY: override_dh_clean override_dh_autoreconf override_dh_auto_configure \
	override_dh_auto_test override_dh_auto_install \
	override_dh_install-arch override_dh_install-indep
