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

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

# This has to be exported to make some magic below work.
export DH_OPTIONS

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

ZVER	  := 2.9
ZOPE	  := zope$(ZVER)
PACKAGE	  := zope$(ZVER)
DEBIAN    := $(shell pwd)/debian/$(PACKAGE)
PYTHONVER := 2.4
PYTHONBIN := /usr/bin/python$(PYTHONVER)
ZBASE     := Zope-2.9.7-final

distribution := $(shell lsb_release -is)

unpack: unpack-stamp
unpack-stamp:
	tar xfz $(ZBASE).tgz
	mv $(ZBASE) z
	touch unpack-stamp

clean: unpatch
	dh_testdir
	dh_testroot
	for f in debian/*.in; do \
	  generated=`echo $$f | sed 's,.in$$,,;s,ZVER,$(ZVER),'`; \
	  rm -f $$generated $$generated.tmp; \
	done
	rm -f build-arch-stamp build-indep-stamp unpack-stamp
	rm -f -r build-base $(ZBASE) z
	dh_clean

build: build-arch build-indep

build-arch: unpack-stamp patch-stamp build-arch-stamp
build-arch-stamp:
	cd z && ./configure \
		--prefix=$(DEBIAN)/usr/lib/$(ZOPE) \
		--with-python=$(PYTHONBIN)
	cd z && make
	touch build-arch-stamp

build-indep: build-indep-stamp
build-indep-stamp:
	touch build-indep-stamp

install: install-indep install-arch

install-indep:
	dh_testdir
	dh_testroot
	dh_installzopeinstance -pzope$(ZVER)-sandbox sandbox
	dh_installdocs -i
	dh_install -i
ifeq ($(distribution),Ubuntu)
	mv debian/zope$(ZVER)-sandbox/usr/share/doc/zope$(ZVER)-sandbox/README.Debian \
       debian/zope$(ZVER)-sandbox/usr/share/doc/zope$(ZVER)-sandbox/README.Ubuntu
endif

install-arch:
	dh_testdir
	dh_testroot
	dh_clean -k -s
	for f in debian/*.in; do \
	  generated=`echo $$f | sed 's,.in$$,,;s,ZVER,$(ZVER),'`; \
	  sed 's,@ZVER@,$(ZVER),g' $$f > $$generated.tmp; \
	  if cmp --quiet "$$generated" "$$generated.tmp"; then \
	    rm -f $$generated.tmp; \
	  else \
	    mv -f $$generated.tmp $$generated; \
	  fi; \
	done
	dh_installdirs -s

	# Install Zope and remove *.pyc
	cd z && make install
	find $(DEBIAN) -name '*.pyc' | xargs -r rm -r

	# FHS enforcement
	mkdir -p $(DEBIAN)/etc/$(ZOPE) \
	         $(DEBIAN)/var/log/$(ZOPE) \
	         $(DEBIAN)/var/lib/$(ZOPE)/instance \
	         $(DEBIAN)/var/lib/$(ZOPE)/zeo \
	         $(DEBIAN)/usr/lib/$(ZOPE)/lib

	echo "Zope$(ZVER) logs placeholder" > $(DEBIAN)/var/log/$(ZOPE)/.placeholder

	mkdir -p  $(DEBIAN)/usr/share/doc/
	#dh_link -p$(ZOPE) /usr/share/doc/$(ZOPE) /usr/lib/$(ZOPE)/doc

	# Fix python interpreter where needed
	for i in `find $(DEBIAN) -type f`; do \
	  if sed '1s,#!.*python[^ ]*\(.*\),#!$(PYTHONBIN)\1,' \
		$$i > $$i~ ; then \
	  if cmp --quiet $$i $$i~ ; then \
	    rm -f $$i~ ; \
	  else \
	    mv -f $$i~ $$i; \
	    chmod 755 $$i; \
	    echo "fixed interpreter: $$i"; \
	  fi; else  rm -f $$i~ ; fi ; \
	done

	rm $(DEBIAN)/usr/lib/zope2.9/lib/python/zope/formlib/LICENSE.txt \
	   $(DEBIAN)/usr/lib/zope2.9/lib/python/Products/Five/COPYING.txt \
	   $(DEBIAN)/usr/lib/zope2.9/lib/python/Products/Five/doc/ZopePublicLicense.txt \
	   $(DEBIAN)/usr/lib/zope2.9/lib/python/docutils/COPYING.txt \
	   $(DEBIAN)/usr/lib/zope2.9/lib/python/pytz/LICENSE.txt \
	   $(DEBIAN)/usr/lib/zope2.9/lib/python/ZConfig/LICENSE.txt

	rm -fr $(DEBIAN)/usr/lib/zope2.9/doc

	chmod 755 $(DEBIAN)/usr/lib/zope2.9/lib/python/DocumentTemplate/release.sh \
	          $(DEBIAN)/usr/lib/zope2.9/lib/python/ZPublisher/Client.py \
	          $(DEBIAN)/usr/lib/zope2.9/lib/python/ZPublisher/Test.py

	find $(DEBIAN)/usr/lib/zope2.9/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)/usr/lib/zope2.9/bin/reindex_catalog.py \
	          $(DEBIAN)/usr/lib/zope2.9/skel/import/README.txt \
			  $(DEBIAN)/usr/lib/zope2.9/lib/python/ImageFile.py \
			  $(DEBIAN)/usr/lib/zope2.9/lib/python/Products/ZSQLMethods/Setup \
			  $(DEBIAN)/usr/lib/zope2.9/lib/python/Products/ZGadflyDA/Setup

	# Lintian and linda overrides (images in /usr/lib)
	mkdir -p -m 0755 $(DEBIAN)/usr/share/lintian/overrides
	install -m 0644 debian/$(ZOPE).lintian-overrides \
	            $(DEBIAN)/usr/share/lintian/overrides/$(ZOPE)

	mkdir -p -m 0755 $(DEBIAN)/usr/share/linda/overrides
	install -m 0644 debian/$(ZOPE).linda-overrides \
	        $(DEBIAN)/usr/share/linda/overrides/$(ZOPE)

	dh_installdocs -p$(ZOPE) z/README.txt
	dh_installdocs -a -X INSTALL.txt z/doc/*
	dh_link /usr/share/doc/zope-common/README.Debian.gz \
	        /usr/share/doc/zope$(ZVER)/README.Debian.gz
	cp debian/zope2.9.NEWS.Debian $(DEBIAN)/usr/share/doc/zope2.9/NEWS.Debian
	dh_installexamples -p$(ZOPE) z/lib/python/Products/PageTemplates/examples/*
	dh_pycentral -p$(ZOPE) /usr/lib/$(ZOPE) -V $(PYTHONVER)

binary-common:
	dh_testdir
	dh_testroot
	dh_installchangelogs -k z/doc/CHANGES.txt
	dh_installlogrotate
	dh_installdebconf
	dh_installinit
	dh_installman
	dh_link
	dh_strip
	dh_compress -X.zexp
	dh_fixperms
	dh_shlibdeps
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture independant packages using the common target.
binary-indep: build-indep install-indep
	$(MAKE) -f debian/rules DH_OPTIONS=-i binary-common

# Build architecture dependant packages using the common target.
binary-arch: build-arch install-arch
	$(MAKE) -f debian/rules DH_OPTIONS=-a binary-common

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

include /usr/share/dpatch/dpatch.make
