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

# Variables declaration
ABI_VERSION=1
VERSION=$(shell cat VERSION | sed -e 's/\([0-9]*\.[0-9]*\)-[0-9]*/\1/')
MAJOR_VERSION=$(shell cat VERSION | sed -e 's/\([0-9]*\)\.[0-9]*/\1/')
MINOR_VERSION=$(shell cat VERSION | sed -e 's/[0-9]*\.\([0-9]*\)/\1/')
SONAME=$(MAJOR_VERSION)

LIBRHEOLEF=librheolef${ABI_VERSION}
LIBRHEOLEF_DEV=librheolef-dev
RHEOLEF_DOC=rheolef-doc
RHEOLEF_RUNTIME=rheolef

TMPDIR=debian/tmp-rheolef
CFLAGS = -Wall -g

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif

configure: configure-stamp
configure-stamp:
	# Prepare the debian control files necessary for the creation of the packages
	cat debian/shlibs.librheolefX.X | \
	  sed -e "s/\@LIBRHEOLEF\@/$(LIBRHEOLEF)/g" \
              -e "s/\@MAJOR_VERSION\@/$(MAJOR_VERSION)/g" \
              -e "s/\@MINOR_VERSION\@/$(MINOR_VERSION)/g" \
              -e "s/\@VERSION\@/$(VERSION)/g" \
              -e "s/\@SONAME\@/$(SONAME)/g" > debian/shlibs.$(LIBRHEOLEF)
	cat debian/librheolefX.X.overrides | \
	  sed -e "s/\@LIBRHEOLEF\@/$(LIBRHEOLEF)/g" \
              -e "s/\@MAJOR_VERSION\@/$(MAJOR_VERSION)/g" \
              -e "s/\@MINOR_VERSION\@/$(MINOR_VERSION)/g" \
              -e "s/\@VERSION\@/$(VERSION)/g" \
              -e "s/\@SONAME\@/$(SONAME)/g" > debian/$(LIBRHEOLEF).lintian-overrides
	cat debian/librheolefX.X.install | \
          sed -e "s/\@VERSION\@/$(VERSION)/g" > debian/$(LIBRHEOLEF).install
	cp -f debian/librheolefX.X.substvars debian/$(LIBRHEOLEF).substvars
	dh_testdir
	./bootstrap
	CC=gcc CXX=g++ ./configure --enable-fast-install --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --disable-optim --with-umfpack
	touch configure-stamp

build: build-stamp

build-stamp: configure-stamp
	dh_testdir

	# Compile the package.
	$(MAKE)
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp
	dh_clean
	# Removing installation directories
	rm -Rf 	$(TMPDIR)

	# Removing and/or reinitialising files generated after templates
	# rm -f debian/shlibs.local

install: build
	dh_testdir
	dh_testroot
	dh_clean
	dh_installdirs

	# Add here commands to install the package into DESTDIR
	$(MAKE) install DESTDIR=$(CURDIR)/$(TMPDIR)
	dh_movefiles --sourcedir=$(TMPDIR)

	# removing some useless progs
	rm -f $(TMPDIR)/usr/bin/doc2man
	rm -f $(TMPDIR)/usr/bin/doc2texi
	rm -f $(TMPDIR)/usr/bin/doc2oxy

	# remove info/dir
	rm -f $(TMPDIR)/usr/share/info/dir*

	# respect debian naming conventions
	test -d $(TMPDIR)/usr/share/doc/rheolef && mv $(TMPDIR)/usr/share/doc/rheolef $(TMPDIR)/usr/share/doc/rheolef-doc

	# Changing file access permissions
	chmod 755 $(TMPDIR)/usr/share/rheolef/check-shlibpath_var.sh

# Build architecture-independent files here.
binary-indep: build install

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_install --sourcedir=$(TMPDIR)
	dh_lintian
	dh_installinfo
	dh_installman
	dh_installexamples
	dh_makeshlibs
	dh_shlibdeps --dpkg-shlibdeps-params="--ignore-missing-info"
	dh_installdocs
	dh_installchangelogs ChangeLog
	dh_compress -X.pdf
	dh_strip
	dh_link
	dh_fixperms
	dh_installdeb
	dh_gencontrol --dpkg-gencontrol-params="-Tdebian/$(LIBRHEOLEF).substvars"
	dh_md5sums
	dh_builddeb || true

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

