#!/usr/bin/make -f

include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/pkg-info.mk

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# -O3 is used by default with the meson build.  -O2 triggers cxx weak
# -symbols in libephysics1 that shouldn't be there.
export DEB_CFLAGS_MAINT_APPEND = -fvisibility=hidden -O3
export DEB_CXXFLAGS_MAINT_APPEND = -fvisibility=hidden -O3
export DEB_LDFLAGS_MAINT_APPEND = -Wl,-z,defs -Wl,--as-needed

ifneq (,$(filter $(DEB_HOST_ARCH), armhf armel))
	arch_flags += -Dnative-arch-optimization=false
endif

BINPKGS := $(shell dh_listpackages | paste -sd " ")

ifeq (linux,$(DEB_HOST_ARCH_OS))
	fb_flags += -Dfb=true
endif

# upstream's default is to not build the docs.  Enable it, unless:
# 1) efl-doc binary pkg was suppressed (e.g. due to an only-arch-any build)
# 2) nodoc is specified in the build options
ifeq ($(filter efl-doc,$(BINPKGS)),efl-doc)
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	doc_flags += -Ddocs=true
endif
endif

%:
	dh $@

override_dh_auto_configure:
# cross-builds need host eolian_gen, but ensure the versions match
ifneq (,$(filter cross,$(DEB_BUILD_PROFILES)))
	test "`dpkg-query -f '$${Version}' -W libedje-bin | sed 's/+b[0-9]*$$//'`" = "$(DEB_VERSION)"
	test "`dpkg-query -f '$${Version}' -W libeet-bin | sed 's/+b[0-9]*$$//'`" = "$(DEB_VERSION)"
	test "`dpkg-query -f '$${Version}' -W libelementary-bin | sed 's/+b[0-9]*$$//'`" = "$(DEB_VERSION)"
	test "`dpkg-query -f '$${Version}' -W libeolian-bin | sed 's/+b[0-9]*$$//'`" = "$(DEB_VERSION)"
endif

	$(CURDIR)/debian/validate_symvers.sh

	dh_auto_configure --verbose -- \
		-Dcrypto=openssl \
		-Dtslib=false \
		-Dembedded-lz4=false \
		-Dembedded-libunibreak=false \
		-Devas-loaders-disabler=json,avif \
		-Dnetwork-backend=none \
		-Dwl=true \
		-Ddrm=true \
		-Dbuild-tests=false \
		-Dlua-interpreter=lua \
		-Dbindings= \
		$(arch_flags) \
		$(fb_flags) \
		$(doc_flags)

override_dh_auto_build:
	# ensure shaders are rebuilt from sources
	find -name '*shader*.x' -exec rm {} \;
	cd src && lib/ector/gl/shader/gen_shaders.sh
	cd src && modules/evas/engines/gl_common/shader/gen_shaders.sh

	dh_auto_build --verbose
ifeq ($(filter efl-doc,$(BINPKGS)),efl-doc)
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	dh_auto_build -- doc
endif
endif

override_dh_auto_build-indep:
ifeq ($(filter efl-doc,$(BINPKGS)),efl-doc)
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	dh_auto_build -- doc
endif
endif

execute_before_dh_install:
	# dh_fixperms misses this one for some reason
	chmod -x debian/tmp/usr/share/elementary/images/animated_webp_image.webp

execute_after_dh_install:
	# remove unused file, fail otherwise
	rm -f obj-$(DEB_HOST_GNU_TYPE)/html/jquery.js
	if grep -qr jquery.js obj-$(DEB_HOST_GNU_TYPE)/html/ ; \
		then echo jquery is used ; false ; fi
	# this is shipped under /usr/share/docs
	rm -f debian/libelementary-data/usr/share/icons/Enlightenment-X/README

override_dh_makeshlibs:
	dh_makeshlibs -Xmodule.so

override_dh_shlibdeps:
	dh_shlibdeps -Nlibefreet1a
	dh_shlibdeps -plibefreet1a -- -xlibefreet-bin

override_dh_clean:
	dh_clean
	find -name '*.luac' -exec rm {} \;

override_dh_installexamples:
	dh_installexamples -X.gitignore

override_dh_auto_test-indep:

override_dh_auto_test-arch:

override_dh_installman:
	dh_installman
ifeq ($(filter efl-doc,$(BINPKGS)),efl-doc)
	dh_installman -pefl-doc --sourcedir obj-$(DEB_HOST_GNU_TYPE)
endif

override_dh_installdocs:
	dh_installdocs
ifeq ($(filter efl-doc,$(BINPKGS)),efl-doc)
	dh_installdocs -pefl-doc --doc-main-package=efl-doc obj-$(DEB_HOST_GNU_TYPE)/html
endif
