#!/usr/bin/make -f

#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=
@{if-not gb-clipper2?}@DISABLE_MODULES+= --disable-clipper2
@{endif}@
@{if-not gb-qt6?}@DISABLE_MODULES+= --disable-qt6
@{endif}@
@{if-not gb-mongodb?}@DISABLE_MODULES+= --disable-mongodb
@{endif}@
@{if-not gb-db-sqlite2?}@DISABLE_MODULES+= --disable-sqlite2
@{endif}@
@{if-not gb-gtk-opengl?}@DISABLE_MODULES+= --disable-gtkopengl
@{endif}@
@{if-not gb-qt4?}@DISABLE_MODULES+= --disable-qt4
@{endif}@
@{if-not gb-qt5-webkit?}@DISABLE_MODULES+= --disable-qt5webkit
@{endif}@
@{if-not gb-desktop-gnome-keyring?}@DISABLE_MODULES+= --disable-keyring
@{endif}@
@{if-not gb-sdl?}@DISABLE_MODULES+= --disable-sdl
@{endif}@
@{if-not gb-sdl-sound?}@DISABLE_MODULES+= --disable-sdlsound
@{endif}@
ifneq (linux,$(DEB_HOST_ARCH_OS))
  DISABLE_MODULES+= --disable-inotify
  DISABLE_MODULES+= --disable-qt5wayland
  DISABLE_MODULES+= --disable-qt6wayland
  DISABLE_MODULES+= --disable-gtk3wayland
  DISABLE_MODULES+= --disable-v4l
endif

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

ifeq (armhf,$(DEB_HOST_ARCH))
@{if version like {debian-11,ubuntu-20.04}}@
  export CC=gcc-10
  export CXX=g++-10
@{else}@
  export CC=gcc-11
  export CXX=g++-11
@{endif}@
endif

%:
	dh $@

override_dh_auto_configure:
	GAMBAS_CONFIG_FAILURE=1 dh_auto_configure -- $(DISABLE_MODULES) --libdir=\$${prefix}/lib
	
override_dh_strip:
	dh_strip --no-automatic-dbgsym

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

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

