#!/usr/bin/make -f
# -*- makefile -*-
include /usr/share/dpkg/default.mk

%:
	dh $@

INSTALL_DIR = debian/libtgowt-dev
INCLUDE_DIR = $(INSTALL_DIR)/usr/include/tg_owt

# Do not link PipeWire unconditionally and reduce required dependencies.
EXTRA_CMAKE_VARIABLES += TG_OWT_DLOPEN_PIPEWIRE=ON

ifeq ($(DEB_HOST_ARCH),armhf)
# WebRTC provides no runtime NEON checking, forbid unconditionally usage.
EXTRA_CMAKE_VARIABLES += TG_OWT_ARCH_ARMV7_USE_NEON=OFF
endif

override_dh_auto_configure:
	dh_auto_configure -- $(EXTRA_CMAKE_VARIABLES:%=-D%)

execute_after_dh_auto_install:
# Cut non UTF-8 bytes off to silence national-encoding Lintian warning. Regex taken from https://stackoverflow.com/a/1401716/5000805
	find $(INCLUDE_DIR) -type f -print0 | xargs -0 perl -pi -e 's/((?:[\x00-\x7F]|[\xC0-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}|[\xF0-\xF7][\x80-\xBF]{3}){1,100})|./$$1/g'
# Skip empty directories to fix package-contains-empty-directory Lintian info diagnostic.
	find $(INCLUDE_DIR) -empty -type d -delete
# Delete no longer used headers.
	$(RM) -r $(INCLUDE_DIR)/third_party/libyuv

# Gather libraries needed for linking, search for their development and runtime packages, put them as dependencies.
execute_before_dh_gencontrol:
	( find $(INSTALL_DIR) -name '*.cmake' | xargs grep -h -o '[^";]*\.so\>' ;\
		echo /usr/lib/$(DEB_HOST_MULTIARCH)/libabsl_base.so ) |\
		xargs dpkg -S > debian/libtgowt-dev.deps.1
	cut -d: -f3 debian/libtgowt-dev.deps.1 | xargs realpath | xargs dpkg -S > debian/libtgowt-dev.deps.2
	sort debian/libtgowt-dev.deps.? > debian/libtgowt-dev.deps
	echo static:Depends=$$(cut -d: -f1 debian/libtgowt-dev.deps | uniq | sed 's/$$/,/') >> debian/libtgowt-dev.substvars
ifeq ($(filter terse,$(DEB_BUILD_OPTIONS)),)
	column -t debian/libtgowt-dev.deps
endif

execute_after_dh_clean:
	$(RM) debian/libtgowt-dev.deps debian/libtgowt-dev.deps.?
