#!/usr/bin/make -f

libvteN := $(shell sed -rn 's/^Package:[[:space:]]*(libvte[0-9]+)[[:space:]]*$$/\1/p' debian/control)
libvteNgtk3 := $(shell sed -rn 's/^Package:[[:space:]]*(libvte-2.90-[0-9]+)[[:space:]]*$$/\1/p' debian/control)
udeb := $(libvteN)-udeb

buildbasedir := $(CURDIR)/debian/build
DEB_BUILDDIR := $(buildbasedir)/main
DEB_BUILDDIR_$(udeb) := $(buildbasedir)/udeb

include /usr/share/cdbs/1/rules/debhelper.mk
include /usr/share/cdbs/1/rules/utils.mk
include /usr/share/cdbs/1/class/gnome.mk
include /usr/share/cdbs/1/rules/autoreconf.mk
include /usr/share/gnome-pkg-tools/1/rules/uploaders.mk
-include /usr/share/gnome-pkg-tools/1/rules/gnome-get-source.mk

PY_VERSIONS = $(shell pyversions --requested debian/control)

DEB_CONFIGURE_LIBEXECDIR := \$${prefix}/lib/$(libvteN)
DEB_FIXPERMS_EXCLUDE += gnome-pty-helper
DEB_DH_MAKESHLIBS_ARGS_libvte9 += -V '$(libvteN) (>= 1:0.24.0)' --add-udeb=$(udeb)

DEB_CONFIGURE_EXTRA_FLAGS += \
	--enable-glade-catalogue \
	--enable-introspection

UDEB_CONFIGURE_EXTRA_FLAGS += \
	--disable-python \
	--disable-gnome-pty-helper \
	--disable-gtk-doc \
	--without-ncurses

DEB_DESTDIR := $(CURDIR)/debian/tmp/main
DEB_DESTDIR_$(udeb) := $(CURDIR)/debian/tmp/udeb

CFLAGS_$(udeb) += $(CFLAGS) -Os

DEB_DH_INSTALL_ARGS = --sourcedir=$(if $(findstring $(udeb),$(cdbs_curpkg)),$(DEB_DESTDIR_$(udeb)),$(DEB_DESTDIR))

binary-predeb/gir%::
	dh_girepository -p$(cdbs_curpkg)

######
# gtk3 rules
gtk3-configure-stamp:
	: # configure for GTK+ 3.0
	set -e; \
	rm -rf $(buildbasedir)/gtk3; \
	mkdir $(buildbasedir)/gtk3; \
	cd $(buildbasedir)/gtk3; \
	$(DEB_CONFIGURE_SCRIPT_ENV) $(DEB_CONFIGURE_SCRIPT) \
	  $(filter-out --srcdir=%, $(DEB_CONFIGURE_NORMAL_ARGS) \
	                           $(cdbs_configure_flags) \
	                           $(DEB_CONFIGURE_EXTRA_FLAGS) \
	                           $(DEB_CONFIGURE_USER_FLAGS)) \
	  --with-gtk=3.0 --libexecdir="\$${prefix}/lib/$(libvteNgtk3)"
	touch $@
gtk3-build-stamp: gtk3-configure-stamp
	: # build for GTK+ 3.0
	$(MAKE) -C $(buildbasedir)/gtk3
	touch $@
gtk3-install-stamp: gtk3-build-stamp
	: # install for GTK+ 3.0
	$(MAKE) -C $(buildbasedir)/gtk3 install DESTDIR=$(DEB_DESTDIR)
	touch $@
common-configure-arch:: gtk3-configure-stamp
common-build-arch:: gtk3-build-stamp
common-install-prehook-indep:: gtk3-install-stamp
common-install-prehook-arch:: gtk3-install-stamp
clean::
	rm -f gtk3-*-stamp

######
# udeb rules
configure/$(udeb):: $(DEB_BUILDDIR_$(udeb))/config.status
$(DEB_BUILDDIR_$(udeb))/config.status:
	cd $(DEB_BUILDDIR_$(udeb)) && \
		$(DEB_CONFIGURE_SCRIPT_ENV) \
		CFLAGS="$(CFLAGS_$(udeb))" \
			$(DEB_CONFIGURE_SCRIPT) \
				$(DEB_CONFIGURE_NORMAL_ARGS) \
				$(cdbs_configure_flags) \
				$(DEB_CONFIGURE_EXTRA_FLAGS) \
				$(UDEB_CONFIGURE_EXTRA_FLAGS) \
				$(DEB_CONFIGURE_USER_FLAGS)

build/$(udeb)::
	make -C $(DEB_BUILDDIR_$(udeb))

install/$(udeb)::
	make -C $(DEB_BUILDDIR_$(udeb)) install DESTDIR=$(DEB_DESTDIR_$(udeb))


######
# Python rules
configure-stamp-%:
	mkdir $(buildbasedir)/$*
	cd $(buildbasedir)/$* && PYTHON=`which $*` $(DEB_CONFIGURE_SCRIPT_ENV) \
	    $(DEB_CONFIGURE_SCRIPT) \
		$(DEB_CONFIGURE_NORMAL_ARGS) \
		$(cdbs_configure_flags) \
		$(DEB_CONFIGURE_EXTRA_FLAGS) \
		$(DEB_CONFIGURE_USER_FLAGS)
	touch $@

configure/python-vte:: $(addprefix configure-stamp-, $(PY_VERSIONS))

build-stamp-%:
	make -C $(buildbasedir)/$* PYTHON=`which $*`
	touch $@

build/python-vte:: $(addprefix build-stamp-, $(PY_VERSIONS))

install-stamp-%:
	PYTHON=`which $*` make -C $(buildbasedir)/$* install DESTDIR=$(DEB_DESTDIR)
	# stop shipping *.a and *.la files for the Python module;
	# python-support moves them anyway (#412477)
	find $(DEB_DESTDIR)/usr/lib/$* -name \*.a -exec rm -f '{}' \;
	find $(DEB_DESTDIR)/usr/lib/$* -name \*.la -exec rm -f '{}' \;
	touch $@

install/python-vte:: $(addprefix install-stamp-, $(PY_VERSIONS))

binary-install/python-vte::
	dh_pysupport -ppython-vte


######
# common cleanup
clean::
	-rm -rf $(buildbasedir)
	-rm -rf $(addprefix configure-stamp-, $(PY_VERSIONS))
	-rm -rf $(addprefix build-stamp-, $(PY_VERSIONS))
	-rm -rf $(addprefix install-stamp-, $(PY_VERSIONS))

common-binary-fixup-arch::
	t=libvte-common; \
	for p in $$(dh_listpackages -a -N$$t); do \
	  rm -rf debian/$$p/usr/share/doc/$$p; \
	  ln -s $$t debian/$$p/usr/share/doc/$$p; \
	  if [ -f debian/$$p.docs ]; then \
	    mkdir -p debian/$$p/usr/share/doc/$$t; \
	    cp -a $$(eval echo $$(cat debian/$$p.docs)) debian/$$p/usr/share/doc/$$t/; \
	    dh_compress -p$$p; \
	  fi; \
	  ( \
	    echo "# doc dir is now a symlink to $$p"; \
	    echo 'if [ "$$1" = upgrade ] && [ ! -L /usr/share/doc/'$$p' ]; then'; \
	    echo "    rm -rf /usr/share/doc/$$p"; \
	    echo "fi"; \
	  ) >> debian/$$p.preinst.debhelper; \
	done
