#!/usr/bin/make -f

dist_release := $(shell lsb_release -cs)
ifneq ($(dist_release),dapper)
	use_pycentral = yes
endif
ifeq (,$(filter $(dist_release), hardy lucid))
	use_dhpython2 = yes
endif

dh_extra_flags = -plandscape-common -plandscape-client
ifeq (,$(filter $(dist_release),hardy lucid natty oneiric))
	# We want landscape-client-ui only from precise onward
	dh_extra_flags += -plandscape-client-ui -plandscape-client-ui-install
endif

-include /usr/share/python/python.mk
ifeq (,$(py_sitename))
	py_sitename = site-packages
	py_libdir = /usr/lib/python$(subst python,,$(1))/site-packages
	py_sitename_sh = $(py_sitename)
	py_libdir_sh = $(py_libdir)
endif

package = landscape-client
root_dir = debian/tmp/

revision = $(shell dpkg-parsechangelog | grep ^Version | cut -f 2 -d " "| cut -f 2 -d "-")

landscape_common_substvars = debian/landscape-common.substvars
landscape_client_substvars = debian/landscape-client.substvars

build-arch: build

build-indep: build

build: build-stamp
build-stamp:
	dh_testdir
	sed -i -e "s/^DEBIAN_REVISION = \"\"/DEBIAN_REVISION = \"-$(revision)\"/g" landscape/__init__.py
	python setup.py build
	make -C apt-update
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp
	rm -rf build
	make -C apt-update clean
	dh_clean
	debconf-updatepo
	sed -i -e "s/^DEBIAN_REVISION = .*/DEBIAN_REVISION = \"\"/g" landscape/__init__.py

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs \
		etc/landscape \
		var/lib/landscape \
		var/log/landscape \
		usr/share/landscape

	python setup.py install --root $(root_dir) $(py_setup_install_args)

	install -D -o root -g root -m 755 debian/landscape-sysinfo.wrapper $(root_dir)/usr/share/landscape/landscape-sysinfo.wrapper
	install -D -o root -g root -m 644 debian/cloud-default.conf $(root_dir)/usr/share/landscape/cloud-default.conf
	install -D -o root -g root -m 755 apt-update/apt-update $(root_dir)/usr/lib/landscape/apt-update
	install -D -o root -g root -m 644 dbus/landscape.conf $(root_dir)/etc/dbus-1/system.d/landscape.conf

binary-indep:
# do nothing
#
binary-arch: build install
	dh_lintian
	dh_testdir
	dh_testroot
	dh_installdocs
	dh_installman -p landscape-client man/landscape-client.1 man/landscape-config.1 man/landscape-message.1
	dh_installchangelogs
	dh_install --sourcedir debian/tmp/
	dh_installinit -- start 45 2 3 4 5 . stop 15 0 1 6 .
	dh_installlogrotate
	dh_installdebconf
	dh_strip
	dh_compress
	dh_fixperms
	dh_shlibdeps

ifneq (,$(findstring $(dist_release),hardy))
	# We depend on bug-fixed versions of python-dbus and pycurl on hardy
	echo "extra:Depends=python-dbus" >> $(landscape_common_substvars)
	echo "extra:Depends=python-pycurl, hal" >> $(landscape_client_substvars)
	# The python-image-store-proxy package is needed for the eucalyptus plugin
	echo "extra:Suggests=python-image-store-proxy" >> $(landscape_common_substvars)
endif
ifneq (,$(filter $(dist_release),lucid))
	# We want libpam-modules in lucid
	echo "extra:Depends=libpam-modules (>= 1.0.1-9ubuntu3), python-dbus" >> $(landscape_common_substvars)
	echo "extra:Depends=python-pycurl, hal" >> $(landscape_client_substvars)
endif
ifeq (,$(filter $(dist_release),hardy lucid))
	# Starting natty, no more hal or dbus
	echo "extra:Depends=libpam-modules (>= 1.0.1-9ubuntu3)" >> $(landscape_common_substvars)
	echo "extra:Depends=python-pycurl, gir1.2-gudev-1.0 (>= 165-0ubuntu2), python-gi" >> $(landscape_client_substvars)
	echo "extra:Suggests=python-dbus, hal" >> $(landscape_client_substvars)
endif

ifeq ($(use_dhpython2),yes)
	dh_python2 --no-guessing-versions
else
    ifeq ($(use_pycentral),yes)
        ifneq (,$(py_setup_install_args))
			DH_PYCENTRAL=include-links dh_pycentral
        else
			DH_PYCENTRAL=nomove dh_pycentral
        endif
    else
		dh_python
    endif
endif
	dh_installdeb $(dh_extra_flags)
	dh_gencontrol $(dh_extra_flags)
	dh_md5sums $(dh_extra_flags)
	dh_builddeb $(dh_extra_flags)


binary: binary-arch binary-indep

.PHONY: binary binary-arch binary-indep clean
