#!/usr/bin/make -f
# -*- mode: makefile; coding: utf-8 -*-

include /usr/share/dpkg/pkg-info.mk

NULL  =
PKD   = $(word 2, abspath $(dir $(MAKEFILE_LIST)))
PKG   = $(DEB_SOURCE)
UVER  = $(shell echo $(DEB_VERSION) | cut -d "-" -f1)
DTYPE =
VER  ?= $(subst $(DTYPE),,$(UVER))
REV   = $(shell echo $(VER) | perl -ne 'print "$$1" if m/(?:git|svn|hg|bzr)(\d+)/;')

export PYBUILD_NAME=caja-rename
export PYBUILD_SYSTEM=distutils

%:
	dh ${@} --with python3 --buildsystem=pybuild

override_dh_installchangelogs:
	dh_installchangelogs ChangeLog.bzr


override_dh_auto_clean:
	-dh_auto_clean
	rm -Rfv data/usr/share/locale
	rm -fv cajarename.egg-info/PKG-INFO
	rm -fv cajarename.egg-info/SOURCES.txt
	rm -fv cajarename.egg-info/dependency_links.txt
	rm -fv cajarename.egg-info/requires.txt
	rm -fv cajarename.egg-info/top_level.txt
	rm -Rfv .pybuild

## http://wiki.debian.org/onlyjob/get-orig-source
.PHONY: get-orig-source
get-orig-source: $(PKG)_$(VER)$(DTYPE).orig.tar.xz $(info I: $(PKG)_$(VER)$(DTYPE))
	@

## checkout from bzr (add "bzr (>= 2.6.0~bzr6520)" to Build-Depends, see #666496)
UURL = lp:cajarename
$(PKG)_$(VER)$(DTYPE).orig.tar.xz:
	$(if $(wildcard $(PKG)-$(VER)),$(error $(PKG)-$(VER) exist, aborting..))
	bzr checkout --hardlink --lightweight --revision=$(REV) $(UURL) $(PKG)-$(VER) \
	    || $(RM) -r $(PKG)-$(VER)
	@echo "# Creating ChangeLog..."
	cd $(PKG)-$(VER) && bzr log --log-format=line > ChangeLog.bzr
	@echo "# Modifying..."
	cd $(PKG)-$(VER) && rm -Rfv \
	    .bzr-builddeb \
	    .bzr \
	    debian \
	    $(NULL)
	@echo "# Packing..."
	find -L "$(PKG)-$(VER)" -xdev -type f -print | sort \
	    | XZ_OPT="-6v" tar -caf "../$(PKG)_$(VER)$(DTYPE).orig.tar.xz" -T- --owner=root --group=root --mode=a+rX \
	    && $(RM) -r "$(PKG)-$(VER)"
