#!/usr/bin/make -f

include /usr/share/dpkg/architecture.mk

export DEB_BUILD_MAINT_OPTIONS = hardening=+all nocheck
export DEB_CFLAGS_MAINT_APPEND = -fvisibility=hidden
export DEB_LDFLAGS_MAINT_APPEND = -Wl,-z,defs -Wl,--as-needed -lm -ldl

ifneq (,$(filter $(DEB_HOST_ARCH), armel armhf))
	arch_flags += --with-opengl=es --enable-egl
endif
ifneq (,$(filter $(DEB_HOST_ARCH), arm64 s390x))
	arch_flags += --enable-lua-old
	dhinstallflags += --exclude=elua
endif
ifneq (,$(filter $(DEB_HOST_ARCH), armhf))
	arch_flags += --disable-neon
endif


ifeq (linux,$(DEB_HOST_ARCH_OS))
	arch_flags += --enable-fb
endif
LETSBUILDINDEP := $(shell if dh_listpackages | grep -q efl-doc ; then \
	echo yes ;\
	else echo no ; fi)

ifeq (,$(findstring nodoc,$(DEB_BUILD_OPTIONS)))
	docs_flag += --enable-doc
else
	docs_flag += --disable-doc
endif

%:
	dh $@

override_dh_auto_configure:
	dh_auto_configure --verbose -- \
	--disable-rpath \
	--disable-silent-rules \
	$(docs_flag) \
	--with-profile=release \
	--with-tests=none \
	--with-crypto=gnutls \
	--disable-tslib \
	--enable-liblz4 \
	--enable-xinput22 \
	--enable-image-loader-webp \
	--enable-harfbuzz \
	--enable-systemd \
	--enable-static \
	--enable-image-loader-webp \
	--with-generic_vlc \
	--enable-wayland \
	--enable-elput \
	--enable-drm \
	--enable-scim \
	$(arch_flags)

override_dh_auto_build:
	find -name '*shader*.x' -exec rm {} \;
	$(CURDIR)/debian/fake_home.sh env EFL_SHD_REGEN=1 \
		dh_auto_build --verbose
ifeq ($(filter yes,$(LETSBUILDINDEP)),yes)
	mkdir -p doc/html
	$(CURDIR)/debian/fake_home.sh $(MAKE) doc
endif

override_dh_auto_build-indep:
	mkdir -p doc/html
	$(MAKE) doc

# The install phase doesn't work reliably in parallel
# Force it to run with only 1 thread to avoid issues
override_dh_auto_install:
	$(MAKE) -j1 DESTDIR=$(CURDIR)/debian/tmp install

override_dh_install:
	dh_install $(dhinstallflags)
ifeq ($(DEB_HOST_ARCH_OS),linux)
	dh_install -plibefl-all-dev $(dhinstallflags) \
		debian/tmp/usr/lib/*/libecore_fb.so
endif
	# remove unused file, fail otherwise
	rm -f debian/efl-doc/usr/share/doc/efl-doc/html/jquery.js
	if grep -qr jquery.js debian/efl-doc/usr/share/doc/efl-doc/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_missing:
	dh_missing --list-missing

override_dh_makeshlibs:
	dh_makeshlibs -Xmodule.so

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

override_dh_strip:
	dh_strip --ddeb-migration='efl-dbg (<< 1.8.1-2~)'

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