#!/usr/bin/make -f

PACKAGE_NAME=diamond
MODULE_NAME=diamond
DEB_UPSTREAM_VERSION=1.0

PYVERS=2.5

# 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)


config.status: configure
	dh_testdir
	# Add here commands to configure the package.

ifneq "$(wildcard /usr/share/misc/config.sub)" ""
	cp -f /usr/share/misc/config.sub config.sub
endif
ifneq "$(wildcard /usr/share/misc/config.guess)" ""
	cp -f /usr/share/misc/config.guess config.guess
endif
	./configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info CFLAGS="$(CFLAGS)" LDFLAGS="-Wl,-z,defs"

build-libspud: build-libspud-stamp
build-libspud-stamp: build-libspud
	touch $@

build-libspud: config.status
	dh_testdir

	# Add here commands to compile the package.
	$(MAKE) libspud.a
	#docbook-to-man debian/fluidity.sgml > fluidity.1
	$(MAKE) doc


build-diamond: build-diamond-stamp 
build-diamond-stamp: $(PYVERS:%=build-python%)
	touch $@
build-python%: config.status
# Force setuptools, but reset sys.argv[0] to 'setup.py' because setup.py files expect that.
	cd diamond;python$* -c "import setuptools,sys;f='setup.py';sys.argv[0]=f;execfile(f,{'__file__':f,'__name__':'__main__'})" build
	touch $@
clean:
	dh_testdir
	dh_testroot
	rm -f *-stamp
	rm -rf dist build
	-find -name '*.py[co]' | xargs rm -f
	$(MAKE) clean || true
#	find . -name *.pyc -exec rm {} \;
	dh_clean

install-libspud: build-libspud install-prereq
	$(MAKE) install-libspud DESTDIR=$(CURDIR)/debian/libspud-dev

install-spudtools: install-prereq
	$(MAKE) install-spudtools DESTDIR=$(CURDIR)/debian/spudtools

install-diamond: build-diamond install-prereq $(PYVERS:%=install-python%)
install-prereq:
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs -a 

install-python%:
# Force setuptools, but reset sys.argv[0] to 'setup.py' because setup.py files expect that.
	cd diamond;python$* -c "import setuptools,sys;f='setup.py';sys.argv[0]=f;execfile(f,{'__file__':f,'__name__':'__main__'})" install		 --no-compile --single-version-externally-managed		 --root $(CURDIR)/debian/${PACKAGE_NAME}
	mv debian/${PACKAGE_NAME}/usr/lib/python$*/site-packages/${MODULE_NAME}-${DEB_UPSTREAM_VERSION}-py$*.egg-info		 debian/${PACKAGE_NAME}/usr/lib/python$*/site-packages/${MODULE_NAME}.egg-info

binary-arch: build-libspud install-libspud install-spudtools
	dh_testdir
	dh_testroot
	dh_installchangelogs 
	dh_installdocs
	dh_installexamples
#	dh_install
#	dh_installmenu
#	dh_installdebconf	
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_python
#	dh_installinit
#	dh_installcron
#	dh_installinfo
	dh_installman
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
#	dh_perl
#	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary-indep: build-diamond install-diamond
	dh_testdir -i
	dh_testroot -i
	dh_pycentral -i
	dh_installdocs -i
	dh_installdirs -i
	dh_installexamples  -i
	dh_strip -i
	dh_compress -i -X.py
	dh_fixperms -i
	: # Replace all '#!' calls to python with $(PYTHON)
	: # and make them executable
	for i in \
	  `find debian/diamond/usr/bin -type f` \
	  `find debian/diamond/usr/lib -type f`; \
	do \
	  case "$$i" in *-[0-9].[0-9]) continue; esac; \
	  sed '1s,#!.*python[^ ]*\(.*\),#! /usr/bin/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
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

binary: binary-indep binary-arch


.PHONY: build clean binary-indep binary-arch binary-diamond binary-libspud install configure
