#!/usr/bin/make -f
# -*- makefile -*-
# debian/rules for libsbml

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

# hardening

CFLAGS = `dpkg-buildflags --get CFLAGS`
CFLAGS += -Wall
CXXFLAGS = `dpkg-buildflags --get CXXFLAGS`
CXXFLAGS += -Wall
LDFLAGS = `dpkg-buildflags --get LDFLAGS`
CPPFLAGS = `dpkg-buildflags --get CPPFLAGS`

# 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 ###
OCTAVE_PATH ?= 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

version= $(shell dpkg-parsechangelog | grep Version | cut -d\  -f2 | cut -d- -f1)
major= $(shell dpkg-parsechangelog | grep Version | cut -d\  -f2 | cut -d. -f1)

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:
	uscan --verbose --force-download --repack

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

###-deactivated, because the docu only builds with doxygen-1.6.3-###
#				--with-doxygen				\

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

build: build-arch build-indep
build-arch: build-stamp
build-indep: 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
	### remove the by debian/rules created files ###
	rm -f debian/libsbml5-octave.install
	rm -f changelog
	### the clean target of the libsbml makefile is highly b0rken ###
	### verx ugly ###
	rm -rf .pc .debs config docs examples macosx src config.guess config.sub
	dh_clean
	tar --no-same-owner -xzf ../libsbml_$(version).orig.tar.gz -C ../

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)
###-deactivated, because the docu only builds with doxygen-1.6.3-###
#	$(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)/$(OCTAVE_PATH)
	cp src/bindings/matlab/buildSBML.m $(debtmp)/$(OCTAVE_PATH)/
	cp src/bindings/matlab/CheckAndConvert.m $(debtmp)/$(OCTAVE_PATH)/
	cp src/bindings/matlab/ConvertFormulaToMathML.m $(debtmp)/$(OCTAVE_PATH)/
	cp src/bindings/matlab/Contents.m $(debtmp)/$(OCTAVE_PATH)/
	cp src/bindings/matlab/isoctave.m $(debtmp)/$(OCTAVE_PATH)/
	cp src/bindings/matlab/isSBML_Model.m $(debtmp)/$(OCTAVE_PATH)/
	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 examples -type f -name "*.R" -exec chmod -x {} + || true
	sed -i "/dependency_libs/ s/'.*'/''/" `find $(debtmp)/usr/lib/ -name '*.la'`
	strip --strip-unneeded $(debtmp)/usr/lib/*.mex* || true
	strip --strip-unneeded $(debtmp)/$(OCTAVE_PATH)/*.mex || true
	strip --strip-unneeded $(debtmp)/$(OCTAVE_PATH)/*.mex || true
	echo $(OCTAVE_PATH) >debian/libsbml5-octave.install
	cp NEWS.txt changelog

# 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}
	if [ "$(BIND_MATLAB)" = "--with-matlab" ] ; then \
	strip --remove-section=.comment debian/libsbml5-matlab/usr/lib/*.mexa64 || true ; fi
	strip --remove-section=.comment debian/libsbml5-octave/$(OCTAVE_PATH)/*.mex || true
	dh_compress
	dh_fixperms
	dh_makeshlibs
	dpkg-shlibdeps debian/libsbml5-octave/$(OCTAVE_PATH)/* -Tdebian/libsbml5-octave.substvars
	if [ "$(BIND_MATLAB)" = "--with-matlab" ] ; then \
	dpkg-shlibdeps debian/libsbml5-matlab/usr/lib/* -Tdebian/libsbml5-matlab.substvars ; fi
	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
