#!/usr/bin/make -f
# -*- makefile -*-
# Package building rules for the SimGrid Debian Package.
# Strongly inspirated from the sample debhelper file, originally written by Joey Hess and Craig Small.
# Adapted to SimGrid by Lucas Nussbaum and Martin Quinson.

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

# See 'man dpkg-architecture'
include /usr/share/dpkg/architecture.mk

CMAKE_FLAGS=

ifneq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
  CFLAGS += -O0
  CMAKE_FLAGS += -Denable_compile_optimizations=off
else
  CFLAGS += $(shell dpkg-buildflags --get CFLAGS)
  # build with -O3 everywhere since it is upstream policy to do so.
  CFLAGS += -O3
endif	

ifneq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
  CMAKE_FLAGS += -Denable_documentation=off
endif

export DEB_BUILD_MAINT_OPTIONS += hardening=+all

ifeq ($(DEB_HOST_GNU_CPU),arm)
  CFLAGS += -funwind-tables
endif

LDFLAGS += $(shell dpkg-buildflags --get LDFLAGS)

%:
	dh $@ --with python3 --parallel

# CMAKE_BUILD_TYPE= (unset CMAKE_BUILD_TYPE) required, because:
# - dh magic runs cmake with -DCMAKE_BUILD_TYPE=RelWithDebInfo
# - this adds -DNDEBUG to CFLAGS (maybe a bug, see #711515)
# - this breaks simgrid (or at least some tests).
override_dh_auto_configure:
	CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)" \
	  dh_auto_configure -Scmake -- \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_BUILD_TYPE= \
		-Denable_smpi_MPICH3_testsuite=ON \
		-Denable_java=ON -Denable_lib_in_jar=OFF \
		${CMAKE_FLAGS}

override_dh_auto_build:
	dh_auto_build -- all documentation

# FIXME renable tests at some point
override_dh_auto_test:
	dh_auto_test || true

# Make install and prepare package building
override_dh_auto_install:
	dh_auto_install
	chrpath -d debian/tmp/usr/bin/graphicator
	chrpath -d debian/tmp/usr/bin/smpimain
	mv debian/tmp/usr/bin/graphicator debian/tmp/usr/bin/simgrid-graphicator
	# move doc to correct place
	mkdir -p debian/tmp/usr/share/doc/simgrid
	mv debian/tmp/usr/doc/simgrid/* debian/tmp/usr/share/doc/simgrid/
	mv debian/tmp/usr/share/doc/simgrid/html/javadoc debian/tmp/usr/share/doc/simgrid/
	rmdir debian/tmp/usr/doc/simgrid
	# move jarfile to the right location
	mkdir -p debian/tmp/usr/share/java
	mv debian/tmp/usr/java/simgrid.jar debian/tmp/usr/share/java
	rm -rf debian/tmp/usr/java
	# Don't embeed jquery (unless we are compiling with nodoc)
	if [ -e debian/tmp/usr/share/doc/simgrid/html/jquery.js ] ; then \
	  rm debian/tmp/usr/share/doc/simgrid/html/jquery.js ; \
	  ln -s /usr/share/javascript/jquery/jquery.js debian/tmp/usr/share/doc/simgrid/html/jquery.js ; \
	fi

# Ensure that no file where left over when building the package
override_dh_install:
	dh_install --fail-missing

# Recreate the upstream tarball, removing the UMPIRE test suites as they are non-free

version=$(shell uscan --dehs 2>/dev/null|grep upstream-version|sed 's/[^>]*>//'|sed "s/<.*$$//")
tmpdir:=$(shell mktemp -d)
wrkdir:=$(shell pwd)

get-orig-source:
	rm -f ${wrkdir}/../simgrid_${version}+dfsg.orig.tar.gz
	rm -f ${wrkdir}/../simgrid_${version}.orig.tar.gz 
	uscan --verbose --force-download && cp ../SimGrid-${version}.tar.gz ${tmpdir} || true
	set +ex; cd ${tmpdir} && \
	  tar xf SimGrid-${version}.tar.gz && rm -f SimGrid-${version}.tar.gz && \
	  rm -rf SimGrid-${version}/teshsuite/smpi/isp/umpire/ && \
	  tar cf ${wrkdir}/../simgrid_${version}+dfsg.orig.tar * ; \
	gzip --best ${wrkdir}/../simgrid_${version}+dfsg.orig.tar
	rm -rf ${tmpdir}
	@echo "successfully created new tarball: ${wrkdir}/../simgrid_${version}+dfsg.orig.tar.xz"
