#!/usr/bin/make -f

# Copyright 2009, Noah Slater <nslater@tumbolia.org>
# Copyright 2014, Olivier Berger <obergix@debian.org>

# Copying and distribution of this file, with or without modification, are
# permitted in any medium without royalty provided the copyright notice and this
# notice are preserved.

#export DH_VERBOSE=1

%:
	dh $@ --with python2

#override_dh_auto_build:
#	help2man -N -n "an aggregate feed generator" --version-string="planet 1" ./planet.py > planet.1

override_dh_installdocs:
	dh_installdocs
	cp -a docs debian/planet-venus/usr/share/doc/planet-venus/html

override_dh_auto_install:
	cp planet.py debian/planet-venus/usr/bin/planet
	cp admin_cb.py debian/planet-venus/usr/share/planet-venus/cgi-bin/
	cp expunge.py debian/planet-venus/usr/share/planet-venus/tools
	cp favicon.py debian/planet-venus/usr/share/planet-venus/tools
	cp publish.py debian/planet-venus/usr/share/planet-venus/tools
	cp spider.py debian/planet-venus/usr/share/planet-venus/tools
	cp splice.py debian/planet-venus/usr/share/planet-venus/tools

override_dh_fixperms:
	dh_fixperms
	find debian/planet-venus/usr/share/pyshared/planet/ -type f -name '*.py' -print0 2>/dev/null | xargs -0r chmod 644

# override_dh_auto_test:
# 	python runtests.py -v
# 	# Re-run to for verifying that individually, 'test_foaf_document' passes (has produced "RedlandError: 'XML parser error: xmlParseCharRef: invalid xmlChar value 12'" through runtests.py above)
# 	python tests/test_foaf.py

# Much borrowed from https://wiki.debian.org/onlyjob/get-orig-source :

PKD  = $(abspath $(dir $(MAKEFILE_LIST)))
PKG  = $(word 2,$(shell dpkg-parsechangelog -l$(PKD)/changelog | grep ^Source))
VER ?= $(shell dpkg-parsechangelog -l$(PKD)/changelog | perl -ne 'print $$1 if m{^Version:\s+(?:\d+:)?(\d.*)(?:\-\d+.*)};')

.PHONY: get-orig-source

get-orig-source: $(PKG)_$(VER)$(DTYPE).orig.tar.xz
	@

## checkout from git (add "git" to Build-Depends)
UURL = git://github.com/rubys/venus.git
UDATE = $(shell date --rfc-3339=seconds --date='TZ="UTC" $(shell echo $(VER) | perl -ne 'print "$$1-$$2-$$3" if m/\+(?:git|svn|hg)(\d{4})(\d{2})(\d{2})/')')
$(PKG)_$(VER)$(DTYPE).orig.tar.xz:
	$(if $(wildcard $(PKG)-$(VER)),$(error $(PKG)-$(VER) exist, aborting..))
	@echo "# Downloading..."
	git clone $(UURL) $(PKG)-$(VER) \
	|| $(RM) -r $(PKG)-$(VER)
	cd $(PKG)-$(VER) \
	&& git checkout $(shell echo $(VER) | sed -r "s/.*~git//") \
	&& echo "# Setting times..." \
	&& for F in $$(git ls-tree -r --name-only HEAD); do touch --no-dereference -d "$$(git log -1 --format="%ai" -- $$F)" "$$F"; done \
	&& echo "# Cleaning-up..." \
	&& $(RM) -r .git .git*
	@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)"
