#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.

##export DH_VERBOSE=1

ZVER:=2.13
ZOPE:=zope$(ZVER)
PYVER:=2.7
PYTHON:=python$(PYVER)
DEBIAN:=$(CURDIR)/debian
DEB_SATISFIED:=Docutils:distribute:mechanize:initgroups:pip:pytz
distribution:=$(shell lsb_release -is)

# get-orig-source variables
ZOPE_INDEX:=http://download.zope.org/Zope2/index/
SRC_VERSION:=$(shell dpkg-parsechangelog | sed -ne 's/^Version: \(\([0-9]\+\):\)\?\(.*\)-.*/\3/p')
SRC_DIR:=$(ZOPE)-$(SRC_VERSION)
TARBALL:=$(ZOPE)_$(SRC_VERSION).orig.tar.gz

get-orig-source:
	rm -rf $(CURDIR)/$(SRC_DIR)
	mkdir $(CURDIR)/$(SRC_DIR)
	cd $(CURDIR)/$(SRC_DIR); \
	$(PYTHON) $(DEBIAN)/build-scripts/fetch.py \
		--versions-cfg-url=${ZOPE_INDEX}$(SRC_VERSION)/versions.cfg \
		--index=${ZOPE_INDEX}$(SRC_VERSION) \
		--exclude-singles=$(DEB_SATISFIED) \
		Zope2==$(SRC_VERSION)
	tar czf $(CURDIR)/../$(TARBALL) $(SRC_DIR)
	rm -rf $(CURDIR)/$(SRC_DIR)

%:
	dh --with python2 $@ 

override_dh_auto_clean:
	for f in debian/*.in; do \
		generated=`echo $$f | sed 's,.in$$,,;s,ZVER,$(ZVER),'`; \
		rm -f $$generated $$generated.tmp; \
	done
	dh_auto_clean --
	# quilt has issues applying permissions to non-existent files, so we'll need to apply executable permissions here
	chmod a+x configure

override_dh_auto_configure:
	./configure --prefix=/usr/lib/${ZOPE}

override_dh_auto_build:
	make PYTHON=$(PYTHON) \
	     DIST_EXCLUSIONS=$(DEB_SATISFIED)

override_dh_prep:
	for f in debian/*.in; do \
		generated=`echo $$f | sed 's,.in$$,,;s,ZVER,$(ZVER),'`; \
		sed -e 's,@ZVER@,$(ZVER),g' -e 's,@PYVER@,$(PYVER),g' $$f > $$generated.tmp; \
		if cmp --quiet "$$generated" "$$generated.tmp"; then \
			rm -f $$generated.tmp; \
		else \
			mv -f  $$generated.tmp $$generated; \
		fi; \
	done
	dh_prep --
	dh_installzopeinstance -pzope$(ZVER)-sandbox sandbox

override_dh_auto_install:
	make install PYTHON=$(PYTHON) DESTDIR=$(DEBIAN)/$(ZOPE)

        # install the changelogs
	dh_installchangelogs source/Zope2/doc/CHANGES.rst

	cd $(CURDIR)/source; for egg_src in *; do \
		if test "$$egg_src" != "Zope2"; then \
			egg_changes=$$(find $$egg_src -name CHANGES.txt -or -name NEWS.txt | head -n1); \
			if test -n "$$egg_changes"; then \
				cp $$egg_changes $(DEBIAN)/$(ZOPE)/usr/share/doc/$(ZOPE)/changelogs/changelog.$$egg_src; \
				echo "Copied changelog: $(DEBIAN)/$(ZOPE)/usr/share/doc/$(ZOPE)/changelogs/changelog.$$egg_src"; \
			fi; \
		fi; \
	done

	# remove extra license files
	find $(DEBIAN)/$(ZOPE)/usr/lib/$(ZOPE)/lib/python -type f -a \( -name LICENSE.txt -o -name COPYING.txt \) -exec rm -f {} \;

	# remove scripts from egg-info subfolders
	rm -r $(DEBIAN)/$(ZOPE)/usr/lib/$(ZOPE)/lib/python/*/EGG-INFO/scripts

	cp $(DEBIAN)/python_interpreter_template $(DEBIAN)/$(ZOPE)/usr/lib/$(ZOPE)/bin/python

override_dh_installdocs:
	dh_installdocs
ifeq ($(distribution),Ubuntu)
	if [ -f $(DEBIAN)/$(ZOPE)-sandbox/usr/share/doc/$(ZOPE)-sandbox/README.Debian; then \
	    mv $(DEBIAN)/$(ZOPE)-sandbox/usr/share/doc/$(ZOPE)-sandbox/README.Debian \
		$(DEBIAN)/$(ZOPE)-sandbox/usr/share/doc/$(ZOPE)-sandbox/README.Ubuntu; \
	fi
endif

override_dh_python2:
	dh_python2 --shebang="/usr/lib/zope$(ZVER)/bin/python"

override_dh_fixperms:
	dh_fixperms --
	# Setuptools apparently makes every .py file executable during the build.
	find $(DEBIAN)/$(ZOPE)/usr/lib/$(ZOPE)/lib/python/ -type f -perm /a+x -exec chmod a-x {} \;
	chmod 755 \
		$(DEBIAN)/$(ZOPE)/usr/lib/$(ZOPE)/lib/python/Zope2-*/ZPublisher/Client.py \
		$(DEBIAN)/$(ZOPE)/usr/lib/$(ZOPE)/lib/python/Zope2-*/ZPublisher/Test.py
	find $(DEBIAN)/$(ZOPE)/usr/lib/$(ZOPE)/lib/python -type f -a \
		\( -name \*.gif -o -name \*.html -o -name \*.dtml -o -name \*.txt -o -name \*.bat -o -name \*.jpg \) -exec chmod 644 {} \;
	chmod 644 $(DEBIAN)/$(ZOPE)/usr/lib/$(ZOPE)/lib/python/Zope2-*/Zope2/utilities/reindex_catalog.py
	chmod 755 $(DEBIAN)/$(ZOPE)/usr/lib/$(ZOPE)/bin/python

.PHONY: get-orig-source override_dh_auto_clean override_dh_auto_configure override_dh_auto_build override_dh_prep override_dh_auto_install override_dh_fixperms
