#!/usr/bin/make -f

SHELL := sh -e

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

%:
	dh ${@} --with autotools_dev

override_dh_auto_clean:
	dh_auto_clean

	for _FILE in debian/*.install.in; \
	do \
		rm -f debian/$$(basename $${_FILE} .in); \
		sed -e 's|@DEB_HOST_MULTIARCH@|$(DEB_HOST_MULTIARCH)|g' \
		$${_FILE} > debian/$$(basename $${_FILE} .in); \
	done

override_dh_auto_configure:
	dh_auto_configure -- --libdir=\$${prefix}/lib/$(DEB_HOST_MULTIARCH)/lxc --with-rootfs-path=\$${prefix}/lib/$(DEB_HOST_MULTIARCH)/lxc --enable-doc

override_dh_auto_install:
	dh_auto_install

	# creating lxc directories
	mkdir -p debian/tmp/etc/lxc/auto
	mkdir -p debian/tmp/etc/lxc/debconf
	mkdir -p debian/tmp/var/log/lxc

	# replacing upstreams debian template
	rm -f debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/lxc/templates/lxc-debian
	ln -s lxc-debconf debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/lxc/templates/lxc-debian
	ln -s lxc-debconf.d debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/lxc/templates/lxc-debian.d

	# adding progress template symlink
	ln -s lxc-debconf debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/lxc/templates/lxc-progress
	ln -s lxc-debconf.d debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/lxc/templates/lxc-progress.d

	# removing currently unsupported lxc-ubuntu until lxc-debconf also supports ubuntu
	rm -f debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/lxc/templates/lxc-ubuntu

	# removing useless files
	rm -f debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/lxc/templates/lxc-busybox
	rm -f debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/lxc/templates/lxc-lenny

override_dh_builddeb:
	dh_builddeb -- -Zgzip -z9

override_dh_compress:
	dh_compress -X.cfg

override_dh_fixperms:
	dh_fixperms

	chmod 0700 debian/lxc/var/log/lxc

override_dh_gencontrol:
ifeq ($(DEB_HOST_ARCH_OS),)
	dh_gencontrol
else
	dh_gencontrol -- -Vmultiarch:Pre-Depends="multiarch-support"
endif

override_dh_install:
	dh_install --fail-missing

override_dh_installinit:
	dh_installinit --no-restart-on-upgrade

override_dh_strip:
	dh_strip --dbg-package=lxc-dbg
