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

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

PYTHON_VERSIONS	= 2.1 2.2 2.3 2.4
IS_PURE		= yes
HAVE_DH_PYTHON	= yes
DUMMY_PACKAGE	= python-iplib
CHANGELOG	= 
ARCHITECTURE	= all      # architecture for binary packages

PYTHON		= /usr/bin/python${PYTHON_VERSION}
SETUP_PY	= setup.py
SETUP		= ${PYTHON} ${SETUP_PY}
DEBPACKAGE	= python${PYTHON_VERSION}-iplib
DESTDIR		= ${CURDIR}/debian/${DEBPACKAGE}

CFLAGS	= -Wall -g
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
    CFLAGS += -O0
else
    CFLAGS += -O2
endif
export CFLAGS

ifeq (,$(wildcard PKG-INFO))
    PKG_INFO = debian/PKG-INFO          # generate our own PKG-INFO
else
    PKG_INFO = PKG-INFO                 # use the distributed one
endif

# Macro to run setup.py once for each of the PYTHON_VERSIONS
echo_and_run	= { echo $(1); $(1); }
foreach_python_version\
		= @$(foreach PYTHON_VERSION, ${PYTHON_VERSIONS}, \
			$(call echo_and_run, ${$(firstword $(1))_CMD}) \
                        && ) true
# Commands which can be run by the foreach_python_version macro:
SETUP_CLEAN_CMD	= ${SETUP} clean --all
SETUP_BUILD_CMD	= ${SETUP} build
SETUP_INSTALL_CMD= ${SETUP} install --root='${DESTDIR}' --no-compile
DH_PYTHON_CMD	= dh_python -V ${PYTHON_VERSION} -p ${DEBPACKAGE}

configure: configure-stamp
configure-stamp:
	dh_testdir
	# Add here commands to configure the package.
	touch configure-stamp

build: build-stamp $(PKG_INFO)
build-stamp: configure-stamp
	dh_testdir
	$(call foreach_python_version, SETUP_BUILD)
	touch build-stamp

debian/PKG-INFO: ${SETUP_PY}
	@echo generating $@; \
	python -c 'import sys; from distutils.core import run_setup; \
		run_setup(sys.argv[1]).metadata.write_pkg_info("debian")' $<

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp
	rm -f debian/PKG-INFO
	$(call foreach_python_version, SETUP_CLEAN)
	rm -rf build dist
	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs
	$(call foreach_python_version, SETUP_INSTALL)


binary-indep: build install dummy-package
binary-arch: build install

export DH_OPTIONS =

ifeq (${ARCHITECTURE},all)
   binary-indep: real-packages
else
   binary-arch: real-packages
endif

# Build the real packages
ifdef DUMMY_PACKAGE
real-packages: DH_OPTIONS = -N${DUMMY_PACKAGE}
endif
real-packages: 
	@echo DH_OPTIONS=$$DH_OPTIONS
	dh_testdir
	dh_testroot
	dh_installchangelogs $(CHANGELOG)
	dh_installdocs -A $(PKG_INFO)
	dh_installexamples
	dh_installman
ifndef IS_PURE        
	dh_strip
endif
	dh_compress
	dh_fixperms
ifdef HAVE_DH_PYTHON        
	$(call foreach_python_version, DH_PYTHON)
endif
	dh_installdeb
ifndef IS_PURE        
	dh_shlibdeps
endif
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build the dummy package
#
# There's not much to do, since the only content of the package
# is a symlink:
#
#     /usr/share/doc/python-<basename> -> python<curver>-<basename>
#
dummy-package: DH_OPTIONS = -p${DUMMY_PACKAGE}
dummy-package:
ifdef DUMMY_PACKAGE
	@echo DH_OPTIONS=$$DH_OPTIONS
	dh_testdir
	dh_testroot
	dh_link
	dh_fixperms
	dh_gencontrol
	dh_builddeb
endif

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install configure
.PHONY: dummy-package real-packages


