#!/usr/bin/make -f
# -*- makefile -*-
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.
#
# Modified to make a template file for a multi-binary package with separated
# build-arch and build-indep targets  by Bill Allombert 2001

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

include /usr/share/dpkg/architecture.mk

# This flag breaks the build.
export DEB_BUILD_MAINT_OPTIONS=reproducible=-fixdebugpath
DEB_CFLAGS_MAINT_APPEND += -Wall -g
DEB_CXXFLAGS_MAINT_APPEND += -Wall -g -fpermissive

ifeq ($(DEB_HOST_ARCH),armhf)
  DEB_CFLAGS_MAINT_APPEND += -marm
  DEB_CXXFLAGS_MAINT_APPEND += -marm
endif

include /usr/share/dpkg/buildflags.mk

confflags += --includedir="\$${prefix}/include/$(DEB_HOST_MULTIARCH)"
confflags += --disable-ppl_lpsol --disable-ppl_lcdd

# Disable the SWI-Prolog interface during architecture bootstrap, and
# on some architectures
interface_swi_prolog =
ifneq (,$(filter $(shell dh_listpackages),libppl-swi libppl-doc))
  interface_swi_prolog = ,swi_prolog
endif
confflags += --enable-interfaces=c,cxx$(interface_swi_prolog)

# If the version string was set using automake then the build would
# have to be configured before the dpkg-buildpackage command.
PPL_VERSION = $(shell grep "^AC_INIT" $(CURDIR)/configure.ac | sed -e "s/^AC_INIT.\[[^]]*\],[ \t]*\[\([^]]*\)\],.*/\1/")

ifneq (,$(filter $(DEB_HOST_ARCH), armel armhf powerpc))
  # powerpc runs out of memory building the test suite with -g, but is OK
  # with -gstabs instead.
  CHECK_ENV := CFLAGS="$(CFLAGS:-g=-gstabs)" CXXFLAGS="$(CXXFLAGS:-g=-gstabs)"
else
  CHECK_ENV :=
endif

%:
	dh $@

override_dh_auto_configure:
	 dh_auto_configure -- $(confflags)

override_dh_auto_test-arch:
ifneq (,$(filter $(DEB_HOST_ARCH), alpha armel s390x))
	# There are some failures in the testsuite on alpha,
	# maybe related to http://gcc.gnu.org/PR8966
	# Testsuite is miscompiled on arm, see #593324
	dh_auto_test -- -k $(CHECK_ENV) || true
else
	# FIXME: there are test failures with swi-prolog 7.2
	# for now just ignore these.
	dh_auto_test $(CHECK_ENV) || true
endif

override_dh_auto_test-indep:
	# Nothing to do

# Documentation not built by default, but also requires an arch build
override_dh_auto_build-indep:
	dh_auto_build
	save_size=10000 dh_auto_build --builddirectory=doc -- user-configured

# Destination directory for user manual.
DOC_DIR=$(CURDIR)/debian/tmp/usr/share/doc/ppl
override_dh_install-indep:
	mkdir -p $(DOC_DIR)/html
	mv $(DOC_DIR)/ppl-user-$(PPL_VERSION)-html $(DOC_DIR)/html/user-manual
	mv $(DOC_DIR)/ppl-user-$(PPL_VERSION).pdf $(DOC_DIR)/ppl-user-browse.pdf
	mv $(DOC_DIR)/ppl-user-$(PPL_VERSION).ps.gz $(DOC_DIR)/ppl-user-print.ps.gz
	rm $(DOC_DIR)/html/user-manual/jquery.js
	dh_install -i

override_dh_install-arch:
	-chrpath -d \
		debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libppl.so.14.0.0 \
		debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libppl_c.so.4.0.0 \
		debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/ppl/libppl_swiprolog.so
	dh_install -a

override_dh_installchangelogs:
	dh_installchangelogs ChangeLog
