#!/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), alpha arm64 hppa ia64 m68k powerpc ppc64 ppc64el riscv64 s390x sh4 sparc64 x32))
	lua_flags = -Dlua-interpreter=lua -Delua=false -Dbindings=
	dhinstallflags += --exclude=elua
else
	lua_flags = -Delua=true -Dbindings=lua

endif

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

%:
	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=gnutls \
		-Dtslib=false \
		-Dembedded-lz4=false \
		-Devas-loaders-disabler=json,avif \
		-Dnetwork-backend=none \
		-Dwl=true \
		-Ddrm=true \
		-Dbuild-tests=false \
		$(arch_flags) \
		$(fb_flags) \
		$(lua_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

	$(CURDIR)/debian/fake_home.sh env \
		dh_auto_build --verbose
ifeq ($(filter efl-doc,$(BINPKGS)),efl-doc)
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS) $(DEB_BUILD_PROFILES)))
	$(CURDIR)/debian/fake_home.sh dh_auto_build -- doc
endif
endif

override_dh_auto_build-indep:
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS) $(DEB_BUILD_PROFILES)))
	$(CURDIR)/debian/fake_home.sh dh_auto_build -- doc
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

override_dh_install:
	dh_install $(dhinstallflags)

	# 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

execute_before_dh_missing:
ifneq ($(filter libelua1,$(BINPKGS)),libelua1)
	# upstream installs some elua files even if elua is disabled
	echo 'usr/share/elua*' >> debian/not-installed
endif

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
