#!/usr/bin/make -f

command = $(shell /bin/sh -c "command -v $(1)")

ifneq (,$(call command, dh_octave_version))
DH_OPTIONS = --with octave
endif

ifneq (,$(call command, pybuild))
DH_OPTIONS += --buildsystem=pybuild
PYTHON3 := $(shell pybuild --system distutils --print {interpreter.include_dir})
endif

%:
	dh $@ $(DH_OPTIONS)

export DEB_BUILD_MAINT_OPTIONS = hardening=+all optimize=-lto

# drop back to C++14 (https://github.com/janverschelde/PHCpack/issues/55)
export DEB_CXXFLAGS_MAINT_APPEND = -std=gnu++14

ifeq (,$(filter terse,$(DEB_BUILD_OPTIONS)))
GPRBUILD_OPTS = -v
else
GPRBUILD_OPTS =
endif

GPRBUILD_OPTS += -R # prevent custom-library-search-path lintian error

ifneq (,$(call command, gnatgcc))
gnat_version := $(shell gnatgcc -dumpversion)
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk
include /usr/share/ada/debian_packaging-$(gnat_version).mk
endif

export PYBUILD_DIR = src/Python/PHCpy3

override_dh_auto_build-arch:
	@echo "+------------------+"
	@echo "| building PHCpack |"
	@echo "+------------------+"
	cd src/Ada/Main && gprbuild $(GPRBUILD_OPTS) main.gpr
	cp src/bin/phctop src/bin/phc
	$(MAKE) -C src/doc man
ifeq (,$(filter nopython, $(DEB_BUILD_PROFILES)))
	@echo "+----------------+"
	@echo "| building PHCpy |"
	@echo "+----------------+"
	cd src/Ada/Main && gprclean main.gpr # so we can recompile with -fPIC
	$(MAKE) -C src/Objects phcpy2c3.so \
		PYTHON3=$(PYTHON3)         \
		GNATFLAGS="$(ADAFLAGS)"
	pybuild --build
ifeq (,$(filter nodoc, $(DEB_BUILD_OPTIONS)))
	@echo "+---------------------+"
	@echo "| building PHCpy docs |"
	@echo "+---------------------+"
	$(MAKE) -C src/Python/PHCpy3/doc html
	jdupes -l src/Python/PHCpy3/doc/build/html/_images/math
endif
endif

ifeq (,$(filter nodoc, $(DEB_BUILD_OPTIONS)))
override_dh_auto_build-indep:
	@echo "+-----------------------+"
	@echo "| building PHCpack docs |"
	@echo "+-----------------------+"
	$(MAKE) -C src/doc html
endif

# skip upstream test suite for now
# https://github.com/janverschelde/PHCpack/issues/41
# override_dh_auto_test:
#	$(MAKE) -C src/Objects testall

M_FILES := $(patsubst src/Octave/%.m,inst/%.m,$(wildcard src/Octave/*.m))

execute_before_dh_octave_version: DESCRIPTION

execute_after_dh_auto_install-indep: phclab-banner COPYING DESCRIPTION \
	inst/PKG_ADD $(M_FILES)
	dh_auto_install --buildsystem=octave
# dh_octave installs everything in debian/phcpack, since that's the name of the
# source package, but we want the files under debian/tmp where dh_install can
# find them
	mkdir -p debian/tmp/usr/share
	mv debian/phcpack/usr/share/octave debian/tmp/usr/share
	rm -rf debian/phcpack

phclab-banner:
	@echo "+-----------------+"
	@echo "| building PHClab |"
	@echo "+-----------------+"

COPYING: src/Octave/COPYING.txt
	cp $< $@

include /usr/share/dpkg/pkg-info.mk
DESCRIPTION: debian/DESCRIPTION.in
	sed -e "s/@VERSION@/$(DEB_VERSION_UPSTREAM)/" \
	    -e "s/@DATE@/$(shell date -u -I -d@$(SOURCE_DATE_EPOCH))/" $< > $@

inst/PKG_ADD: inst
	echo 'set_phcpath("/usr/bin/phc")' > $@

inst/%.m: src/Octave/%.m inst
	cp $< $@

inst:
	mkdir -p $@

override_dh_octave_substvar:
	dh_octave_substvar -poctave-phclab

override_dh_clean:
	dh_clean -Xbak

execute_after_dh_auto_clean-arch:
	$(MAKE) -C src/Objects clean
	$(MAKE) -C src/Python/PHCpy3/doc clean

execute_after_dh_auto_clean-indep:
	$(MAKE) -C src/doc clean
	dh_auto_clean --buildsystem=octave || true
	rm -rf inst

.PHONY: phclab-banner
