#Packager: George Vlahavas <vlahavas~at~gmail~dot~com>
# Updated and modified for Slint by Didier Spaier didieratslintdotfr
pkgname=flatpak
export pkgver=1.16.1
pkgrel=3slint
source=(
	"https://github.com/flatpak/flatpak/releases/download/${pkgver}/flatpak-${pkgver}.tar.xz"
	"35-org.freedesktop.Flatpak.appstream.rules"
	"https://dl.flathub.org/repo/flathub.flatpakrepo"
	"flathub.desktop"
	"flathub.svg"
	"README.Slint"
	"make-flatpak-accessible"
	)
docs=("README.slint" "install" "copying" "changelog" "authors" "news" "todo" "about-nls")
url=https://flatpak.org/
options=("noautodotnew" "nosrcpack")

doinst() {
# Remove a possibly remining stale kernel setting
rm -f /etc/sysctl.d/max_user_namespaces.conf
# add flathub repo
if [ -x /usr/bin/flatpak ]; then
  /usr/bin/flatpak remote-add --if-not-exists flathub etc/flatpak/remotes.d/flathub.flatpakrepo
fi
# update the desktop database
if [ -x /usr/bin/update-desktop-database ]; then
  /usr/bin/update-desktop-database -q usr/share/applications >/dev/null 2>&1
fi
# and the icon cache
if [ -e usr/share/icons/hicolor/icon-theme.cache ]; then
  if [ -x /usr/bin/gtk-update-icon-cache ]; then
    /usr/bin/gtk-update-icon-cache usr/share/icons/hicolor >/dev/null 2>&1
  fi
fi
# create the group and user flatpak, dissallow setting/changing its password.
/usr/sbin/groupadd -g 96 flatpak 1>/dev/null 2>/dev/null
/usr/sbin/useradd -g flatpak -u 96 -d /var/empty -s /bin/false -c "flatpak User" flatpak  1>/dev/null 2>/dev/null
/usr/bin/passwd -l flatpak  1>/dev/null 2>/dev/null
# We want to put all docs under /usr/doc/pkname regardless of the version.
mv usr/doc/flatpak-$pkgver/* usr/doc/flatpak
rm -rf usr/doc/flatpak-$pkgver
}

slackdesc=\
(
#|-----handy-ruler------------------------------------------------------|
"flatpak (Linux application sandboxing and distribution framework)"
"Flatpak is a system for building, distributing, and running sandboxed"
"desktop applications on Linux."
)


build() {
	cd $startdir/src/flatpak-$pkgver
	meson setup \
		--prefix=/usr \
		--libdir=/usr/lib${LIBDIRSUFFIX} \
		--infodir=/usr/info \
		--localstatedir=/var \
		--mandir=/usr/man \
		--sysconfdir=/etc \
		--strip \
		-Dtests=false \
		-Ddocdir=/usr/doc/flatpak \
		-Dgtkdoc=disabled \
		-Dsystem_bubblewrap=/usr/bin/bwrap \
		build
	meson compile -C build -j $numjobs
	meson install -C build --destdir $startdir/pkg/

	#chmod 4755 $startdir/pkg/usr/bin/flatpak
	chmod 755 $startdir/pkg/etc/profile.d/flatpak.sh

	# match rules.d user/permissions with slackware polkit package
	chown polkitd $startdir/pkg/usr/share/polkit-1/rules.d
	chmod 700 $startdir/pkg/usr/share/polkit-1/rules.d
	# and rename the polkit rules so that they are read earlier
	# also make a change to ask for user password instead of root
	# password
	sed "s/AUTH_ADMIN/AUTH_SELF/" \
		$startdir/pkg/usr/share/polkit-1/rules.d/org.freedesktop.Flatpak.rules > \
		$startdir/pkg/usr/share/polkit-1/rules.d/35-org.freedesktop.Flatpak.rules
	# and add some additional rules
	cp $startdir/src/35-org.freedesktop.Flatpak.appstream.rules $startdir/pkg/usr/share/polkit-1/rules.d/
	# just so we know that this will be created by flatpak anyway
	mkdir -p $startdir/pkg/var/lib/flatpak
	# install default repo (processed in doinst.sh)
	mkdir -p $startdir/pkg/etc/flatpak/remotes.d
	cp $startdir/src/flathub.flatpakrepo $startdir/pkg/etc/flatpak/remotes.d/
	# desktop file and icon
	mkdir -p $startdir/pkg/usr/share/applications
	cp $startdir/src/flathub.desktop $startdir/pkg/usr/share/applications/
	mkdir -p $startdir/pkg/usr/share/icons/hicolor/scalable/apps
	cp $startdir/src/flathub.svg $startdir/pkg/usr/share/icons/hicolor/scalable/apps
	# install the script make-flatpak-accessible
	mkdir -p $PKG/usr/bin
	cp $SRC/make-flatpak-accessible $PKG/usr/bin
	chown root:root $PKG/usr/bin/make-flatpak-accessible
	chmod 755 $PKG/usr/bin/make-flatpak-accessible
} 
