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

include /usr/share/dpkg/architecture.mk
include /usr/share/mpi-default-dev/debian_defaults

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# FETK needs SuperLU, make sure the system library is used
SUPERLU_INCLUDES = $(shell pkg-config --cflags-only-I superlu | sed "s/^-I//" )
SUPERLU_LIBRARIES = $(shell pkg-config --libs superlu | sed "s/^-l//" )

BUILDDIR := $(CURDIR)/obj-$(DEB_HOST_MULTIARCH)

export CPPFLAGS := $(shell dpkg-buildflags --get CPPFLAGS)
export CFLAGS   := $(shell dpkg-buildflags --get CFLAGS) $(CPPFLAGS) -fcommon
export CXXFLAGS := $(shell dpkg-buildflags --get CXXFLAGS) $(CPPFLAGS)
export LDFLAGS  := $(shell dpkg-buildflags --get LDFLAGS) -Wl,--as-needed

%:
	dh $@ --with python3,sphinxdoc --buildsystem=cmake

override_dh_auto_configure:
	dh_auto_configure -- \
		-DCMAKE_BUILD_TYPE=None \
		-DAPBS_STATIC_BUILD=Off \
		-DBUILD_DOC=Off -DBUILD_TOOLS=On \
		-DENABLE_OPENMP=On -DENABLE_MPI=On \
		-DENABLE_PYTHON=On -DBUILD_SHARED_LIBS=On \
		-DENABLE_READLINE=Off -DENABLE_ZLIB=On \
		-DFETCHCONTENT_SOURCE_DIR_FETK=$(CURDIR)/debian/external_deps/fetk \
		-DRPC_CFLAGS=$(shell pkg-config --cflags libtirpc) \
		-DBUILD_MALOC=Off

execute_after_dh_auto_build-indep: export http_proxy=127.0.0.1:9
execute_after_dh_auto_build-indep: export https_proxy=127.0.0.1:9
execute_after_dh_auto_build-indep:
	PYTHONPATH=$(BUILDDIR)/tools/python sphinx-build -b html docs $(BUILDDIR)/docs/_build/html # HTML generator

override_dh_auto_test:
# run tests after installation

execute_after_dh_auto_install:
	-(cd tests; PATH=$(CURDIR)/debian/tmp/usr/bin:$${PATH} LD_LIBRARY_PATH=$(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH):$${LD_LIBRARY_PATH} PYTHONPATH=$${PYTHONPATH}:$(CURDIR)/tools/manip python3 apbs_tester.py -c test_cases.cfg; rm -rf __pycache__)

override_dh_fixperms-indep:
	dh_fixperms
	for example in FKBP/1d7h-dmso/UHBD/pqr2qcd \
			ion-pmf/runme.sh \
			point-pmf/runme.sh \
			protein-rna/postprocess.sh \
			protein-rna/test.sh \
			helix/Run_membrane-helix.sh \
			protein-rna/run_apdx_files.sh; \
	do \
	  chmod +x debian/apbs-data/usr/share/apbs/examples/$${example}; \
	done
	for script in amber2charmm.sh \
			param/pdb2pqr/amber2uhbd.sh \
			qcd2pqr.awk; \
	do \
	  chmod a+x debian/apbs-data/usr/share/apbs/tools/conversion/$${script}; \
	done
	for example in bem-pKa/test_proteins \
			pbsam-barn_bars; \
	do \
	  chmod a-x debian/apbs-data/usr/share/apbs/examples/$${example}/*.pqr; \
	done
