#!/usr/bin/make -f
# -*- makefile -*-

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk
CFLAGS+=$(CPPFLAGS)
CXXFLAGS+=$(CPPFLAGS)

modulesfile = debian/ants.environment-modules

include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/pkg-info.mk

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

# Exclude all heavy tests while building on slowish build
# bots/platforms
BOGOMIPS = $(shell awk '/^bogomips/{print $$3;}' /proc/cpuinfo | sed -ne '1s,\..*,,gp' )
CTEST_EXCLUDE = $(shell [ "$(BOGOMIPS)" -gt 2000 ] || echo "-E '^ANTS(_|PSE)'")

export SHELL=/bin/bash

# Default mega-rule
%:
	dh $@


override_dh_auto_configure:
	dh_auto_configure -- \
		-DANTS_SUPERBUILD:BOOL=OFF \
		-DANTS_USE_GIT_PROTOC:BOOL=OFF \
		-DCOPY_SCRIPT_FILES_TO_BIN_DIR:BOOL=OFF \
		-DCMAKE_SKIP_RPATH:BOOL=OFF \
		-DUSE_SYSTEM_ITK:BOOL=ON \
		-DUSE_VTK:BOOL=OFF \
		-DBUILD_SHARED_LIBS:BOOL=ON \
		-DRUN_SHORT_TESTS:BOOL=ON \
		-DRUN_LONG_TESTS:BOOL=OFF \
		-DEXECUTABLE_OUTPUT_PATH:PATH=$(DESTBINDIR) \
		-DCMAKE_INSTALL_RPATH:STRING="/usr/lib/ants/lib" \
		--debug-output

%modules: %modules.in
	sed -e 's,$$UVERSION,$(DEB_VERSION_UPSTREAM),g' $< >| $@

override_dh_auto_build: $(modulesfile)
	dh_auto_build

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	cd obj-*; \
	 ctest --force-new-ctest-process $(CTEST_EXCLUDE)
endif

override_dh_auto_install:
	dh_auto_install
	: Move binaries under usr/lib/ants
	mv debian/ants/usr/bin/* debian/ants/usr/lib/ants
	: Move shared libs under usr/lib/ants/lib
	mkdir -p debian/ants/usr/lib/ants/lib
	mv debian/ants/usr/lib/$(DEB_HOST_MULTIARCH)/* debian/ants/usr/lib/ants/lib
	rm -rf debian/ants/usr/lib/$(DEB_HOST_MULTIARCH)/
	: Symlink binaries of primary importance and with ants prefix into /usr/bin, skipping .sh suffix
	for fp in debian/ants/usr/lib/ants/{ants*,ANTS*,WarpImageMultiTransform,WarpTimeSeriesImageMultiTransform,Atropos,LaplacianThickness}; do \
		f=$$(basename $$fp); \
		dh_link usr/lib/ants/$$f usr/bin/$${f%.sh}; \
	done
	: Install shell scripts
	install -t debian/ants/usr/lib/ants Scripts/*
	: Install modules file
	install -m 664 $(modulesfile) debian/ants/usr/share/modules/modulefiles/ants/$(DEB_VERSION_UPSTREAM)
	: Adjust ANTSPATH
	cd debian/ants/usr/lib/ants && sed -ie 's,\([^"=]*\(/bin/ants\|ANTS/release/bin\)\),/usr/lib/ants,g' *.sh *.pl && rm *.she *.ple

override_dh_installman:
	: Create and install manpages for scripts symlinked from /usr/bin
	# Putting the path to shared libs in LD_LIBRARY_PATH so that calls to
	# executable files by help2man succeed.
	cd debian/ants/usr/bin && mkdir -p ../../../../manGen && \
    for f in *; do \
	 LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:../lib/ants/lib" \
	 help2man --name="part of ANTS registration suite" --version-string $(DEB_VERSION_UPSTREAM) \
	          --no-info --no-discard-stderr ./$$f >| ../../../../manGen/$$f.1; \
     done
	dh_installman manGen/*

override_dh_auto_clean:
	dh_auto_clean
	rm -rf $(BUILDDIR) debian/ants.environment-modules manGen/
