# Contributor: Bart Ribbers <bribbers@disroot.org>
# Maintainer: team/kde <bribbers@disroot.org>

# The group tag is just to easily find this APKBUILD by some scripts for automation
# group=kde-plasma
pkgname=discover
pkgver=6.1.5
pkgrel=0
pkgdesc="KDE Plasma resources management GUI"
url="https://userbase.kde.org/Discover"
# armhf blocked by qt5-qtdeclarative
# ppc64le, riscv64, loongarch64 and s390x blocked by purpose
arch="all !armhf !ppc64le !riscv64 !s390x !loongarch64"
license="LGPL-2.0-or-later AND (GPL-2.0-only OR GPL-3.0-only) AND GFDL-1.2-only"
depends="
	kirigami
	kirigami-addons
	kitemmodels
	kuserfeedback
	purpose
	"
makedepends="
	appstream-dev
	attica-dev
	extra-cmake-modules
	flatpak-dev
	karchive-dev
	kauth-dev
	kcmutils-dev
	kconfig-dev
	kcoreaddons-dev
	kcrash-dev
	kdbusaddons-dev
	kdeclarative-dev
	ki18n-dev
	kidletime-dev
	kio-dev
	kirigami-addons-dev
	kirigami-dev
	kitemmodels-dev
	knewstuff-dev
	kstatusnotifieritem-dev
	kuserfeedback-dev
	kxmlgui-dev
	libapk-qt-dev
	purpose-dev
	qcoro-dev
	qt6-qtbase-dev
	qt6-qtdeclarative-dev
	qt6-qtwebview-dev
	samurai
	"
checkdepends="xvfb-run"

# secfixes:
#   5.21.3-r0:
#     - CVE-2021-28117

case "$pkgver" in
*.90*) _rel=unstable ;;
*) _rel=stable ;;
esac
_repo_url="https://invent.kde.org/plasma/discover.git"
source="https://download.kde.org/$_rel/plasma/$pkgver/discover-$pkgver.tar.xz
	https://dev.alpinelinux.org/archive/discover/0001-apk-support.patch
	alpine-appstream-data.json
	alpine-linux-logo-icon.png
	"
subpackages="$pkgname-backend-flatpak:backend_flatpak $pkgname-backend-apk:backend_apk $pkgname-lang $pkgname-dbg"

case "$CARCH" in
x86|x86_64|armv7|aarch64)
	makedepends="$makedepends fwupd-dev"
	subpackages="$subpackages $pkgname-backend-fwupd:backend_fwupd"
	;;
*) ;;
esac

build() {
	# -g1: doesn't have any impact on the debug symbol size in this case
	export CFLAGS="$CFLAGS -g1"
	export CXXFLAGS="$CXXFLAGS -g1"
	cmake -B build -G Ninja \
		-DCMAKE_BUILD_TYPE=RelWithDebInfo \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DCMAKE_DISABLE_FIND_PACKAGE_Snapd=ON \
		-DWITH_KCM=OFF
	cmake --build build
}

check() {
	# knsbackendtest and flatpaktest fail to find their required executables
	# CategoriesTest requires non-present testdata
	xvfb-run -a ctest --test-dir build --output-on-failure -E "((knsbackend|flatpak)test)|CategoriesTest"
}

package() {
	DESTDIR="$pkgdir" cmake --install build
}

backend_flatpak() {
	pkgdesc="Flatpak backend for $pkgname"
	depends=""
	install_if="$pkgname=$pkgver-r$pkgrel flatpak"

	amove usr/lib/qt6/plugins/discover/flatpak-backend.so
	amove usr/share/libdiscover/categories/flatpak-backend-categories.xml
	amove usr/lib/qt6/plugins/discover-notifier/FlatpakNotifier.so
	amove usr/share/applications/org.kde.discover-flatpak.desktop
	amove usr/share/metainfo/org.kde.discover.flatpak.appdata.xml
	amove usr/share/icons/hicolor/scalable/apps/flatpak-discover.svg
}

backend_fwupd() {
	pkgdesc="fwupd backend for $pkgname"
	depends=""
	install_if="$pkgname=$pkgver-r$pkgrel fwupd"

	amove usr/lib/qt6/plugins/discover/fwupd-backend.so
}

backend_apk() {
	pkgdesc="Apk backend for $pkgname"
	depends=""
	install_if="$pkgname=$pkgver-r$pkgrel apk-tools"

	amove usr/lib/qt6/plugins/discover/alpineapk-backend.so
	amove usr/share/polkit-1/actions/org.kde.discover.alpineapkbackend.policy
	amove usr/share/dbus-1/system.d/org.kde.discover.alpineapkbackend.conf
	amove usr/share/dbus-1/system-services/org.kde.discover.alpineapkbackend.service

	# JSON file contains placeholder for replacement - @CARCH@
	sed -i "s/@CARCH@/$CARCH/g" "$srcdir"/alpine-appstream-data.json
	install -Dm644 "$srcdir"/alpine-appstream-data.json \
		"$subpkgdir"/usr/share/libdiscover/external-appstream-urls/alpine-appstream-data.json

	install -Dm644 -t "$subpkgdir"/usr/share/icons/hicolor/32x32/apps/ \
		"$srcdir"/alpine-linux-logo-icon.png
}

sha512sums="
ea604afb73003b3d0e67bab78c6165f7a347499baf778928deaa03a48f0e9d06f6d4e753b3aa823bbd4e6cf39932d7a8432924d03da3bc3eea98052d7e433a4e  discover-6.1.5.tar.xz
84d16faa404f03726c176d22ee1ad81db3660beb44eb66dc8579566a945198255920400a74fa6207080dc007f64e7a57f2114f9d864252336a7cf7ae023003be  0001-apk-support.patch
d9cc12a5a92df820d6d133a3698a4fbb08a97f08f00554a11d87c73c6144b42d07cecd6682bf9576d83c8e2556e35ade2fb39e77895afbbe33e2f94630441bb7  alpine-appstream-data.json
0766668630cb14f58c840ebdda8b76bd1dec98b4dd61469677d4ad6eb4966809a25dbec2a846502cad0391df5b6ca9d692bb857a6fa203b61a52e6ac044cfbf4  alpine-linux-logo-icon.png
"
