#!/usr/bin/make -f
# -*- makefile -*-

export COMPIZ_DEB_BUILD=1

include /usr/share/dpkg/architecture.mk

gles2_architectures := armel armhf
build_testing := OFF

ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	build_testing := ON
endif

# currently, segfault if CMAKE_BUILD_TYPE=Release
export DEB_LDFLAGS_MAINT_STRIP=-Wl,-Bsymbolic-functions

CORE_ABIVERSION := $(shell sed -rn 's/^\#define[[:space:]]+CORE_ABIVERSION[[:space:]]+//p' include/core/abiversion.h )

override_dh_auto_configure:
	# right now, xorg-gtest won't compile on arm.
ifeq ($(DEB_HOST_ARCH),$(findstring $(DEB_HOST_ARCH), $(gles2_architectures)))
	dh_auto_configure -- -DCOMPIZ_BUILD_WITH_RPATH=FALSE -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCOMPIZ_PACKAGING_ENABLED=TRUE -DCOMPIZ_DISABLE_GS_SCHEMAS_INSTALL=OFF -DCOMPIZ_BUILD_TESTING=$(build_testing) -DBUILD_GLES=ON -DBUILD_XORG_GTEST=OFF -DCOMPIZ_WERROR=FALSE
else
	dh_auto_configure -- -DCOMPIZ_BUILD_WITH_RPATH=FALSE -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCOMPIZ_PACKAGING_ENABLED=TRUE -DCOMPIZ_DISABLE_GS_SCHEMAS_INSTALL=OFF -DCOMPIZ_BUILD_TESTING=$(build_testing) -DCOMPIZ_WERROR=FALSE
endif

override_dh_install:
	# cmake findcompiz_install use COMPIZ_DESTDIR and using COMPIZ_DESTDIR and
	# DESTDIR together is completely broken upstream
	# So, doing the same in debian/rules to find a way to install things in the
	# right place, sorry for the kittens
ifeq ($(DEB_HOST_ARCH),$(findstring $(DEB_HOST_ARCH), $(gles2_architectures)))
	cmake_dir=`cmake --system-information 2> /dev/null | grep "^CMAKE_ROOT " | sed -e 's/.*"\(.*\)"/\1/'` && \
	mkdir -p debian/tmp$${cmake_dir}/Modules && \
	mkdir -p debian/compiz-dev$${cmake_dir}/Modules && \
	cp cmake/FindCompiz.cmake debian/tmp$${cmake_dir}/Modules && cp cmake/FindOpenGLES2.cmake debian/compiz-dev$${cmake_dir}/Modules
else
	cmake_dir=`cmake --system-information 2> /dev/null | grep "^CMAKE_ROOT " | sed -e 's/.*"\(.*\)"/\1/'` && \
	mkdir -p debian/tmp$${cmake_dir}/Modules && \
	cp cmake/FindCompiz.cmake debian/tmp$${cmake_dir}/Modules
endif

	# remove .a files
	rm -f debian/tmp/usr/lib/*.a

	# remove acceptance test binary
	rm -f debian/tmp/usr/bin/compiz_autopilot_acceptance_tests

	dh_install --fail-missing

	# remove the compizconfig dev file from compiz-dev
	rm debian/compiz-dev/usr/share/compiz/cmake/LibCompizConfigCommon.cmake

override_dh_gencontrol:
	dh_gencontrol -- -Vcoreabiversion=$(CORE_ABIVERSION)

override_dh_auto_test:
	dh_auto_test --no-parallel

%:
	dh $@ --parallel --with python3,translations
