#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

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

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

headers = err.h     machine.h   matrix.h    meminfo.h   sparse.h    zmatrix.h \
        iter.h      matlab.h    matrix2.h   oldnames.h  sparse2.h   zmatrix2.h 

torm  = macheps maxint torture sptort ztorture meschach.a libmeschach.a

# shared library versions, option 1
# (version=1.2)
version=$(shell expr `pwd` : '.*-\([0-9.]*\)')
# (major=1)
major=$(shell expr `pwd` : '.*-\([0-9]*\).[0-9.]*')
# option 2, assuming the library is created as src/.libs/libfoo.so.2.0.5 or so
#version=`ls src/.libs/lib*.so.* | \
# awk '{if (match($$0,/[0-9]+\.[0-9]+\.[0-9]+$$/)) print substr($$0,RSTART)}'`
#major=`ls src/.libs/lib*.so.* | \
# awk '{if (match($$0,/\.so\.[0-9]+$$/)) print substr($$0,RSTART+4)}'`

configure: configure-stamp
configure-stamp:
	dh_testdir
	# Add here commands to configure the package.
	./configure --prefix=/usr --mandir=\$${prefix}/share/man --with-all
	
	touch configure-stamp

build-arch: build
build-indep: build

build: configure-stamp build-stamp
build-stamp:
	dh_testdir
	
	# Add here commands to compile the package.
	$(MAKE) vers=$(version) CC=$(CC) CFLAGS="-O3 -fPIC -D_REENTRANT" shared
	$(MAKE) clean
	$(MAKE) vers=$(version) CC=$(CC) CFLAGS="-O3 -D_REENTRANT" static
	
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp
	
	# Add here commands to clean up after the build process.
	# make realclean removes config.status, so ignore the lintian warning
	[ ! -f makefile ] || $(MAKE) realclean
	
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	# Add here commands to install the package into debian/libmeschach1.2
	install -d debian/libmeschach1.2/usr/lib/$(DEB_HOST_MULTIARCH)/
	cp libmeschach.so debian/libmeschach1.2/usr/lib/$(DEB_HOST_MULTIARCH)/libmeschach.so.$(version)
	ln -s libmeschach.so.$(version) debian/libmeschach1.2/usr/lib/$(DEB_HOST_MULTIARCH)/libmeschach.so.$(major)

	install -d debian/libmeschach-dev/usr/lib/$(DEB_HOST_MULTIARCH)/
	cp libmeschach.a  debian/libmeschach-dev/usr/lib/$(DEB_HOST_MULTIARCH)/libmeschach.a
	ln -s libmeschach.so.$(version) debian/libmeschach-dev/usr/lib/$(DEB_HOST_MULTIARCH)/libmeschach.so
	install -d debian/libmeschach-dev/usr/include/meschach
	cp $(headers) debian/libmeschach-dev/usr/include/meschach

# 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_installdocs
	dh_installexamples -plibmeschach-dev tut*.c *tort*.c *.dat
	dh_installchangelogs 
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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