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

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

export DPKG_GENSYMBOLS_CHECK_LEVEL=4

# Workaround https://gitlab.com/ubports/development/core/packaging/libhybris/-/issues/21
# Basically, libhybris' libEGL implementation crash inside libhybris itself if
# there's no Hybris/Halium environment. This variable forces GLVND's libEGL
# loader to consider only Mesa's implementation and ignore libhybris one.
# On CI, Mesa will simply use software rendering.
export __EGL_VENDOR_LIBRARY_FILENAMES=/usr/share/glvnd/egl_vendor.d/50_mesa.json

BUILDHOME=/tmp/home

# Add migrations add-on when build on Ubuntu & original name is available.
DH_MIGRATIONS = $(shell type dh_migrations >/dev/null 2>&1 && echo "--with migrations")

%:
	dh $@ $(DH_MIGRATIONS)

apparmor:
	aa-easyprof -m ./debian/morph-browser-apparmor.manifest --no-verify | \
	egrep -v '(# Click packages|CLICK_DIR)' | \
	sed 's/@{APP_PKGNAME}_@{APP_APPNAME}_@{APP_VERSION}/@{APP_PKGNAME}/g' | \
	egrep -v 'deny /run/udev/data/\*\* r,' | \
	sed 's#^}$$#\n  /sys/class/ r,\n  /sys/class/input/ r,\n  /run/udev/data/** r,\n}#g' | \
	egrep -v '^\s*deny /dev/ r,\s*$$' | \
	sed 's#^\(\s*\)deny\(\s\+/{run,dev}/shm/pulse-shm\*\s\+w,\).*$$#\1owner\2#g' | \
	sed 's#^}$$#\n  /dev/dri/ r,\n  /sys/devices/pci[0-9]*/**/config r,\n}#g' | \
	sed 's#^}$$#\n  @{HOME}/@{XDG_DOWNLOAD_DIR}/** rwk,\n}#g' \
	> ./debian/usr.bin.morph-browser
ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
	apparmor_parser -QTK ./debian/usr.bin.morph-browser
endif

override_dh_auto_configure:
	dh_auto_configure --builddirectory=build-qt6 -- -DENABLE_QT6=ON -DENABLE_WERROR=OFF
	dh_auto_configure --builddirectory=build-qt5 -- -DENABLE_WERROR=OFF

override_dh_auto_build:
	dh_auto_build --builddirectory=build-qt6 ..
	dh_auto_build --builddirectory=build-qt5 ..

override_dh_auto_install:
	dh_auto_install --builddirectory=build-qt6 --destdir=debian/tmp-qt6 ..
	dh_auto_install --builddirectory=build-qt5 --destdir=debian/tmp-qt5 ..

override_dh_install: apparmor
	install -D -m644 ./debian/usr.bin.morph-browser ./debian/tmp-qt5/etc/apparmor.d/usr.bin.morph-browser
	install -D -m644 ./debian/usr.bin.morph-browser ./debian/tmp-qt6/etc/apparmor.d/usr.bin.morph-browser
	dh_install -Nmorph-browser-qt6 -Nmorph-webapp-container-qt6 -Nqml6-module-morph-web --sourcedir=debian/tmp-qt5
	dh_install -pmorph-browser-qt6 -pmorph-webapp-container-qt6 -pqml6-module-morph-web --sourcedir=debian/tmp-qt6

override_dh_missing:
	dh_missing --fail-missing

override_dh_installdeb:
	dh_apparmor --profile-name=usr.bin.morph-browser -pmorph-browser
	dh_apparmor --profile-name=usr.bin.morph-browser -pmorph-browser-qt6
	dh_installdeb

override_dh_auto_clean:
	dh_auto_clean --builddirectory=build-qt6
	dh_auto_clean --builddirectory=build-qt5

override_dh_clean:
	dh_clean
	rm -rf $(BUILDHOME)

override_dh_auto_test:
	mkdir -p $(BUILDHOME)
	-HOME=$(BUILDHOME) dh_auto_test --no-parallel --builddirectory=build-qt6 -- ARGS+=-VV
	HOME=$(BUILDHOME) dh_auto_test --no-parallel --builddirectory=build-qt5 -- ARGS+=-VV
