# Maintainer:  Bartłomiej Piotrowski <bpiotrowski@archlinux.org>
# Contributor: Jan Alexander Steffens (heftig) <jan.steffens@gmail.com>

pkgname=flatpak
pkgver=1.4.2
pkgrel=1
pkgdesc="Linux application sandboxing and distribution framework (formerly xdg-app)"
url="https://flatpak.org"
arch=(x86_64)
license=(LGPL2.1)
depends=(dbus systemd glib2 libsoup polkit libxau ostree json-glib libseccomp libarchive
         python bubblewrap appstream-glib xdg-dbus-proxy)
makedepends=(intltool gobject-introspection gtk-doc git docbook-xsl xmlto)
checkdepends=(valgrind)
_commit=f70978b4ad892cca6837153a3a1f7bc33ac347e8  # tags/1.4.2^0
source=("git+https://github.com/flatpak/flatpak#commit=$_commit"
        "git+https://gitlab.gnome.org/GNOME/libglnx.git"
        "git+https://github.com/projectatomic/bubblewrap"
        "git+https://github.com/flatpak/xdg-dbus-proxy"
        https://dl.flathub.org/repo/flathub.flatpakrepo
        python3.diff
        flatpak-bindir.sh)
sha256sums=('SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            '3371dd250e61d9e1633630073fefda153cd4426f72f4afa0c3373ae2e8fea03a'
            '4b2e70d10402e179a1fdcfc367a8be128e2ceaafb8b16a737e14c16596417cc5'
            'cf20e4831bceaaee74e9298b4ad7c4d2d51085d43f62d1c6a2d2977d2f421e00')

prepare() {
  cd $pkgname

  patch -Np1 -i ../python3.diff

  git submodule init
  git config --local submodule.bubblewrap.url "$srcdir/bubblewrap"
  git config --local submodule.libglnx.url "$srcdir/libglnx"
  git config --local submodule.dbus-proxy.url "$srcdir/xdg-dbus-proxy"
  git submodule update

  # https://github.com/flatpak/flatpak/issues/267
  sed -i '/locale\/C\./d' tests/make-test-runtime.sh

  NOCONFIGURE=1 ./autogen.sh
}

pkgver() {
  cd $pkgname
  git describe --tags | sed 's/-/+/g'
}

build() {
  cd $pkgname

  ./configure \
    --prefix=/usr \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --sbindir=/usr/bin \
    --libexecdir=/usr/lib \
    --disable-static \
    --enable-gtk-doc \
    --with-system-bubblewrap \
    --with-system-dbus-proxy \
    --with-dbus-config-dir=/usr/share/dbus-1/system.d

  sed -i -e 's/ -shared / -Wl,-O1,--as-needed\0/g' libtool

  make
}

check() {
  cd $pkgname
  # Xattr tests fail on tmpfs
  make -k check
}

package() {
  depends+=(xdg-desktop-portal)

  cd $pkgname
  make DESTDIR="$pkgdir" install
  rm -rf "$pkgdir/usr/share/selinux"

  install -Dm644 "$srcdir/flatpak-bindir.sh" "$pkgdir/etc/profile.d/flatpak-bindir.sh"
  install -Dm644 "$srcdir/flathub.flatpakrepo" "$pkgdir/etc/flatpak/remotes.d/flathub.flatpakrepo"

  echo 'u flatpak - "Flatpak system helper"' |
    install -Dm644 /dev/stdin "$pkgdir/usr/lib/sysusers.d/flatpak.conf"

  # Fixup mode to match polkit
  install -d -o root -g 102 -m 750 "$pkgdir/usr/share/polkit-1/rules.d"
}
