#!/usr/bin/make -f

ifeq (armhf,$(DEB_HOST_ARCH))
  export CC=gcc-11
  export CXX=g++-11
endif

#export DH_VERBOSE=1
export DEB_BUILD_MAINT_OPTIONS=hardening=+all
export XDG_DATA_HOME=$(CURDIR)/debian/tmp/usr/share
export XDG_UTILS_INSTALL_MODE=user
DISABLE_MODULES=
ifneq (linux,$(DEB_HOST_ARCH_OS))
DISABLE_MODULES+= --disable-inotify
DISABLE_MODULES+= --disable-qt5wayland
DISABLE_MODULES+= --disable-gtk3wayland
endif
ifeq (hurd,$(DEB_HOST_ARCH_OS))
DISABLE_MODULES+= --disable-v4l
endif

ifeq (,$(filter $(DEB_HOST_ARCH),amd64 arm64 i386 mips64el mipsel))
DISABLE_MODULES+= --disable-qt5webview
endif

ifeq (armel,$(DEB_HOST_ARCH))
  export CC=clang
  export CXX=clang++
  export DEB_LDFLAGS_APPEND= -latomic

override_dh_dwz:
	# skip as this results in errors for clang-produced binaries
endif


%:
	dh $@

override_dh_auto_configure:
	GAMBAS_CONFIG_FAILURE=1 dh_auto_configure -- --disable-sqlite2 --disable-gtk --disable-qt4 --disable-keyring $(DISABLE_MODULES) --libdir=\$${prefix}/lib

override_dh_auto_clean:
	dh_auto_clean
	find . -name ".action" |xargs rm -Rfv
	find . -name ".gambas" |xargs rm -Rfv
	find . -name "*.gambas" |xargs rm -Rfv
	find . -name "DISABLED" |xargs rm -Rfv
	find . -name "DISABLED.gb*" |xargs rm -Rfv
	find . -name "warnings.log" |xargs rm -Rfv
	find . -name ".info" |xargs rm -Rfv
	find . -name ".list" |xargs rm -Rfv
	find . -name ".startup" |xargs rm -Rfv
	find . -name "*.mo" -delete
	find . -name "*.pot" -delete
	rm -fv aclocal.m4 main/aclocal.m4
	rm -rf main/lib/inotify/.libs
	find . -name config.sub -delete
	find . -name config.guess -delete
	rm -f gb.qt5/src/webview/Makefile main/lib/inotify/Makefile main/lib/inotify/gb.inotify.la main/lib/inotify/gb_inotify_la-c_watch.lo main/lib/inotify/gb_inotify_la-gb_list.lo main/lib/inotify/gb_inotify_la-main.lo

override_dh_auto_install:
	dh_auto_install --no-parallel
	xdg-icon-resource install --novendor --size 256 app/desktop/gambas3.png
	xdg-desktop-menu  install --novendor app/desktop/gambas3.desktop
	find $(CURDIR)/debian/tmp/usr -name "*.la" -delete
	find $(CURDIR)/debian/tmp/usr -type d -empty -delete
	find $(CURDIR)/debian/tmp/usr -name *.png -perm /ugo+x -exec chmod 644 \{} \;
	find $(CURDIR)/debian/tmp/ -name .gitignore -delete
