#Packager: George Vlahavas <vlahavas~at~gmail~dot~com>

pkgname=flatpak
pkgver=1.18.4
pkgrel=1gv
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"
	)
docs=("readme.md" "install" "copying" "changelog" "authors" "news" "todo" "about-nls")
url=https://flatpak.org/
options=('noautodotnew')

doinst() {
# 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
}

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/$pkgname-$pkgver
	meson setup \
                --prefix=/usr \
                --libdir=/usr/lib${LIBDIRSUFFIX} \
                --infodir=/usr/info \
                --localstatedir=/var \
                --mandir=/usr/man \
                --sysconfdir=/etc \
                --strip \
		-Dsystem_bubblewrap=/usr/bin/bwrap \
		-Dsystem_helper_user=nobody \
		-Dgtkdoc=disabled \
		-Dtests=false \
		build || exit 1
	meson compile -C build -j $numjobs || exit 1
        meson install -C build --destdir $startdir/pkg/ || exit 1

	#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
} 
