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

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

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

AUTOGENERATED = graphviz.postinst graphviz.postrm graphviz.triggers libgvc6.postinst

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

N =

ifeq ($(DEB_HOST_ARCH),ia64)
RUBY = --disable-ruby
N += -Nlibgv-ruby
else
RUBY = --enable-ruby
endif

ifneq ($(DEB_BUILD_ARCH),$(DEB_HOST_ARCH))
PERLVER := $(shell perl -MConfig -e 'print $$Config{version}')
export PERL5LIB := /usr/lib/$(DEB_HOST_MULTIARCH)/perl/cross-config-$(PERLVER)
endif

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

override_dh_clean:
	dh_clean
	$(RM) $(patsubst %, debian/%, ${AUTOGENERATED})
	$(RM) $(CURDIR)/tclpkg/gv/*.pdf $(CURDIR)/cmd/dot/*.pdf
	$(RM) $(CURDIR)/tclpkg/gv/gv.3*
	$(RM) $(CURDIR)/configure $(CURDIR)/version.m4
	$(RM) -r $(CURDIR)/public/
	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 \
	--enable-static \
	--without-included-ltdl \
	--with-pangocairo \
	--with-x \
	--with-gdk-pixbuf \
	--with-glut \
	--with-gnomeui \
	--without-gtk \
	--with-poppler \
	--with-gts \
	--with-ipsepcola \
	--with-qt \
	--with-rsvg \
	--with-ann \
	--with-webp \
	--with-devil \
	--without-smyrna \
	--disable-go \
	--enable-guile \
	--enable-lua \
	--disable-php \
	$(RUBY) \
	--enable-tcl \
	--disable-java \
	--disable-ocaml \
	--disable-r \
	--disable-sharp \
	--disable-python \
	--enable-python3 \
	$(CONFIGURE_LIBS) \
	$(shell $(dpkg_buildflags) --export=configure)

override_dh_auto_build-indep:
	dh_auto_build --indep
	# still need registered layout module
	PATH=./cmd/dot/:$(PATH) $(MAKE) doxygen

override_dh_auto_install:
	dh_auto_install
	find $(CURDIR)/debian/tmp/ -name \*.la -delete
	# move Tcl files to correct path
	install -d $(CURDIR)/debian/tmp/usr/lib/tcltk/$(DEB_HOST_MULTIARCH)/
	mv $(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/graphviz/tcl \
		$(CURDIR)/debian/tmp/usr/lib/tcltk/$(DEB_HOST_MULTIARCH)/gv
	# Duplicate library
#	rm $(CURDIR)/debian/tmp/usr/lib/tcl*/graphviz/libgv_tcl.so

override_dh_install-arch:
	dh_install --arch
	# NFU
	rm -rf $(CURDIR)/debian/tmp/usr/lib64/

#override_dh_auto_test:
#	dh_auto_test
#	pytest-3

override_dh_missing:
	dh_missing --list-missing

%:
	dh $@ --with python3 $(N)

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