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

#  Apparently WGET agent is not favored by upstream's MoinMoin
WGET_AGENT_STRING="Lynx/2.8.8dev.4 libwww-FM/2.14 SSL-MM/1.4.1 GNUTLS/2.8.6"
WGET=wget -q --user-agent=$(WGET_AGENT_STRING)
WIKI_URL=http://wiki.cython.org

# Directory where to store upstream changelogs
CHANGELOGS=debian/changelogs

# Quick workaround for signed/unsigned portability issues in cython:
export CFLAGS += -fsigned-char

# Python versions
PYDEF=$(shell pyversions -dv)
PY2VERS=$(shell pyversions -vr)

%:
	dh $@ --with python2 --buildsystem python_distutils

override_dh_installman:
	: # Generate a manpage using help2man
	PYTHONPATH=`/bin/ls -d $(CURDIR)/build/lib.*-$(PYDEF)` \
	  help2man --no-info --no-discard-stderr \
		-n "compile Cython code (.pyx) into C to build a Python extension" \
		bin/cython >| build/cython.1
	: # Verify that manpage generated nicely -- no Errors reported
	grep -q 'ImportError:' build/cython.1 && exit 1 || :
	dh_installman build/cython.1

override_dh_install:
	dh_install

	: # fix shebang
	sed -i -e '1 s,#!.*python.*,#!/usr/bin/python,' debian/cython/usr/bin/*

	: # remove files installed from cython.install that belong in -dbg
	find $(CURDIR)/debian/cython/ -name "*_d.so" -delete

	: # share -dbg and normal package doc dirs
	rm -rf debian/cython-dbg/usr/share/doc/cython-dbg
	dh_link -pcython-dbg usr/share/doc/cython usr/share/doc/cython-dbg

override_dh_installchangelogs:
	dh_installchangelogs $(CHANGELOGS)/ReleaseHistory

override_dh_installdocs: assure-changelogs
	dh_installdocs $(CHANGELOGS)/ReleaseNotes-*

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	set -e; for P in $(PY2VERS); do \
	 PYTHONPATH=`/bin/ls -d $(CURDIR)/build/lib.*-$$P` \
	  /usr/bin/python$$P runtests.py --no-refnanny -v -v --exclude="parallel" --work-dir=build/work-dir; \
	done
endif

override_dh_strip:
	dh_strip -pcython --dbg-package=cython-dbg

override_dh_auto_clean:
	rm -rf build
	find -name "*.py[oc]" -type f -delete

# To fetch and update upstream Changelogs
DEB_UPSTREAM_VERSION=$(shell dpkg-parsechangelog | sed -rne 's,^Version: ([^-]+).*,\1,p')

# Notes:
#  SED is used to do minor de-wikification to make text readable
get-changelogs:
	mkdir -p $(CHANGELOGS)
	: # Fetching ReleaseHistory and other available ReleaseNotes
	@release_notes=`$(WGET) -O- '$(WIKI_URL)/ReleaseHistory?action=raw' | sed -n -e '1,/^=/p' | grep ReleaseNotes | sed -e 's/.*\[\([^]]*\)\].*/\1/g'`; \
	for rnotes in ReleaseHistory $$release_notes; do \
		rnotes_url=$(WIKI_URL)/$$rnotes; \
		echo "Fetching $$rnotes_url"; \
		{ \
		 echo "Fetched from $$rnotes_url on `date`\n"; \
		 $(WGET) -O- "$$rnotes_url?action=raw" \
		 | sed -e 's,\({{{\|}}}\),,g' -e 's,\[\[\([^]]*\)|[^]]*\]\],\1,g' -e 's/\r//'; \
		} >| $(CHANGELOGS)/$$rnotes; \
	 done

# Assure presence of a changelog for current upstream release
assure-changelogs:
	@: "Assure present upstream changelog file"
	@if [ ! -e $(CHANGELOGS)/ReleaseNotes-"$(DEB_UPSTREAM_VERSION)" ] && ! grep -q '^$(DEB_UPSTREAM_VERSION)$$' $(CHANGELOGS)/skip ; then \
		echo >&2 "E: ReleaseNotes for $(DEB_UPSTREAM_VERSION) are missing from $(CHANGELOGS)"; \
		echo >&2 "I: Run 'debian/rules get-changelogs' or add version into $(CHANGELOGS)/skip"; \
		exit 1; \
	fi
