#! /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

# brace expansion used in this Makefile
export SHELL=/bin/bash

DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

zbase	= ZopeX3-3.0.0

PWD	:= $(shell pwd)
PYTHON	= /usr/bin/python2.3
d	= debian/tmp
libdir	= usr/lib/zope3
pylibdir= usr/lib/zope3/lib/python
sboxdir	= var/lib/zope3-sandbox

p_lib	= zope3-lib
p_bin	= zope3-scripts
p_add	= zope3-addons
p_dutil	= zope3-docutils
p_sbox	= zope3-sandbox
p_doc	= zope3-doc

d_lib	= debian/$(p_lib)
d_bin	= debian/$(p_bin)
d_add	= debian/$(p_add)
d_dutil	= debian/$(p_dutil)
d_sbox	= debian/$(p_sbox)
d_doc	= debian/$(p_doc)

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

configure: config-stamp
config-stamp: unpack-stamp
	dh_testdir
	cd z && ./configure \
		--prefix=/$(libdir) \
		--with-python=/usr/bin/python2.3 \
		--force
	touch config-stamp

build: build-arch build-indep

build-arch: build-arch-stamp
build-arch-stamp: config-stamp
	cd z && $(PYTHON) install.py build
	touch build-arch-stamp

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

clean:
	dh_testdir
	dh_testroot
	rm -f unpack-stamp config-stamp build-arch-stamp build-indep-stamp
	rm -rf build z $(zbase)
	dh_clean

install: install-indep install-arch install-sandbox
install-indep: build-indep install-sandbox

install-arch: build-arch
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	cd z && $(PYTHON) install.py install \
	    --skip-build --home $(PWD)/$(d)/$(libdir)
	-find $(d) -name '*.py[co]' | xargs -n 100 rm -f
	-find $(d) -name tests | xargs -n 100 rm -rf

	: # $(p_lib)
	mkdir -p $(d_lib)/$(libdir)
	mv $(d)/$(libdir)/{bin,lib,zopeskel} $(d_lib)/$(libdir)/

	: # Replace all '#!' calls to python with $(PYTHON)
	: # and make them executable
	for i in `find $(d_lib)/$(libdir) -type f`; do \
	  sed '1s,#!.*python[^ ]*\(.*\),#! $(PYTHON)\1,' \
		$$i > $$i.temp; \
	  if cmp --quiet $$i $$i.temp; then \
	    rm -f $$i.temp; \
	  else \
	    mv -f $$i.temp $$i; \
	    chmod 755 $$i; \
	    echo "fixed interpreter: $$i"; \
	  fi; \
	done

	: # fix wrong permissions as shipped in the tarball
	chmod 644 \
	    $(d_lib)/$(pylibdir)/zope/app/locales/fr/LC_MESSAGES/zope.po \
	    $(d_lib)/$(pylibdir)/zope/app/locales/it/LC_MESSAGES/zope.po \
	    $(d_lib)/$(pylibdir)/zope/app/broken/broken.gif \
	    $(d_lib)/$(pylibdir)/zope/app/rdb/gadfly/index.html \
	    $(d_lib)/$(pylibdir)/zope/app/errorservice/browser/texttbentry.pt

disabled-script-package:
	: # $(p_bin)
	mkdir -p $(d_bin)/$(libdir)
	dh_link -p$(p_bin) \
	  /$(libdir)/bin/mkzeoinst.py		/usr/bin/zope3-mkzeoinst \
	  /$(libdir)/bin/mkzopeinstance		/usr/bin/zope3-mkzopeinstance \
	  /$(libdir)/bin/runzeo.py		/usr/bin/zope3-runzeo \
	  /$(libdir)/bin/zconfig		/usr/bin/zope3-zconfig \
	  /$(libdir)/bin/zconfig_schema2html \
					/usr/bin/zope3-zconfig_schema2html \
	  /$(libdir)/bin/zdctl.py		/usr/bin/zope3-zdctl \
	  /$(libdir)/bin/zdrun.py		/usr/bin/zope3-zdrun \
	  /$(libdir)/bin/zeoctl.py		/usr/bin/zope3-zeoctl \
	  /$(libdir)/bin/zeopasswd.py		/usr/bin/zope3-zeopasswd \
	  /$(libdir)/bin/zopetest		/usr/bin/zope3-zopetest


install-sandbox: install-arch
	dh_testdir
	dh_testroot
	rm -rf $(d_sbox)
	dh_installdirs -p$(p_sbox) \
	    $(sboxdir) \
	    var/log \
	    etc \
	    usr/lib/zope3-sandbox

	: # create the instance
	$(d_lib)/$(libdir)/bin/mkzopeinstance \
	    -u admin:foobar \
	    -d $(d_sbox)/$(sboxdir)

	: # correct pathes for final installation location
	for i in $$(find $(d_sbox)/$(sboxdir) -type f); do \
	  sed 's,$(PWD)/$(d_sbox),,g;s,$(PWD)/$(d_lib),,g;s,^INSTANCE_HOME.*,INSTANCE_HOME = "/$(sboxdir)",' \
	    $$i > $$i.new; \
	  if diff -u $$i $$i.new; then \
	    rm -f $$i.new; \
	  else \
	    chmod --reference $$i $$i.new; \
	    mv -f $$i.new $$i; \
	  fi; \
	done

	: # make FHS compliant
	mv $(d_sbox)/$(sboxdir)/log $(d_sbox)/var/log/zope3-sandbox
	mv $(d_sbox)/$(sboxdir)/{bin,lib} $(d_sbox)/usr/lib/zope3-sandbox/
	mv $(d_sbox)/$(sboxdir)/etc $(d_sbox)/etc/zope3-sandbox
	dh_link -p$(p_sbox) \
	    /usr/lib/zope3-sandbox/bin	/$(sboxdir)/bin \
	    /usr/lib/zope3-sandbox/lib	/$(sboxdir)/lib \
	    /var/log/zope3-sandbox	/$(sboxdir)/log \
	    /etc/zope3-sandbox		/$(sboxdir)/etc


# Build architecture independant packages using the common target.
binary-indep: build-indep install-indep
	dh_testdir
	dh_testroot
	dh_installchangelogs -i z/ZopeX3/doc/CHANGES.txt
	dh_installdocs -i z/README.txt
	mv $(d_sbox)/usr/share/doc/$(p_sbox)/README.Debian \
		$(d_sbox)/usr/share/doc/$(p_sbox)/README.Ubuntu
	dh_installdocs -p$(p_doc) \
	    -XCHANGES.txt -XINSTALL.txt -XREADME.txt -XMakefile \
	    z/ZopeX3/doc/*
#	dh_installdebconf -i
#	dh_installlogrotate -i
#	dh_installinit -i
#	dh_installcron -i
	dh_installman -i
#	rm -rf $(d_bin)/usr/share/doc/$(p_bin)
#	dh_link -p$(p_bin) /usr/share/doc/$(p_lib) /usr/share/doc/$(p_bin)
	dh_compress -i -X.rst -X.css
	dh_fixperms -i
	dh_python -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture dependant packages using the common target.
binary-arch: build-arch install-arch
	dh_testdir
	dh_testroot
	dh_installchangelogs -a z/ZopeX3/doc/CHANGES.txt
	dh_installdocs -a z/README.txt
	mv $(d_lib)/usr/share/doc/$(p_lib)/README.Debian \
		$(d_lib)/usr/share/doc/$(p_lib)/README.Ubuntu
#	dh_installdebconf -a
#	dh_installlogrotate -a
#	dh_installmime -a
#	dh_installinit -a
#	dh_installcron -a
	dh_installman -a
	dh_link -a
	dh_strip -a
	dh_compress -a -X.rst -X.css
	dh_fixperms -a
	dh_python -a /$(libdir)/{bin,lib}
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

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