#!/usr/bin/make -f
# Made with the aid of debmake, by Christoph Lameter,
# based on the sample debian/rules file for GNU hello by Ian Jackson.

DEB_VER = $(shell dpkg-parsechangelog | sed -ne 's/^Version: \(\([0-9]\+\):\)\?\(.*\)-[0-9]*/\3/p')

package=elmer

# Support multiple makes at once
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
NJOBS := $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
else
NJOBS := 1
endif

ELMER_MODULES = eio matc elmerparam hutiter meshgen2d fem front elmergrid post

# The build process modifies these files, so we need to move them aside in
# order for the clean process to restore the tree to its original state
BACKUP_FILES = eio/Makefile.in eio/configure eio/include/Makefile.in \
  eio/src/Makefile.in elmergrid/Makefile.in elmergrid/aclocal.m4 \
  elmergrid/configure elmergrid/src/Makefile.in elmerparam/Makefile.in \
  elmerparam/aclocal.m4 elmerparam/config.h.in elmerparam/configure \
  elmerparam/src/Makefile.in elmerparam/src/R/Makefile \
  elmerparam/src/R/elmerparam/src/Makevars elmerparam/src/matlab/Makefile \
  elmerparam/test/Makefile.in fem/Makefile.in fem/aclocal.m4 fem/config.h \
  fem/configure fem/stamp-h1 fem/src/Makefile.in fem/src/binio/Makefile.in \
  fem/src/binio/test/Makefile.in fem/src/view3d/Makefile.in \
  fem/src/viewaxis/Makefile.in fem/tests/Makefile.in fem/tests/runtests \
  front/Makefile.in front/aclocal.m4 front/configure front/include/Makefile.in\
  front/src/Makefile.in front/src/tcl/ecif_tcl_mainScript.tcl \
  hutiter/Makefile.in hutiter/configure hutiter/examples/Makefile.in \
  hutiter/examples/ex1/Makefile.in hutiter/include/Makefile.in \
  hutiter/src/Makefile.in matc/Makefile.in matc/aclocal.m4 matc/configure \
  matc/src/Makefile.in meshgen2d/Makefile.in meshgen2d/aclocal.m4 \
  meshgen2d/configure meshgen2d/src/Makefile.in \
  meshgen2d/src/include/Makefile.in post/Makefile.in post/aclocal.m4 \
  post/configure post/src/Makefile.in post/src/camera/Makefile.in \
  post/src/elements/Makefile.in post/src/glaux/Makefile.in \
  post/src/graphics/Makefile.in post/src/modules/Makefile.in \
  post/src/objects/Makefile.in post/src/sico2elmer/Makefile.in \
  post/src/tcl/Makefile.in post/src/tk/Makefile.in \
  post/src/visuals/Makefile.in

MISSING_FILES = eio/NEWS eio/README eio/AUTHORS eio/ChangeLog \
  matc/NEWS matc/README matc/AUTHORS matc/ChangeLog matc/COPYING \
  meshgen2d/NEWS meshgen2d/README meshgen2d/AUTHORS meshgen2d/ChangeLog \
  meshgen2d/COPYING meshgen2d/INSTALL \
  fem/NEWS fem/AUTHORS fem/ChangeLog fem/COPYING fem/INSTALL \
  front/NEWS front/README front/AUTHORS front/COPYING front/INSTALL \
  elmergrid/NEWS elmergrid/README elmergrid/AUTHORS elmergrid/ChangeLog \
  elmergrid/COPYING elmergrid/INSTALL elmergrid/src/metis/Makefile.am \
  post/NEWS post/AUTHORS post/ChangeLog post/COPYING post/INSTALL

clean: patch
	dh_testdir
	for elmermodule in $(ELMER_MODULES); do \
	  if [ -e $$elmermodule/Makefile ]; then \
	    echo; echo CLEANING ELMER MODULE $$elmermodule; echo; \
	    make -C $$elmermodule maintainer-clean; \
	  fi; \
	done
	for missingfile in $(MISSING_FILES); do rm -f $$missingfile; done
	for backupfile in $(BACKUP_FILES); do \
	  if [ -e $$backupfile.bak ]; then \
	    mv -f $$backupfile.bak $$backupfile; \
	  fi; \
	done
	rm -rf elmergrid/src/metis post/src/fonts/TrueType
	rm -f stamp-*
	if [ -d .pc ]; then \
	  QUILT_PATCHES=debian/patches quilt pop -a && rm -rf .pc; \
	fi
	dh_clean

patch:
	if [ ! -d .pc ]; then \
	  QUILT_PATCHES=debian/patches quilt push -a; \
	fi

# In a bit of a hack, the "build" target configures, builds and installs, so
# modules can depend on each other
build: stamp-build
build-arch: stamp-build
build-indep: stamp-build
stamp-build: patch
	dh_testdir
	for backupfile in $(BACKUP_FILES); do \
	  cp -a $$backupfile $$backupfile.bak; \
	done
	mkdir elmergrid/src/metis
	install -d post/src/fonts/TrueType
	for missingfile in $(MISSING_FILES); do touch $$missingfile; done
	for aclocaldir in eio matc elmerparam hutiter meshgen2d fem front elmergrid post; do \
	  echo Running aclocal/automake in module $$aclocaldir; \
	  (cd $$aclocaldir && aclocal && automake); \
	done
	set -e; for elmermodule in $(ELMER_MODULES); do \
	  echo; echo CONFIGURING ELMER MODULE $$elmermodule; echo; \
	  (cd $$elmermodule && \
	   autoconf && \
	   LIBS="-L$(CURDIR)/debian/tmp/usr/lib" \
	     CPPFLAGS="$(CPPFLAGS) -I$(CURDIR)/debian/tmp/usr/include -I/usr/include/freetype2" \
	     FCPPFLAGS="$(FCPPFLAGS) -I$(CURDIR)/debian/tmp/usr/include" \
	     CFLAGS="$(CFLAGS) -I$(CURDIR)/debian/tmp/usr/include -fPIC" \
	     CXXFLAGS="$(CXXFLAGS) -I$(CURDIR)/debian/tmp/usr/include -I/usr/include/freetype2 -fPIC" \
	     FCFLAGS="$(FCFLAGS) -I$(CURDIR)/debian/tmp/usr/include -fPIC" \
	     ./configure --prefix=/usr --with-metis=/usr \
	       --with-mpi-dir=/usr --with-mpi-inc-dir=/usr/include/mpi \
	       --with-blas=-lblas --with-lapack=-llapack \
	       --with-mumps=dmumps_scotch); \
	  echo; echo BUILDING ELMER MODULE $$elmermodule; echo; \
	  make -C $$elmermodule; \
	  echo; echo INSTALLING ELMER MODULE $$elmermodule; echo; \
	  make -C $$elmermodule install DESTDIR=$(CURDIR)/debian/tmp; \
	done
# Last shifts and cleanups
	mv debian/tmp/usr/share/elmerpost/modules debian/tmp/usr/lib/elmerpost
	ln -s ../../lib/elmerpost debian/tmp/usr/share/elmerpost/modules
	rm -rf debian/tmp/usr/share/elmerpost/fonts/TrueType
	(cd debian/tmp/usr/share/elmerpost/fonts && \
	 ln -s ../../fonts/truetype/freefont TrueType)
	rm -f debian/tmp/usr/share/elmerpost/help/html_library-0.3/license.terms
	mv debian/tmp/usr/share/elmersolver/lib debian/tmp/usr/lib/elmersolver
	ln -s ../../lib/elmersolver debian/tmp/usr/share/elmersolver/lib
	rm -f debian/tmp/usr/lib/elmersolver/libelmersolver*
	chmod -x debian/tmp/usr/lib/elmersolver/*
	chmod -x debian/tmp/usr/share/elmersolver/include/*
	chmod -x debian/tmp/usr/share/elmerpost/help/*.html
	chmod -x debian/tmp/usr/share/elmerpost/help/*.tcl
	chmod -x debian/tmp/usr/share/elmerpost/help/figs/*
	chmod -x debian/tmp/usr/share/elmerpost/help/html_library-0.3/*
	chmod -x debian/tmp/usr/share/elmerpost/help/matc/*.gif
	chmod -x debian/tmp/usr/share/elmerpost/help/matc/*.html
	chmod -x debian/tmp/usr/share/elmerpost/help/matc/lh-figs/*
	chmod -x debian/tmp/usr/share/elmerfront/lib/*
	chmod -x debian/tmp/usr/share/elmerfront/tcl/*.tcl*
	chmod -x debian/tmp/usr/share/elmerfront/tcl/*.edf
	chmod -x debian/tmp/usr/share/elmerfront/tcl/images/*
	chmod -x debian/tmp/usr/share/elmerpost/lib/rgb.txt
	chmod -x debian/tmp/usr/share/elmerpost/lib/cameras/*
	chmod -x debian/tmp/usr/share/elmerpost/lib/colormaps/*
	chmod -x debian/tmp/usr/share/elmerpost/lib/images/*
	chmod -x debian/tmp/usr/share/elmerpost/tcl/*
	chmod -x debian/tmp/usr/lib/R/elmerparam/DESCRIPTION
	mv debian/tmp/usr/lib/elmersolver debian/tmp/usr/lib/elmersolver-6.1
	install -d debian/tmp/usr/share/applications
	touch $@

binary-indep: patch
	dh_testdir -i
	dh_testroot -i
	dh_movefiles -i
	dh_installdocs -i
	dh_installchangelogs -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

binary-arch:
	dh_testdir -a
	dh_testroot -a
	dh_installdirs -a
	dh_movefiles -a
	dh_installdebconf -a
	dh_installdocs -a
	dh_installchangelogs -a
	dh_strip -pelmer --dbg-package=elmer-dbg
	dh_strip -plibelmersolver-6.1 --dbg-package=libelmersolver-dbg
	dh_makeshlibs -a
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-arch binary-indep

.PHONY: build binary-indep binary-arch binary

get-orig-source:
	$(CURDIR)/debian/elmerfem_get-orig-source.sh $(DEB_VER)
