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


# 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)
ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
CROSS= --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
else
CROSS= --build $(DEB_BUILD_GNU_TYPE)
endif

### part1 of a dirty hack for octave bindings, see install: section ###
LOCALOCTFILEDIR ?= usr$(shell grep LOCALOCTFILEDIR config/makefile-common-vars.mk | tr -s ' ' ' ' | cut -d\  -f3)

# prevent the examples from beeing compressed...
DEB_COMPRESS_EXCLUDE := .xml .pdf .py .c .h .cpp .in .cs .java .pl .rb .xsl

# shared library versions, option 1
version=5.2.0
major=5

libpack := libsbml
devpack := $(libpack)-dev
mathpack := $(libpack)-matlab
perlpack := $(libpack)-dev
pythonpack := $(libpack)-dev
javapack := $(libpack)-dev
debtmp := $(CURDIR)/debian/tmp
dbgpack := $(libpack)$(major)-dbg
SRC_TMP := $(CURDIR)/SRC_TMP
# Test for matlab
BIND_MATLAB := $(shell which matlab | grep -q matlab && echo '--with-matlab')

get-orig-source:
	rm -rf $(SRC_TMP) && mkdir $(SRC_TMP)
	[ -f ../libSBML-$(version)-src.zip ] ||  wget -O ../libsbml-$(version)-src.zip http://qa.debian.org/watch/sf.php/sbml/libSBML-$(version)-src.zip
	cd $(SRC_TMP) && unzip ../../libsbml-$(version)-src.zip
	cd $(SRC_TMP)/libsbml-$(version) && tar cvzf ../../../libsbml_$(version).orig.tar.gz .
	rm -rf $(SRC_TMP)

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
# prepare the debian/control file
	if [ "$(BIND_MATLAB)" = "--with-matlab" ] ; then \
	    sed -e 's/^#//'  debian/control.in > debian/control ; else \
	    grep -v ^# debian/control.in > debian/control ; fi

	./configure $(CROSS)	--prefix=/usr				\
				--with-doxygen				\
				--with-perl				\
				--with-python				\
				--with-java				\
				--with-swig				\
				--with-ruby				\
				--with-octave				\
				$(BIND_MATLAB)				\
				--enable-layout				\
				--with-csharp				\
				--mandir=\$${prefix}/share/man		\
				--infodir=\$${prefix}/share/info	\
				CFLAGS="$(CFLAGS)"			\
				LDFLAGS="-Wl,-z,defs"			

build: build-stamp

build-stamp:  config.status
	dh_testdir

	# Add here commands to compile the package.
	$(MAKE)

	touch $@

clean: 
	dh_testdir
	dh_testroot
	rm -f build-stamp

	# Add here commands to clean up after the build process.
	[ ! -f Makefile ] || $(MAKE) distclean
	rm -f config.sub config.guess

	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	# Add here commands to install the package into debian/libsbml.
	mkdir -p $(debtmp)/etc
	cp -a debian/etc $(debtmp)
	$(MAKE) install-docs DESTDIR=$(debtmp)
	$(MAKE) install DESTDIR=$(debtmp)
#	### not required for python2.7 ###
	[ ! -d $(debtmp)/usr/lib/python2.6/ ] || find $(debtmp)/usr/lib/python2.6/ -type d -name site-packages -exec mv {} $(debtmp)/usr/lib/python2.6/dist-packages \; || true
#	### _very_ dirty hack for working octave bindings ###
	mkdir -p $(debtmp)/$(LOCALOCTFILEDIR)
	cp src/bindings/matlab/buildSBML.m $(debtmp)/$(LOCALOCTFILEDIR)/
	cp src/bindings/matlab/CheckAndConvert.m $(debtmp)/$(LOCALOCTFILEDIR)/
	cp src/bindings/matlab/ConvertFormulaToMathML.m $(debtmp)/$(LOCALOCTFILEDIR)/
	cp src/bindings/matlab/Contents.m $(debtmp)/$(LOCALOCTFILEDIR)/
	cp src/bindings/matlab/isoctave.m $(debtmp)/$(LOCALOCTFILEDIR)/
	cp src/bindings/matlab/isSBML_Model.m $(debtmp)/$(LOCALOCTFILEDIR)/
	rm $(debtmp)/usr/lib/*/dist-packages/libsbml/*.pyc || true
	mkdir $(debtmp)/usr/lib/perl5
	mv $(debtmp)/usr/lib/perl/*/* $(debtmp)/usr/lib/perl5/
	chmod 644 $(debtmp)/usr/share/java/libsbmlj.jar
	find $(debtmp)/usr/lib/ -type f -exec chmod 644 {} +
	find $(debtmp)/usr/lib/ -type f -name .packlist -exec rm -f {} +
	find $(debtmp)/usr/lib/ -type d -iname "*.libs" -exec rm -rf {} +
	find $(debtmp)/usr/share/doc/ -type f -name "jquery.js" -exec rm -f {} +
	sed -i "/dependency_libs/ s/'.*'/''/" `find $(debtmp)/usr/lib/ -name '*.la'`
	strip --strip-unneeded $(debtmp)/usr/lib/*.mex* || true
	strip --strip-unneeded $(debtmp)/usr/lib/octave/site/oct/*/*.mex || true

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs
	dh_installdocs
	dh_installexamples
	dh_install
	dh_installman
	dh_link
	dh_strip --dbg-package=${dbgpack}
	dh_compress
	dh_fixperms
	dh_makeshlibs
	dh_makeclilibs -i -V
	dh_installcligac -i
	dh_perl
	dh_python2 --no-guessing-versions
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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