#!/usr/bin/make -f
# -*- makefile -*-

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

PACKAGE := git2cl
VER := $(shell dpkg-parsechangelog | perl -ne 'print $$1 if m/Version:\s*([\d\.+git]+)/')
UVER := $(shell echo $(VER) | perl -ne 'print $$1 if m/\+git(\d+)/')
UVER_DATE := $(shell echo '$(UVER)' | perl -ne 'print $$1 if m/^(\d{8})/')
UVER_TIME := $(shell echo '$(UVER)' | perl -ne 'print $$1 if m/^\d{8}(\d+)/')
UVER_UDATE := $(shell date -u -d "$(UVER_DATE) $(UVER_TIME)" +%s)

MANPAGE := debian/git2cl.1
$(MANPAGE):
	perldoc -o checker git2cl
	pod2man -s1 --stderr --utf8 git2cl $(MANPAGE)
	## pod2man may produce empty file so until #659939 is fixed
	## the following is needed to abort on empty file:
	[ -s "$(MANPAGE)" ]

%:
	dh $@

override_dh_clean:
	dh_clean $(MANPAGE)

override_dh_auto_build: $(MANPAGE)

get-orig-source:
	@echo " Upstream version: $(UVER)\n Version: $(VER)\n UDATE: $(UVER_UDATE)"
	git clone git://repo.or.cz/git2cl.git $(PACKAGE)-$(UVER)
	cd $(PACKAGE)-$(UVER) \
	&& git reset --hard `git rev-list --all -n 1 --before=$(UVER_UDATE)` \
	&& echo "Generating ChangeLog" \
	&& ( [ -e ChangeLog ] || ./git2cl > ChangeLog ) \
	&& $(RM) -r .git && cd .. && echo "Packing..." \
	&& tar cjvf $(PACKAGE)_$(VER).orig.tar.bz2 $(PACKAGE)-$(UVER)
	$(RM) -r $(PACKAGE)-$(UVER)
