#!/usr/bin/make -f

# export DH_VERBOSE=1

PYTHONS        := $(filter-out python2.5, $(shell pyversions --installed; py3versions --installed))
PYTHON_DEFAULT := $(shell pyversions --default)

COMMON_CONFIG_FLAGS = \
	--disable-appliance  \
	--with-readline	     \
	--disable-ruby       \
	--disable-haskell    \
	--disable-php

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

override_dh_auto_configure:
	set -e; for p in $(PYTHONS); do \
	    if [ $$p = $(PYTHON_DEFAULT) ]; then \
		dh_auto_configure --builddir=$(CURDIR)/debian/build-$$p \
		    -- \
		    $(COMMON_CONFIG_FLAGS) \
		    --enable-install-daemon \
		    --with-java-home=/usr/lib/jvm/default-java ; \
	    else \
	        dh_auto_configure --builddir=$(CURDIR)/debian/build-$$p \
	            -- \
		    $(COMMON_CONFIG_FLAGS) \
	            --disable-daemon --disable-fuse \
	            --disable-ocaml --disable-perl \
		    --disable-erlang --with-java-home=no \
	            PYTHON=$$p ; \
	    fi ; \
	done

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

override_dh_auto_build:
# also build part of appliance
	set -e; for p in $(PYTHONS); do \
	    if [ $$p = $(PYTHON_DEFAULT) ]; then \
		dh_auto_build --builddir=$(CURDIR)/debian/build-$$p \
		    -- \
		    INSTALLDIRS=vendor LD_RUN_PATH="" ; \
	        $(MAKE) -C $(CURDIR)/debian/build-$$p/appliance \
		    make.sh \
		    packagelist excludelist \
		    supermin.d/daemon.img supermin.d/init.img ; \
	    else \
		dh_auto_build --builddir=$(CURDIR)/debian/build-$$p ; \
	    fi ; \
	done

override_dh_auto_test:
	set -e; for p in $(PYTHONS); do \
		dh_auto_test --builddir=$(CURDIR)/debian/build-$$p ; \
	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 $(PYTHONS); do \
		dh_auto_install --builddir=$(CURDIR)/debian/build-$$p ; \
	done

override_dh_install:
	rm -f debian/tmp/usr/lib/python*/dist-packages/libguestfsmod.la

# Workaround for:
# W: libguestfs-perl: script-not-executable usr/lib/perl5/Sys/bindtests.pl
	rm -f debian/tmp/usr/lib/perl5/Sys/bindtests.pl

# Workaround for:
# E: libguestfs-dev: non-empty-dependency_libs-in-la-file usr/lib/libguestfs.la
	find debian/tmp -name *.la | xargs sed -i '/^dependency_libs/ s/^/# /'

# 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-${PYTHON_DEFAULT}/appliance/make.sh \
		debian/tmp/usr/lib/guestfs
	install -m644 debian/build-${PYTHON_DEFAULT}/appliance/*list \
		debian/tmp/usr/lib/guestfs
	install -m644 \
		debian/build-${PYTHON_DEFAULT}/appliance/supermin.d/init.img \
		debian/build-${PYTHON_DEFAULT}/appliance/supermin.d/daemon.img \
		debian/tmp/usr/lib/guestfs/supermin.d/

	erlang-depends -perlang-guestfs
	dh_install

# Workaround for python3-defaults < 3.2~b2-1
# ("do not rename .so files outside dist-packages directory")
override_dh_python3:
	dh_python3 -p python-libguestfs
override_dh_python2:
	dh_python2 -p python-libguestfs -V 2.6-

%:
	dh --without=python-support --with=autotools-dev,ocaml,python2,python3,bash-completion $@
