#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_LDFLAGS_MAINT_APPEND = -Wl,-O1 -Wl,-z,defs

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

DISTRO_ID = debian
FREE_REPOS = \'@DISTRO@-*-main\'
FREE_URL = https:\/\/www.debian.org\/social_contract\#guidelines
ifeq (yes,$(shell dpkg-vendor --derives-from Ubuntu && echo yes))
	DISTRO_ID = ubuntu
	FREE_REPOS = \'@DISTRO@-*-main\', \'@DISTRO@-*-universe\'
	FREE_URL = https:\/\/www.ubuntu.com\/about\/about-ubuntu\/licensing
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

%:
	dh $@

override_dh_auto_configure:
	dh_auto_configure -- \
		-Dauto_features=enabled \
		-Dapt=true \
		-Ddefault_featured_apps=true \
		-Dfwupd=$(if $(filter %-fwupd,$(shell dh_listpackages)),true,false) \
		-Dflatpak=$(if $(filter %-flatpak,$(shell dh_listpackages)),true,false) \
		-Dgudev=$(if $(filter linux,$(DEB_HOST_ARCH_OS)),true,false) \
		-Dmalcontent=$(if $(filter %-flatpak,$(shell dh_listpackages)),true,false) \
		-Dpackagekit=true \
		-Dpackagekit_autoremove=true \
		-Drpm_ostree=false \
		-Dsnap=$(if $(filter %-snap,$(shell dh_listpackages)),true,false) \
		-Dsysprof=$(if $(filter linux,$(DEB_HOST_ARCH_OS)),enabled,disabled)

execute_before_dh_installgsettings:
	sed "s#@FREE_REPOS@#$(FREE_REPOS)#g" debian/gnome-software.gsettings-override.in > debian/gnome-software.gsettings-override
	sed -i "s/@DISTRO@/$(DISTRO_ID)/g" debian/gnome-software.gsettings-override
	sed -i "s/@URL@/$(FREE_URL)/g" debian/gnome-software.gsettings-override

execute_after_dh_missing:
	# Not useful for Debian
	rm -f debian/gnome-software/usr/lib/*/gnome-software/plugins-*/libgs_plugin_fedora*
	# These files are split out to separate packages
	rm -f debian/gnome-software/usr/lib/*/gnome-software/plugins-*/libgs_plugin_appstream*
	rm -f debian/gnome-software/usr/lib/*/gnome-software/plugins-*/libgs_plugin_flatpak*
	rm -f debian/gnome-software/usr/lib/*/gnome-software/plugins-*/libgs_plugin_fwupd*
	rm -f debian/gnome-software/usr/lib/*/gnome-software/plugins-*/libgs_plugin_packagekit*
	rm -f debian/gnome-software/usr/lib/*/gnome-software/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.Snap.metainfo.xml
	rm -f debian/gnome-software/usr/share/applications/gnome-software-local-file-fwupd.desktop
# Ubuntu doesn't need the autostart file
ifeq ($(shell dpkg-vendor --query vendor),Ubuntu)
	rm -rf debian/gnome-software/etc/xdg/autostart
endif

override_dh_makeshlibs:
	dh_makeshlibs -Xlibgs_plugin

override_dh_shlibdeps:
	dh_shlibdeps -l/usr/lib/$(DEB_HOST_MULTIARCH)/gnome-software/

# DISABLED
override_dh_auto_test:

# Ubuntu doesn't need the fwupd plugin
override_dh_gencontrol:
ifeq ($(shell dpkg-vendor --query vendor),Ubuntu)
	dh_gencontrol -- -Vplugin:Recommends='gnome-software-plugin-deb'
else
	dh_gencontrol -- -Vplugin:Recommends='gnome-software-plugin-fwupd [linux-any], gnome-software-plugin-deb'
endif

execute_before_dh_clean:
	rm -f debian/gnome-software.gsettings-override
