#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# Debian package major.minor version
VER = $(shell dpkg-parsechangelog -Sversion | sed -r 's/([0-9]+\.[0-9]+)(\..*)/\1/')

# Closest upstream VCS tag, used to generate debian/control and DH files
VCS_VER = $(shell git describe --abbrev=0 --match="v*" 2>/dev/null | sed -r 's/v([0-9]+\.[0-9]+)(\..*)/\1/')

GNT_USERGROUP_PREFIX="gnt-"

# Set the Python interpreter path, as ganeti will use /usr/bin/python2
# otherwise.

PYTHON := /usr/bin/python
export PYTHON

%:
	dh $@ --with python2,sphinxdoc,bash_completion

ifeq ($(VCS_VER),)
debian/control:
	$(error Empty VCS version, the target must be run within the packaging git repository)
else
debian/control: debian/control.in
	for file in debian/templates/*-VER*; do \
		fname="$$(echo $$file | sed -e 's#VER#$(VCS_VER)#' -e 's#templates/##')"; \
		sed -e 's/@version@/$(VCS_VER)/g' $$file >"$$fname"; \
	done
	
	for package in ganeti-haskell-$(VCS_VER) ganeti-htools-$(VCS_VER) ganeti-$(VCS_VER); do \
		for file in debian/templates/versioned.*; do \
			kind="$$(echo $$file | sed -r 's#.*\.(.*)#\1#')"; \
			sed -e "s/@package@/$$package/g" \
			    -e 's/@version@/$(VCS_VER)/g' \
			    "$$file" > "debian/$${package}.$${kind}"; \
		done; \
	done
	
	sed -s 's/#VER#/$(VCS_VER)/g' debian/control.in > debian/control
endif

clean: debian/control
	dh clean --with python2,sphinxdoc,bash_completion,autoreconf

override_dh_autoreconf:
	dh_autoreconf $(CURDIR)/autogen.sh

override_dh_auto_configure:
	dpkg-parsechangelog -Sversion > vcs-version
	./configure \
	  IP_PATH=/bin/ip \
	  --prefix=/usr \
	  --localstatedir=/var \
	  --sysconfdir=/etc \
	  --with-export-dir=/var/lib/ganeti/export \
	  --with-iallocator-search-path=/usr/local/lib/ganeti/iallocators,/usr/lib/ganeti/iallocators \
	  --with-os-search-path=/srv/ganeti/os,/usr/local/lib/ganeti/os,/usr/lib/ganeti/os,/usr/share/ganeti/os \
	  --with-extstorage-search-path=/srv/ganeti/extstorage,/usr/local/lib/ganeti/extstorage,/usr/lib/ganeti/extstorage,/usr/share/ganeti/extstorage \
	  --docdir=/usr/share/doc/ganeti \
	  --enable-restricted-commands \
	  --disable-symlinks \
	  --with-haskell-flags="-optl -Wl,-z,relro -optl -Wl,--as-needed" \
	  --with-user-prefix=$(GNT_USERGROUP_PREFIX) \
	  --with-group-prefix=$(GNT_USERGROUP_PREFIX) \
	  --with-ssh-initscript="/usr/sbin/invoke-rc.d ssh" \
	  --with-backup-dir="/var/backups"

override_dh_auto_clean:
	[ ! -f Makefile ] || $(MAKE) distclean
	# this is not removed by make distclean :(
	rm -f ganeti

	rm -f vcs-version
	dh_auto_clean

override_dh_install-indep:
	dh_install -pganeti-$(VER) -Xsbin/ganeti-confd -Xganeti/mon-collector \
		-Xganeti/default -Xbin/h -Xman1/h -Xsbin/ganeti-mond -Xsbin/ganeti-luxid -Xhail
	dh_install -i -Nganeti-$(VER) -Xganeti/$(VER)
	
override_dh_python2:
	dh_python2

	# /usr/share/ganeti/<VER> is not in the default search path of dh_python2
	dh_python2 -pganeti-$(VER) usr/share/ganeti/$(VER)

override_dh_missing:
	dh_missing --list-missing

override_dh_compress:
	dh_compress

	# Compress version-specific manpages, not automatically detected by dh_compress
	for package in ganeti-$(VER) ganeti-htools-$(VER); do \
		(cd $(CURDIR)/debian/$$package && \
			find usr/share/ganeti/$(VER)/root/usr/share/man -type f) | \
			xargs dh_compress -p$$package; \
	done

# Disable dh_sphinxdoc for binary-arch, as it will raise an error
override_dh_sphinxdoc-arch:

# Disable the test suite, as it requires the daemon users to be present
# at build time.
override_dh_auto_test:


override_dh_strip:
	dh_strip --no-automatic-dbgsym


.PHONY: override_dh_auto_configure override_dh_auto_clean \
	override_dh_install-indep override_dh_sphinxdoc-arch \
	override_dh_auto_test override_dh_compress override_dh_python2 \
	genfiles override_dh_autoreconf check-vcs-version \
	override_dh_missing override_dh_strip
