#!/usr/bin/make -f


# Make sure not to generate manpages with a non-C locale date format:
export LANG=C


prepare-changelog: LAST=$(shell git describe | sed 's/-.*//')
prepare-changelog: BRANCH=$(shell git branch | grep '^*' | awk '{print $$2}')
prepare-changelog:
	# Make sure git-buildpackage is installed (ensures git-core is, too):
	@if [ -z "$(shell which git-dch)" ] ; then \
		echo "E: git-buildpackage isn't installed, needed for this target." ; \
		exit 1 ; \
	fi

	# Determine last tag:
	git dch --debian-branch=$(BRANCH) --since=$(LAST)


source-package: PACKAGE=module-assistant
source-package: VERSION=$(shell dpkg-parsechangelog | grep ^Version: | awk '{print $$2}')
source-package:
	# Make sure git-core is installed:
	@if [ -z "$(shell which git)" ] ; then \
		echo "E: git-core isn't installed, needed for this target." ; \
		exit 1 ; \
	fi

	# Generate the tarball with an appropriate prefix:
	git archive --prefix=$(PACKAGE)-$(VERSION)/ HEAD | gzip -9 \
		> ../$(PACKAGE)_$(VERSION).tar.gz ; echo $$?

	# Extract it, build the source package, clean afterwards
	# (also clean before, there might be something already):
	cd .. && \
		rm -rf $(PACKAGE)-$(VERSION) && \
		tar xfz $(PACKAGE)_$(VERSION).tar.gz && \
		cd $(PACKAGE)-$(VERSION) && \
		debuild -us -uc -S && \
		cd .. && \
		rm -rf $(PACKAGE)-$(VERSION)


pristine-tar-commit: PACKAGE=module-assistant
pristine-tar-commit: VERSION=$(shell dpkg-parsechangelog | grep ^Version: | awk '{print $$2}')
pristine-tar-commit: TARBALL=../$(PACKAGE)_$(VERSION).tar.gz
pristine-tar-commit: BRANCH=$(shell git branch | grep '^*' | awk '{print $$2}')
pristine-tar-commit:
	# Make sure pristine-tar is installed:
	@if [ -z "$(shell which pristine-tar)" ] ; then \
		echo "E: pristine-tar isn't installed, needed for this target." ; \
		exit 1 ; \
	fi

	# Make sure the tarball exists:
	@if [ ! -f $(TARBALL) ] ; then \
		echo "E: $(TARBALL) tarball is missing." ; \
		echo "I: Need to use the 'source-package' target first?" ; \
		exit 1 ; \
	fi

	# Commit the tarball:
	pristine-tar commit $(TARBALL) $(VERSION)


build: build-stamp
build-stamp:
	dh build

	# Build the templates tarball:
	tar cfj templates-debian-dir.tar.bz2 templates

	# Build the translations:
	$(MAKE) -C po

	touch build-stamp


clean:
	dh clean

	# Clean the tarball:
	rm -rf templates-debian-dir.tar.bz2

	# Clean the translations:
	$(MAKE) -C po clean


install: build install-stamp
install-stamp:
	# Skip auto-installing (kind of buggy toplevel Makefile):
	dh install --before dh_auto_install
	dh install --after  dh_auto_install

	# Changelog trick:
	dpkg-parsechangelog >> debian/module-assistant/usr/bin/module-assistant

	# Install translations:
	$(MAKE) -C po install DESTDIR=$(CURDIR)/debian/module-assistant

	touch install-stamp


binary-arch: install
	dh binary-arch


binary-indep: install
	dh binary-indep


binary: binary-arch binary-indep
