#!/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.
#
# Modified to make a template file for a multi-binary package with separated
# build-arch and build-indep targets  by Bill Allombert 2001

# 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

PKGNAME=%%PKGNAME%%
MODULENAME=%%MODULENAME%%

PYVERS=$(subst -dev,,\
           $(subst python,,\
             $(filter python%-dev,\
               $(shell sed -n '/^Build-Depends/s/,//gp' debian/control))))

PYVER   := $(shell python -c 'import sys; print sys.version[:3]')

MANPAGES:= $(shell sh debian/find_manpages.sh)

build: build-stamp
build-stamp: $(PYVERS:%=build-python%)
	touch $@

build-python%:
	#python$* setup.py build
	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp
	rm -rf build dist
	-find -name '*.py[co]' | xargs rm -f
	dh_clean

%%INSTALL_VERSIONS_TARGET%%: build install-prereq $(PYVERS:%=install-python%)
	mkdir -p debian/python-pypi-$(PKGNAME)/usr/bin
	-mv -f debian/python$(PYVER)-pypi-$(PKGNAME)/usr/bin/* debian/python-pypi-$(PKGNAME)/usr/bin/ 
	-find debian/python$(PYVER)-pypi-$(PKGNAME)/usr/lib/python$(PYVER)/pypi-packages/ ! -name '*.egg' -maxdepth 1 -mindepth 1 \
		-exec mv '{}' debian/python-pypi-$(PKGNAME)/usr/bin \;
	-find debian/python?.?-pypi-$(PKGNAME)/usr/lib/python?.?/pypi-packages/ ! -name '*.egg' -maxdepth 1 -mindepth 1 \
		-exec rm -f '{}' \;
	rm -rf debian/python2*-pypi-$(PKGNAME)/usr/bin

%%INSTALL_CD_TARGET%%: build install-prereq
	python  /usr/bin/easy_install.py -d debian/python-pypi-$(PKGNAME)/usr/lib/python/pypi-packages/ .

install-prereq:
	dh_testdir
	dh_testroot
	dh_clean -k

install-python%:
	if grep -q setuptools setup.py; then \
		python$* setup.py bdist_egg -d debian/python$*-pypi-$(PKGNAME)/usr/lib/python$*/pypi-packages/ ;\
		python$* setup.py install_scripts -d debian/python$*-pypi-$(PKGNAME)/usr/lib/python$*/pypi-packages/ ;\
		EGG="$$( ls debian/python$*-pypi-$(PKGNAME)/usr/lib/python$*/pypi-packages/*.egg ; )" ;\
		mv $${EGG} $${EGG}.zip ;\
		mkdir $${EGG} ;\
		unzip -d $${EGG} $${EGG}.zip ;\
		rm -f $${EGG}.zip ;\
		chmod -R a+r $${EGG} ;\
	else\
		python$* /usr/bin/easy_install.py -Z --ignore-conflicts-at-my-risk -d debian/python$*-pypi-$(PKGNAME)/usr/lib/python$*/pypi-packages/ . ;\
	fi	
	mkdir -p debian/python$*-pypi-$(PKGNAME)/var/lib/easydeb/python$*/pths/available
	{ \
	echo -ne /usr/lib/python$*/pypi-packages/; cd debian/python$*-pypi-$(PKGNAME)/usr/lib/python$*/pypi-packages/; echo $(MODULENAME)*.egg ; \
	} > debian/python$*-pypi-$(PKGNAME)/var/lib/easydeb/python$*/pths/available/$(MODULENAME)

binary-arch: build install
	dh_testdir -a
	dh_testroot -a
#	dh_installdebconf -a
	dh_installdocs -a
	dh_installexamples -a
#	dh_installmenu -a
	dh_installman -p python-pypi-$(PKGNAME) $(MANPAGES)
#	dh_installinfo -a
#	dh_undocumented -a
#	dh_installchangelogs -a
	dh_python -a /usr/lib/python?.?/pypi-packages
	dh_strip -a
	dh_link -a
	dh_compress -a -X.py
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary-indep: build install
	dh_testdir -i
	dh_testroot -i 
	dh_installchangelogs -i
	dh_installdocs -i 
	dh_installman -p python-pypi-$(PKGNAME) $(MANPAGES)
	dh_compress -i
	dh_fixperms -i
	dh_python -i /usr/lib/python?.?/pypi-packages
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

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