#!/usr/bin/make -f
# -*- makefile-gmake -*-
# Uncomment this to turn on verbose mode.
export DH_VERBOSE=1
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
# We install both libraries as private (no rdeps in Debian).
export DEB_LDFLAGS_MAINT_APPEND := -Wl,-rpath,/usr/lib/ball
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

# Temporary workaround for GCC PR rtl-optimization/127142
# (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127142) which leads to
# FTBFS on hppa with GCC 16.2.0:
# https://buildd.debian.org/status/fetch.php?pkg=ball&arch=hppa&ver=1.7.4-1&stamp=1790240580&raw=0
# TODO: Remove when GCC 17 becomes the default compiler or if the fix
# is eventually backported to 16.3.0.
ifeq ($(DEB_HOST_ARCH),hppa)
export DEB_CXXFLAGS_MAINT_APPEND := -O1
endif

common_targets := BALL VIEW BALLView

%:
	dh $@ --buildsystem=cmake

override_dh_auto_configure:
# The plugins are not installed so there is no sense to build them.
# BALL_LICENSE=GPL is needed to allow performing the detection tests
# for OpenBabel/FFTW and link with them if found.
	dh_auto_configure -- \
	  -DCMAKE_INSTALL_LIBDIR=lib/ball \
	  -DBALL_PATH=/usr \
	  -DBALL_DATA_PATH=/usr/share/BALL \
	  -DBALL_LICENSE=GPL \
	  -DBALL_PYTHON_SUPPORT=OFF \
	  -DBALL_BUILD_BALLAXY=OFF \
	  -DUSE_PRESENTABALL_PLUGIN=OFF

# Set BALL_DATA_PATH so that test scripts find it
export BALL_DATA_PATH=$(CURDIR)/data
# Make sure HOME exists (for Directory_test)
export HOME=/tmp

# The testsuite issues several failures
# Run it, but ignore the return code for now.
override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	dh_auto_test --no-parallel -- || true
endif

# Different tests are failing in different runs

#The following tests FAILED:
#         86 - Composite_test4 (Failed)
#         87 - Composite_test5 (Failed)
#Errors while running CTest

#The following tests FAILED:
#        112 - Fragment_test (Failed)
#        113 - Chain_test (Failed)
#Errors while running CTest

override_dh_auto_build:
	dh_auto_build -- $(common_targets)
# Don't build the tests if they are not going to be run.
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	dh_auto_build -- build_tests
endif

override_dh_auto_clean:
	dh_auto_clean
	rm -f include/BALL/BALL.doc

execute_after_dh_install-arch:
# Save the config.h header as it will be needed for the autopkgtest to
# compile the example programs.
	cp debian/ballview/usr/include/BALL/CONFIG/config.h \
	  debian/ballview/usr/lib/ball
# Delete unnecessary headers and CMake files.
	rm -r debian/ballview/usr/include
	rm -r debian/ballview/usr/lib/ball/cmake
# Convert icons to PNG since AppStream does not support XPM.
	for icon in data/graphics/ballview*.xpm; do \
	  convert $$icon \
	    debian/ballview/usr/share/pixmaps/`basename -s .xpm $$icon`.png; \
	done

execute_after_dh_install-indep:
# Delete files that trigger Lintian complaints.
	rm -r debian/ballview-data/usr/share/doc/BALL
	rm debian/ballview-data/usr/share/BALL/graphics/license.txt
	rm debian/ballview-data/usr/share/BALL/graphics/*.pdf
# Fix lintian W: national-encoding.
	iconv -f ISO-8859-1 -t UTF-8 < data/structures/test.btf \
	  > debian/ballview-data/usr/share/BALL/structures/test.btf
	iconv -f ISO-8859-1 -t UTF-8 < data/gui-test/tutorial.btf \
	  > debian/ballview-data/usr/share/BALL/gui-test/tutorial.btf

override_dh_makeshlibs:
# Avoid lintian warning for the private libraries.
	dh_makeshlibs --noscripts
