#!/usr/bin/make -f

#DH_VERBOSE=1
export PYBUILD_NAME=onboard
export PYBUILD_INSTALL_ARGS=--root=$(CURDIR)/debian/tmp
export PYBUILD_TEST_PYTEST=1

export DPKG_EXPORT_BUILDFLAGS = 1
export DEB_BUILD_MAINT_OPTIONS = optimize=+lto hardening=+all

%:
	dh $@ --with python3 --buildsystem=pybuild

override_dh_auto_clean:
# Don't fail to build from git-buildpackage:
	-dh_auto_clean
	rm -rf .pybuild
	rm -rf Onboard/osk*.so
	rm -rf Onboard/pypredict/lm*.so
	rm -f  po/onboard.pot
	rm -rf onboard.egg-info/
	rm -rf debian/home/
	rm -rf debian/data/

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	cp   onboard          $$(find .pybuild/cpython3_*_onboard -maxdepth 0 | head -n1)/build
	cp   onboard-settings $$(find .pybuild/cpython3_*_onboard -maxdepth 0 | head -n1)/build
	cp -r data            $$(find .pybuild/cpython3_*_onboard -maxdepth 0 | head -n1)/build
	cp -r icons           $$(find .pybuild/cpython3_*_onboard -maxdepth 0 | head -n1)/build
	cp -r themes          $$(find .pybuild/cpython3_*_onboard -maxdepth 0 | head -n1)/build
	cp -r layouts         $$(find .pybuild/cpython3_*_onboard -maxdepth 0 | head -n1)/build
	mkdir -p debian/home/.config/dconf/
	mkdir -p debian/home/.config/dconf_profile/
	mkdir -p debian/home/.local/share/onboard/
	cp -r layouts         debian/home/.local/share/onboard
	cp -r models          debian/home/.local/share/onboard
	mkdir -p debian/data/glib-2.0/schemas
	cp data/org.onboard.gschema.xml debian/data/glib-2.0/schemas/
	glib-compile-schemas debian/data/glib-2.0/schemas/
	export GSETTINGS_SCHEMA_DIR="$$(pwd)/debian/data/glib-2.0/schemas:/usr/share/glib-2.0/schemas" \
	  && export HOME=$$(pwd)/debian/home \
	  && export XDG_CONFIG_HOME=$$HOME/.config \
	  && export XDG_DATA_HOME=$$HOME/.local/share/ \
	  && xvfb-run -a \
	     dbus-test-runner --max-wait 3600 --keep-env --bus-type=session -t dh_auto_test
	rm $$(find .pybuild/cpython3_*_onboard -maxdepth 0 | head -n1)/build/onboard
	rm $$(find .pybuild/cpython3_*_onboard -maxdepth 0 | head -n1)/build/onboard-settings
	rm $$(find .pybuild/cpython3_*_onboard -maxdepth 0 | head -n1)/build/data/ -Rfv
	rm $$(find .pybuild/cpython3_*_onboard -maxdepth 0 | head -n1)/build/icons/ -Rfv
	rm $$(find .pybuild/cpython3_*_onboard -maxdepth 0 | head -n1)/build/themes/ -Rfv
	rm $$(find .pybuild/cpython3_*_onboard -maxdepth 0 | head -n1)/build/layouts/ -Rfv
endif

override_dh_install:
	rm -f debian/tmp/usr/share/onboard/COPYING
	rm -f debian/tmp/usr/share/onboard/COPYING.GPL3
	rm -f debian/tmp/usr/share/onboard/HACKING
	rm -f debian/tmp/usr/share/onboard/COPYING.BSD3

	# drop gnome-shell-extension-onboard files
	rm -Rf debian/tmp/usr/share/gnome-shell/

	dh_install

override_dh_missing:
	dh_missing --fail-missing

get-orig-source:
	uscan --noconf --force-download --rename --download-current-version --destdir=..
