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

### define the VARS ###
### part 1 of a dirty hack for octave bindings ###
OCTAVE_PATH ?= usr$(shell grep LOCALOCTFILEDIR config/makefile-common-vars.mk | tr -s ' ' ' ' | cut -d\  -f3)
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
debtmp := $(CURDIR)/debian/tmp
dbgpack := $(libpack)$(major)-dbg
SRC_TMP := $(CURDIR)/SRC_TMP
# Test for matlab
CMAKE_OPTS := $(shell sed '/MATLAB/d' debian/cmake_opts)
BIND_MATLAB := $(shell which matlab | grep -q matlab && echo 'yes')
ifdef BIND_MATLAB
CMAKE_OPTS := $(shell cat debian/cmake_opts)
endif

### let's do it ###

DEB_COMPRESS_EXCLUDE = .pdf

%:
	dh  $@ --with-python2 --dbg-package=${dbgpack}

get-orig-source:
	uscan --verbose --force-download --repack

override_dh_auto_clean:
	### the clean target of the libsbml makefile is highly b0rken ###
	### the switch to cmake didn't result in great approvements ###
	### still inline changes of files ###
	### very ugly ###
	rm -rf .pc .debs config docs examples macosx src config.guess config.sub build
	find -maxdepth 1 -type f -delete
	dh_clean
	tar --no-same-owner -xzf ../libsbml_$(version).orig.tar.gz --strip-components=1

override_dh_auto_configure:
ifdef BIND_MATLAB
	sed -e 's/^#//'  debian/control.in > debian/control || true
else
	grep -v ^# debian/control.in > debian/control
endif
	sed -i 's/overriden/overridden/g'  $(shell grep -Rl overriden src/) || true
	mkdir -p build
	cd build ; cmake $(CMAKE_OPTS) ../

override_dh_auto_build:
	cd build ; make
	cp NEWS.txt changelog

override_dh_auto_test:
	echo "no tests"

override_dh_auto_install:
	cd build ; DESTDIR=../debian/tmp make install
	dh_auto_install
	pyclean $(debtmp)/usr/lib/python*
	find $(debtmp)/usr -type f -name "*.js" -delete
	find $(debtmp)/usr -type f -exec chmod 644 {} +
	test -e $(debtmp)/usr/lib/python2.7/site-packages && mv $(debtmp)/usr/lib/python2.7/site-packages $(debtmp)/usr/lib/python2.7/dist-packages || true
	find $(debtmp)/usr/lib -name "*.mex*" -exec strip --strip-unneeded {} + || true
	find $(debtmp)/usr/lib -name "*.mex*" -exec strip --remove-section=.comment {} + || true
	dpkg-shlibdeps $(debtmp)/usr/lib/*/*/*/*/*.mex -Tdebian/libsbml5-octave.substvars
ifdef BIND_MATLAB
	dpkg-shlibdeps $(debtmp)/usr/lib/*.mex* -Tdebian/libsbml5-matlab.substvars
endif
