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

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

PACKAGE := git-ftp
MANPAGES := man/git-ftp.1
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)

%:
	dh $@

override_dh_auto_clean:
	$(RM) $(MANPAGES)

override_dh_auto_build: $(MANPAGES)
	.

$(MANPAGES):
	pandoc --columns=84 --standalone --to=man --output=$@ $@.md
	perl -0pi -e 's{(\s)\-}{$$1\\-}sg;' $@

override_dh_installchangelogs:
	dh_installchangelogs CHANGELOG.md

get-orig-source:
	#uscan --noconf --force-download --repack --rename --download-current-version --verbose --destdir=.
	@echo " Upstream version: $(UVER)\n Version: $(VER)\n UDATE: $(UVER_UDATE)"
	git clone git://github.com/resmo/git-ftp.git $(PACKAGE)-$(UVER)
	cd $(PACKAGE)-$(UVER) \
	&& git reset --hard $$(git rev-list --all -n 1 --before=$(UVER_UDATE)) \
	&& git checkout master \
	&& $(RM) -r .git .git* && cd .. && echo "Packing..." \
	&& tar cJf $(PACKAGE)_$(VER).orig.tar.xz $(PACKAGE)-$(UVER)
	$(RM) -r $(PACKAGE)-$(UVER)
