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

# try to avoid lintian warnings as:
#   W: rheolef: hardening-no-fortify-functions usr/bin/field
CPPFLAGS = -D_FORTIFY_SOURCE=2
CXXFLAGS = -O3 -D_FORTIFY_SOURCE=2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security
LDFLAGS  = -Wl,-z,relro -Wl,-Bsymbolic-functions ${CXXFLAGS}
CFLAGS   = ${CXXFLAGS}

#ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
#	CXXFLAGS += -O0
#else
#	CXXFLAGS += -O3
#endif

configure: configure-stamp
configure-stamp:
	# Prepare the debian control files necessary for the creation of the packages
	cat debian/shlibs.librheolef1.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)
	echo 'shlibs:Depends=libc6 (>= 2.7-1), libgcc1 (>= 1:4.3.1-9), libstdc++6 (>= 4.3.1-9)' >  debian/$(LIBRHEOLEF).substvars
	dh_testdir
	./bootstrap

	CXX=g++ CPPFLAGS="${CPPFLAGS}" CXXFLAGS="${CXXFLAGS}" LDFLAGS="${LDFLAGS}" ./configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --disable-optim --enable-debian-packaging --enable-fast-install
	touch configure-stamp

build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp
build-stamp: configure-stamp
	dh_testdir
	# Compile the package.
	make
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	# for double build, cleaning via Makefile
	if test -f Makefile; then make distclean; fi
	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: install-stamp
install-stamp: 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*
	# delete rpath (debian policy) if any:
	- chrpath -d $(TMPDIR)/usr/bin/branch
	- chrpath -d $(TMPDIR)/usr/bin/field
	- chrpath -d $(TMPDIR)/usr/bin/geo
	- chrpath -d $(TMPDIR)/usr/bin/csr
	- chrpath -d $(TMPDIR)/usr/lib/librheolef.*so*
	- chrpath -d $(TMPDIR)/usr/lib/rheolef/mkgeo_*
	- chrpath -d $(TMPDIR)/usr/lib/rheolef/msh2geo_int
	- chrpath -d $(TMPDIR)/usr/lib/rheolef/mkgeo_ball_gmsh_fix
	# Changing file access permissions
	chmod 755 $(TMPDIR)/usr/share/rheolef/check-shlibpath_var.sh
	touch install-stamp

binary: binary-indep binary-arch

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

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

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