#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
#
# This file was originally written by Joey Hess and Craig Small.
# build-arch and build-indep targets  by Bill Allombert 2001
# Changes for python-pyepl by Yaroslav Halchenko

# 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

PYVERS  := $(shell pyversions -vs)
PYVER   := $(shell pyversions -vd)

include /usr/share/python/python.mk

PYNAME=pyepl
PYPACKAGE=python-$(PYNAME)
BASE=$(CURDIR)/debian
PY_COMMON_PATH=$(BASE)/$(PYPACKAGE)-common/usr/share/$(PYPACKAGE)/

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

#Architecture
build: build-arch build-indep
build-arch: build-arch-stamp
build-arch-stamp: $(PYVERS:%=build-arch-python%)
	touch $@

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

build-indep: build-indep-stamp
build-indep-stamp:
#	$(MAKE) -C code/documentation/
	touch $@

clean:
	dh_testdir
	dh_testroot

	python setup.py clean

	rm -f build-arch-stamp build-arch-python* build-indep-stamp
	rm -rf build
	dh_clean

pre-install:
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

install: build pre-install install-indep install-arch
install-indep:
	dh_installdirs -i

	mkdir -p $(PY_COMMON_PATH)
	cp -rp code/resources/*.png $(PY_COMMON_PATH)

	dh_install -i

install-python%:
	python$* setup.py install --root $(BASE)/$(PYPACKAGE) --no-compile --install-layout=deb
	# remove unneeded and manually installed files
	rm -f $(BASE)/$(PYPACKAGE)/usr/lib/*/*/pyepl/license.txt
	rm -f $(BASE)/$(PYPACKAGE)/usr/lib/*/*/pyepl/resources/*
	python$* setup.py clean

install-arch: pre-install $(PYVERS:%=install-python%)

# Must not depend on anything. This is to be called by
# binary-arch/binary-indep
# in another 'make' thread.
binary-common:
	dh_testdir
	dh_testroot
	dh_installchangelogs
	dh_installdocs
	dh_link

	dh_strip

	dh_python2

	dh_compress -X.py -X.pdf
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	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-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install install-indep install-arch
