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

pkg = python-moinmoin
datadir = /usr/share/moin
libdir = $(datadir)/server
python_all = pyversions -r | tr ' ' '\n' | xargs -t -I {} env {}
staticpath := $(shell sed -n -e "s|^\s*release_short\s*=\s*'\([^']*\)'.*|/moin_static\1|p" MoinMoin/version.py)
$(if $(staticpath),,$(error Failed to resolve staticpath))

export SOURCE_DATE_EPOCH = $(shell date -d "$$(dpkg-parsechangelog --count 1 -SDate)" +%s)

build-arch build-indep: build-stamp
build-stamp:
	dh_testdir
	bash debian/tweak-moin-manpage debian/moin.1.template debian/moin.1
	cd MoinMoin/i18n && make
	$(python_all) setup.py build
	sed s,__STATICPATH__,$(staticpath),g < debian/README.Debian.in > debian/README.Debian
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -rf build
	cd MoinMoin/i18n && make clean
	rm -f MoinMoin/i18n/meta.py
	rm -f debian/stamp-update-translations
	rm -f debian/moin.1
	rm -f debian/copyright_newhints debian/README.Debian
	rm -f build-stamp install-stamp
	find . -name '*.py[co]' -delete
	find . -name __pycache__ -type d -empty -delete
	find . -prune -name '*.egg-info' -exec rm -rf '{}' ';'
	dh_clean

install: install-stamp
install-stamp: build-stamp
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	# Add here commands to install the package into debian/$(pkg)
	$(python_all) setup.py install --prefix=/usr --root=debian/$(pkg) --install-layout=deb
	sed -i '1 s,\(#!/usr/bin/python\).*,\1,' debian/*/usr/bin/*

	# Ensure proper hash-bang in Python scripts (Python Policy 0.9.0.0 ยง3.1)
	egrep -r -l -Z '^#! ?/usr/bin/(env )?python[[:print:]]*$$' debian/$(pkg)$(datadir)/server/* debian/$(pkg)/usr/share/doc/* debian/$(pkg)/usr/bin/moin \
	    | xargs -r -0 perl -pi -e 's,^#! ?/usr/bin/(env )?python[[:print:]]*$$,#!/usr/bin/python$(cdbs_python_nondefault_version),'

	# Strip notes in underlay about editing only on moinmaster.
	sh debian/process_language_pages debian/$(pkg)

	# Make sure scripts are executable
	egrep -r -l -Z '^#! ?/usr/bin/(env )?python[[:print:]]*$$' debian/$(pkg)$(libdir) \
		| xargs -r -0 chmod a+x

	# Move underlay to /usr/share/moin
	for HTDOCS in debian/$(pkg)/usr/lib/python*/*-packages/MoinMoin/web/static/htdocs; do \
	    cp -a $${HTDOCS} debian/$(pkg)$(datadir); \
	    rm -rf $${HTDOCS}; \
	    ln -s $(datadir)/htdocs $${HTDOCS} ; \
	done

	# Include Moinmoin Desktop editions files.
	install -m 755 wikiserver.py wikiconfig.py debian/$(pkg)/usr/share/doc/$(pkg)/examples/desktop-edition
	install -m 755 debian/examples/wikiserver-init.sh debian/$(pkg)/usr/share/doc/$(pkg)/examples/wikiserver-init.sh

	# Setup system wide farmconfig.
	for file in `find debian/$(pkg)$(datadir)/config/wikifarm -type f -print`; do \
		sed -f debian/configtweaks.sed $$file > debian/$(pkg)/etc/moin/$${file##*/}; \
	done
	for file in `find debian/$(pkg)$(datadir)/server -type f -printf '%f\n'`; do \
		sed -i -f debian/servertweaks.sed debian/$(pkg)$(datadir)/server/$$file; \
	done

	# Use systemwide word file as main dictionary
	ln -s /etc/moin/words debian/$(pkg)/usr/share/pyshared/MoinMoin/
	ln -s /usr/share/dict/words debian/$(pkg)/etc/moin/words

	# Strip TwikiDrawPlugin as we need to compile from source
	rm -f debian/$(pkg)$(datadir)/htdocs/applets/TWikiDrawPlugin/twikidraw.jar
	echo "This applet has not yet been packaged for Debian. for now it is available at http://debian.jones.dk/auryn/pool-all/official/moin/twikidraw/" > debian/$(pkg)$(datadir)/htdocs/applets/TWikiDrawPlugin/README

	# Strip AnyWikiDrawForTWiki as needs to compile from source
	rm -rf debian/$(pkg)$(datadir)/htdocs/applets/anywikidraw/*
	echo "This applet has not yet been packaged for Debian." > debian/$(pkg)$(datadir)/htdocs/applets/anywikidraw/README

	# Strip FCKeditor, as it's provided by packages fckeditor
	rm -rf debian/$(pkg)$(datadir)/htdocs/applets/FCKeditor/*
	echo "This applet is provided by Debian's fckeditor package." > debian/$(pkg)$(datadir)/htdocs/applets/FCKeditor/README
	echo "More info at /usr/share/doc/$(pkg)/README.Debian(.gz)" >> debian/$(pkg)$(datadir)/htdocs/applets/FCKeditor/README

	touch install-stamp

# Build architecture-dependent files here.
binary-arch: build install
# We have nothing to do by default.

# Build architecture-independent files here.
binary-indep: build install
	dh_testdir
	dh_testroot
	dh_install
	dh_installdocs
	dh_installexamples
	dh_installman
	dh_installchangelogs
	dh_python2
	dh_compress -X examples/
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
build: build-arch build-indep
.PHONY: build-arch build-indep  build clean binary-indep binary-arch binary