#!/usr/bin/make -f
BUILDDIR = $(CURDIR)/debian/build
JAVA_HOME=/usr/lib/jvm/default-java

export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic
export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

# Add --with javahelper for java-wrappers

%:
	dh $@ --buildsystem=cmake --builddirectory=$(BUILDDIR) --with python2 --with javahelper --parallel

export OMPI_MCA_plm_rsh_agent=/bin/false                #workaround to start MPI-applications in chroot


disable_mpi_archs = armel armhf kfreebsd-amd64 kfreebsd-i386 mips mipsel
export LD_LIBRARY_PATH :=  $(CURDIR)/debian/tmp/usr/lib:$(LD_LIBRARY_PATH); 


extra_flags += \
-DPYTHON_INCLUDE_DIR:PATH=/usr/include/python2.7 \
-DPYTHON_LIBRARY:FILEPATH=/usr/lib/libpython2.7.so \
-DENABLE_METIS:BOOL=OFF \
-DENABLE_TAUCS:BOOL=OFF \
-DENABLE_OSMESA:BOOL=OFF \
-DENABLE_CGNS:BOOL=OFF \
-DENABLE_WRAP_JAVA:BOOL=ON \
-DENABLE_WRAP_PYTHON:BOOL=ON \
-DENABLE_BUILD_SHARED:BOOL=ON \
-DENABLE_ONELAB:BOOL=ON \
-DCMAKE_INCLUDE_PATH:STRING="/usr/include/mpi" \
-DCMAKE_INSTALL_PREFIX:STRING="/usr" \
-DINSTALL_LIB_DIR:STRING="/usr/lib/$(DEB_HOST_MULTIARCH)" \

ifeq (,$(filter $(DEB_HOST_ARCH),$(disable_mpi_archs)))
	extra_flags += \
	-DCMAKE_CXX_FLAGS="-DMPICH_SKIP_MPICXX -DOMPI_SKIP_MPICXX -fopenmp -lmpi -fPIC \
	                   -Wall $(shell dpkg-buildflags --get CXXFLAGS)" \
	-DENABLE_MPI:BOOL=ON
else
	extra_flags += \
	-DCMAKE_CXX_FLAGS="-DMPICH_SKIP_MPICXX -DOMPI_SKIP_MPICXX -fopenmp -fPIC \
	                   -Wall $(shell dpkg-buildflags --get CXXFLAGS)" \
	-DENABLE_MPI:BOOL=OFF
endif

# PETSC_DIR is the location of PETSc.
export PETSC_DIR=/usr/lib/petscdir/3.2
export PETSC_ARCH=linux-gnu-c-opt
export SLEPC_DIR=/usr/lib/slepcdir/3.2

override_dh_auto_clean:
	rm -rf $(BUILDDIR)
	rm -rf $(CURDIR)/debian/tmp
	rm -f doc/texinfo/gmsh.vr 
	rm -f doc/texinfo/gmsh.fn 
	rm -f doc/texinfo/gmsh.ky 
	rm -f doc/texinfo/gmsh.pg
	rm -f doc/texinfo/gmsh.toc
	rm -f doc/texinfo/gmsh.log
	rm -f doc/texinfo/gmsh.cp
	rm -f doc/texinfo/gmsh.tp
	rm -f doc/texinfo/gmsh.tps
	rm -f doc/texinfo/gmsh.cps
	rm -f doc/texinfo/gmsh.aux
	rm -f doc/texinfo/gmsh.html
	rm -f doc/texinfo/gmsh.pdf
	rm -f doc/texinfo/gmsh.info
	jh_build --clean

override_dh_auto_configure:
	dh_auto_configure -- $(extra_flags)

override_dh_auto_build:
	dh_auto_build
	# To avoid the texi2dvi pb add LC_ALL=C
	dh_auto_build -- pdf html info LC_ALL=C
	chrpath -d $(BUILDDIR)/wrappers/gmshpy/*.so

override_dh_auto_test:
	mkdir test-dir
	cp demos/transfinite.geo test-dir
	cd test-dir; $(BUILDDIR)/gmsh transfinite.geo -2 -o transfinite2d.mesh
	cd test-dir; $(BUILDDIR)/gmsh transfinite.geo -3 -o transfinite3d.mesh
	cd test-dir; $(BUILDDIR)/gmsh transfinite.geo -2 -o transfinite2d.msh
	cd test-dir; $(BUILDDIR)/gmsh transfinite.geo -3 -o transfinite3d.msh
	cd test-dir; $(BUILDDIR)/gmsh transfinite.geo -2 -o transfinite2d.stl
	cd test-dir; $(BUILDDIR)/gmsh transfinite.geo -3 -o transfinite3d.stl
	cd test-dir; ls -ln;
	rm -rf test-dir
