#!/usr/bin/make -f

-include /usr/share/gnome-pkg-tools/1/rules/gnome-get-source.mk

GS_CONFIGURE_FLAGS = \
	-Denable-gnome-desktop=true \
	-Denable-odrs=true \
	-Denable-ostree=true \
	-Denable-packagekit=true \
	-Denable-rpm=false \
	-Denable-snap=true

# these are conditionally re-enabled later
GS_CONFIGURE_FLAGS += \
	-Denable-fwupd=false \
	-Denable-flatpak=false \
	-Denable-limba=false \
	-Denable-ubuntu-reviews=true

ifeq ($(DEB_HOST_ARCH_OS), linux)
	# Enable Flatpak and Limba support on Linux
	GS_CONFIGURE_FLAGS += -Denable-limba=true -Denable-flatpak=true

	# Enable fwupd support on supported architectures
	ifneq (,$(findstring $(DEB_HOST_ARCH), amd64 arm64 armhf i386))
		GS_CONFIGURE_FLAGS += -Denable-fwupd=true
	endif
endif

DISTRO_ID = debian
FREE_URL = https:\/\/www.debian.org\/social_contract\#guidelines
ifeq (yes,$(shell dpkg-vendor --derives-from Ubuntu && echo yes))
	DISTRO_ID = ubuntu
	FREE_URL = https:\/\/www.ubuntu.com\/about\/about-ubuntu\/licensing
	GS_CONFIGURE_FLAGS += -Denable-ubuntu-reviews=true
else ifeq (yes,$(shell dpkg-vendor --derives-from Tanglu && echo yes))
	DISTRO_ID = tanglu
else ifeq (yes,$(shell dpkg-vendor --derives-from PureOS && echo yes))
	DISTRO_ID = pureos
endif

# Install target dir
INSTALLDIR = $(CURDIR)/debian/tmp

# Fix the Flatpak plugin: https://launchpad.net/bugs/1650783
export DEB_LDFLAGS_MAINT_STRIP := -Wl,-Bsymbolic-functions

%:
	dh $@ --with gnome --buildsystem=meson

override_dh_auto_configure:
	dh_auto_configure -- --libexecdir=/usr/lib/$(DEB_HOST_MULTIARCH)/gnome-software/ \
			     $(GS_CONFIGURE_FLAGS)

override_dh_auto_install:
	dh_auto_install
ifneq (yes,$(shell dpkg-vendor --derives-from Ubuntu && echo yes))
	rm -f $(INSTALLDIR)/usr/lib/*/gs-plugins-*/libgs_plugin_ubuntu*
endif

override_dh_installgsettings:
	sed 's/@DISTRO@/$(DISTRO_ID)/g' debian/gnome-software.gsettings-override.in > debian/gnome-software.gsettings-override
	sed -i 's/@URL@/$(FREE_URL)/g' debian/gnome-software.gsettings-override
	dh_installgsettings

override_dh_install-arch:
	# Remove unused files
	rm -f debian/tmp/usr/lib/*/gs-plugins-*/*.a
	rm -f debian/tmp/usr/lib/*/gs-plugins-*/*.la
	rm -f debian/tmp/usr/lib/*/gs-plugins-*/libgs_plugin_fedora*
	dh_install

# workaround upstream issue in the new build system
# https://bugzilla.gnome.org/show_bug.cgi?id=785134
# https://bugs.launchpad.net/ubuntu/+source/gnome-software/+bug/1702522
override_dh_translations:
	ninja -C obj-$(DEB_HOST_GNU_TYPE) gnome-software-pot
	dh_translations

override_dh_missing:
	dh_missing --fail-missing

	# These files are split out to separate packages
	rm -f debian/gnome-software/usr/lib/*/gs-plugins-*/libgs_plugin_flatpak*
	rm -f debian/gnome-software/usr/lib/*/gs-plugins-*/libgs_plugin_limba*
	rm -f debian/gnome-software/usr/lib/*/gs-plugins-*/libgs_plugin_ostree*
	rm -f debian/gnome-software/usr/lib/*/gs-plugins-*/libgs_plugin_snap*
	rm -f debian/gnome-software/usr/share/metainfo/org.gnome.Software.Plugin.Flatpak.metainfo.xml
	rm -f debian/gnome-software/usr/share/metainfo/org.gnome.Software.Plugin.Limba.metainfo.xml
	rm -f debian/gnome-software/usr/share/metainfo/org.gnome.Software.Plugin.Snap.metainfo.xml

override_dh_install-indep:
	# Change the name and icon for Unity sessions
	# Also needs override_dh_install renamed to override_dh_install-arch
	dh_install
	sed -i 's/Name=Software/Name=Ubuntu Software/' debian/ubuntu-software/usr/share/ubuntu/applications/org.gnome.Software.desktop
	sed -i 's/Icon\(.*\)=\(org.gnome.Software\)/Icon\1=ubuntusoftware/' debian/ubuntu-software/usr/share/ubuntu/applications/org.gnome.Software.desktop

override_dh_makeshlibs:
	dh_makeshlibs -Xlibgs_plugin

override_dh_auto_test:
	# DISABLED

override_dh_clean:
	rm -f debian/gnome-software.gsettings-override
	dh_clean
