#!/usr/bin/make -f

PRIVATE_LIBDIR := /usr/lib/digikam
export DEB_LDFLAGS_MAINT_APPEND := -Wl,--as-needed
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

# Augment path to find gphoto2-config; workaround for #826166
export PATH := $(PATH):/usr/lib/$(DEB_HOST_MULTIARCH)/libgphoto2-dev/bin


%:
	dh $@ --with kf6

override_dh_auto_configure:
	dh_auto_configure --buildsystem=kf6 -- \
	  -DBUILD_WITH_QT6=on \
	  -DCMAKE_INSTALL_RPATH="$(PRIVATE_LIBDIR)" \
	  -DBUILD_TESTING=OFF \
	  -DDIGIKAMSC_COMPILE_DOC=on \
	  -DDIGIKAMSC_COMPILE_PO=on \
	  -DENABLE_MYSQLSUPPORT=ON \
	  -DENABLE_INTERNALMYSQL=ON \
	  -DENABLE_KFILEMETADATASUPPORT=ON \
	  -DENABLE_AKONADICONTACTSUPPORT=ON \
	  -DENABLE_MEDIAPLAYER=ON \
	  -DENABLE_APPSTYLES=ON

#	help2man -n 'digital photo management application' --no-info obj-*/core/app/digikam > debian/man/digikam.1
#	help2man -n 'image viewer/editor' --no-info obj-*/core/showfoto/showfoto > debian/man/showfoto.1

override_dh_auto_install:
	dh_auto_install
	# G'MIC-Qt (komponent gmic-qt): bygges mot digiKam i debian/tmp,
	# før headere og CMake-filer slettes nedenfor.
	cmake -S gmic-qt -B obj-gmic-qt \
	  -DCMAKE_BUILD_TYPE=Release \
	  -DCMAKE_INSTALL_PREFIX=/usr \
	  -DCMAKE_PREFIX_PATH=$(CURDIR)/debian/tmp/usr \
	  -DCMAKE_MODULE_LINKER_FLAGS="$(LDFLAGS) -Wl,-rpath,$(PRIVATE_LIBDIR)" \
	  -DBUILD_WITH_QT6=ON -DGMIC_QT_HOST=digikam -DENABLE_SYSTEM_GMIC=OFF \
	  -DENABLE_ASAN=OFF -DBUILD_TESTING=OFF
	cmake --build obj-gmic-qt --parallel $(or $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS))),1)
	DESTDIR=$(CURDIR)/debian/tmp cmake --install obj-gmic-qt
	# remove development stuff: headers, cmake config files, pkg-config files, .so symlinks, static libs
	rm -rf --verbose debian/tmp/usr/include
	rm -rf --verbose debian/tmp/usr/share/kde4/apps/cmake
	rm -rf --verbose debian/tmp/usr/lib/cmake
	rm -rf --verbose debian/tmp/usr/lib/*/cmake
	rm -rf --verbose debian/tmp/usr/lib/pkgconfig
	find debian/tmp/usr/lib -type l -name '*.so' -exec rm --verbose {} \;
	# remove potentially conflicting oxygen icons in global icon theme
	rm -rf --verbose debian/tmp/usr/share/icons/oxygen
	# remove haar cascades, the versions in opencv-data will be used instead
	rm -rf --verbose debian/tmp/usr/share/kde4/apps/libkface/haarcascades
	# remove icons for non-installed test program
	rm -f --verbose debian/tmp/usr/share/icons/hicolor/*/apps/avplayer.*

override_dh_installchangelogs:
	dh_installchangelogs -pdigikam ChangeLog
	dh_installchangelogs --remaining-packages

override_dh_install:
	dh_install -pdigikam-private-libs -XGmic
	dh_install --remaining-packages
	dh_missing --fail-missing

override_dh_shlibdeps:
	dh_shlibdeps -l$(CURDIR)/debian/digikam-private-libs/$(PRIVATE_LIBDIR)

# auto tests require user interaction - override
override_dh_auto_test:
