#!/usr/bin/make -f

# export DH_VERBOSE=1

PYTHONS        := $(filter-out python2.5, $(shell pyversions --installed; py3versions --installed))
PYTHON_DEFAULT := $(shell pyversions --default)
RUBIES         := $(shell find /usr/bin/ -name 'ruby[12]*' | xargs -n1 readlink -f | xargs -n1 basename)
RUBY_DEFAULT   := $(shell which ruby | xargs readlink -f | xargs basename)
LUAS           := $(shell find /usr/bin/ -name 'lua5.?' | xargs -n1 readlink -f | xargs -n1 basename)
LUA_DEFAULT    := $(shell which lua | xargs readlink -f | xargs basename)

BUILDS         := $(filter-out $(PYTHON_DEFAULT), $(PYTHONS)) \
		  $(filter-out $(RUBY_DEFAULT), $(RUBIES)) \
		  $(filter-out $(LUA_DEFAULT), $(LUAS)) \
		  default

DEB_HOST_GNU_CPU := $(shell dpkg-architecture -qDEB_HOST_GNU_CPU | sed -e 's,i[456]86,i386,')
QEMU_SYSTEM      := qemu-system-$(DEB_HOST_GNU_CPU)

DEFAULT_CONFIG_FLAGS = \
	--disable-appliance        \
	--with-readline	           \
	--disable-haskell          \
	--disable-php	           \
	--enable-gtk-doc           \
	--with-qemu=$(QEMU_SYSTEM)

BARE_CONFIG_FLAGS = \
	$(DEFAULT_CONFIG_FLAGS) \
	--disable-daemon --disable-fuse \
	--disable-ocaml --disable-perl \
	--disable-erlang --with-java=no \
	--disable-gobject --without-java

override_dh_auto_clean:
	set -e; for p in $(BUILDS); do \
		dh_auto_clean --builddir=$(CURDIR)/debian/build-$$p ; \
	done

override_dh_auto_configure:
	# Make sure that the file is generated in builddir from the template.
	rm -f ruby/ext/guestfs/extconf.rb
	dh_auto_configure --builddir=$(CURDIR)/debian/build-default \
		-- \
		$(DEFAULT_CONFIG_FLAGS) \
		--enable-install-daemon \
		--with-java=/usr/lib/jvm/default-java \
		RUBY=$(RUBY_DEFAULT) \
		RAKE=$(patsubst ruby%,rake%,$(RUBY_DEFAULT)) \
		PYTHON=$(PYTHON_DEFAULT) \
		LUA=$(LUA_DEFAULT) \
		JNI_INSTALL_DIR='$${libdir}/jni'

	set -e; for v in $(patsubst ruby%,%,$(filter-out $(RUBY_DEFAULT), $(RUBIES))) \
	; do \
		if [ $$v = 1.8 ]; then rake=rake; else rake=rake$$v; fi ; \
		dh_auto_configure --builddir=$(CURDIR)/debian/build-ruby$$v \
			-- \
			$(BARE_CONFIG_FLAGS) \
			--disable-lua \
			--disable-python \
			RUBY=ruby$$v RAKE=$$rake ; \
	done

	set -e; for p in $(filter-out $(PYTHON_DEFAULT), $(PYTHONS)) \
	; do \
		dh_auto_configure --builddir=$(CURDIR)/debian/build-$$p \
			-- \
			$(BARE_CONFIG_FLAGS) \
			--disable-lua \
			--disable-ruby \
			PYTHON=$$p ; \
	done

	set -e; for p in $(filter-out $(LUA_DEFAULT), $(LUAS)) \
	; do \
		dh_auto_configure --builddir=$(CURDIR)/debian/build-$$p \
			-- \
			$(BARE_CONFIG_FLAGS) \
			--disable-python \
			--disable-ruby \
			LUA=$$p ; \
	done

override_dh_strip:
	dh_strip --dbg-package=libguestfs0-dbg

override_dh_auto_build:
	set -e; for p in $(BUILDS); do \
		dh_auto_build --builddir=$(CURDIR)/debian/build-$$p \
			-- INSTALLDIRS=vendor LD_RUN_PATH="" \
			   EXTRA_JAVAC_FLAGS+="-source 1.6 -target 1.6" ; \
	done

	# also build part of appliance
	$(MAKE) -C $(CURDIR)/debian/build-default/appliance \
		make.sh \
		packagelist excludelist \
		supermin.d/daemon.img.gz \
		supermin.d/init.img \
		supermin.d/udev-rules.img

override_dh_auto_test:
	set -e; for p in $(BUILDS); do \
		dh_auto_test --builddir=$(CURDIR)/debian/build-$$p \
			-- CFLAGS="$(filter-out -Werror=format-security,$(CFLAGS))"; \
	done

override_dh_auto_install:
# All build trees contain the library plus guestfish and C virt tools.
# We assume that the Python version used for building the Python
# bindings doesn't affect the outcome of those builds.
	set -e; for p in $(BUILDS); do \
		dh_auto_install --builddir=$(CURDIR)/debian/build-$$p \
			-- INSTALLDIRS=vendor; \
	done

override_dh_install:
# We do not install supermin.d/base.img, but ship make.sh which can be
# used to build it.
	mkdir -p debian/tmp/usr/lib/guestfs/supermin.d
	install -m755 debian/build-default/appliance/make.sh \
		debian/tmp/usr/lib/guestfs
	install -m644 debian/build-default/appliance/*list \
		debian/tmp/usr/lib/guestfs
	install -m644 \
		debian/build-default/appliance/supermin.d/*.img* \
		debian/tmp/usr/lib/guestfs/supermin.d/

	erlang-depends -perlang-guestfs

	dh_install -X.la -X.so.owner -Xbindtests -X/usr/lib/go/ \
		--fail-missing

override_dh_python3:
	dh_python3 -p python3-guestfs
override_dh_python2:
	dh_python2 -p python-guestfs -V 2.6-

# Workaround for Lintian warning
# "{pre,post}inst-has-useless-call-to-ldconfig"
override_dh_makeshlibs:
	dh_makeshlibs -X/jni/ -X/python2/ -X/python3/

%:
	dh $@ \
	   --parallel \
	   --without=python-support \
	   --with=autoreconf,ocaml,python2,python3,bash-completion,ruby,gir,lua
