#!/usr/bin/make -f

# Verbose mode
#export DH_VERBOSE=1

# Lucid does not have dh_python2, but we would like to be able to use this
# rules file to build on lucid as well. Thus the branching logic.
WITH_PYTHON2 = $(shell test -f /usr/bin/dh_python2 && echo "--with python2")

%:
	dh $@ ${WITH_PYTHON2}

override_dh_auto_clean:
	dh_auto_clean
	rm -rf doc/build/* doc/source/api doc/.autogenerated
	rm -rf tests.sqlite clean.sqlite run_tests.log
	rm -rf CA
	rm -rf nova.egg-info
	rm -f po/nova.pot

debian/nova-compute.postinst: debian/nova-compute.postinst.in
	if dpkg-vendor --derives-from ubuntu ; then \
		LIBVIRTGRP=libvirtd; \
	else \
		LIBVIRTGRP=libvirt; \
	fi; \
	sed -e "s/%LIBVIRTGRP%/$$LIBVIRTGRP/" < $< > $@

override_dh_auto_build: debian/nova-compute.postinst
	dh_auto_build
ifeq (,$(findstring nodocs, $(DEB_BUILD_OPTIONS)))
	python setup.py build_sphinx
else
	mkdir -p $(CURDIR)/doc/build/html
	mkdir -p $(CURDIR)/doc/build/man
	touch $(CURDIR)/doc/build/man/nova-manage.1
endif

get-orig-source:
	uscan --verbose --force-download --rename --destdir=../build-area


UPSTREAM_GIT=git://github.com/openstack/nova.git
DEBIAN_VERSION=$(shell dpkg-parsechangelog | sed -rne 's,^Version: ([^-]+).*,\1,p')
DEBIAN_NAME=nova

get-orig-source-git:
	git clone $(UPSTREAM_GIT) $(DEBIAN_NAME)
	cd $(DEBIAN_NAME) ; \
		git archive master --format=tar.gz \
			--output=../$(DEBIAN_NAME)_$(DEBIAN_VERSION).orig.tar.gz \
			--prefix=$(DEBIAN_NAME)-$(DEBIAN_VERSION)/

override_dh_install:
	dh_install
	chmod 440 $(CURDIR)/debian/nova-common/etc/sudoers.d/nova_sudoers
	install -D -m 0664 $(CURDIR)/debian/nova-volume.default	$(CURDIR)/debian/nova-volume/etc/default/nova-volume
	for hypervisor in qemu kvm xen uml lxc; do \
		install -D -m 0600 $(CURDIR)/debian/nova-compute-$${hypervisor}.conf $(CURDIR)/debian/nova-compute-$${hypervisor}/etc/nova/nova-compute.conf; \
	done

override_dh_fixperms:
	dh_fixperms -Xnova_sudoers
	rm -f $(CURDIR)/debian/python-nova/usr/share/pyshared/nova/CA/.gitignore
	rm -f $(CURDIR)/debian/python-nova/usr/share/pyshared/nova/CA/projects/.gitignore
	rm -f $(CURDIR)/debian/python-nova/usr/share/pyshared/nova/CA/reqs/.gitignore

ifeq (,$(findstring nocheck, $(DEB_BUILD_OPTIONS)))
override_dh_auto_test:
	./run_tests.sh -N
endif

override_dh_installinit:
	if dpkg-vendor --derives-from ubuntu ; then \
		for i in debian/*.upstart.in ; do \
			MYPKG=`echo $$i | cut -d. -f1` ; \
			cp $$MYPKG.upstart.in $$MYPKG.upstart ; \
		done ; \
	fi
	dh_installinit --error-handler=true

override_dh_python2:
	dh_python2
	dh_python2  /usr/lib/xcp

override_dh_installlogrotate:
	dh_installlogrotate
	dh_installlogrotate --name=nova-manage
	dh_installlogrotate --name=nova-dhcpbridge
