diff --git a/SOURCES/fix-build.patch b/SOURCES/fix-build.patch
new file mode 100644
index 0000000..7c8bd8e
--- /dev/null
+++ b/SOURCES/fix-build.patch
@@ -0,0 +1,46 @@
+libglnx: throw in custom basename implementation
+
+libglnx tries to avoid using a specific implementation of basename by
+doing #undef basename.  This trick doesn't work with glibc anymore
+apparently.
+
+But basename is like 3 lines of code to write, so just write it.
+
+diff --git a/libglnx/glnx-fdio.h b/libglnx/glnx-fdio.h
+--- a/libglnx/glnx-fdio.h	2017-11-08 13:41:49.734931484 -0500
++++ b/libglnx/glnx-fdio.h	2017-11-08 13:45:33.330758205 -0500
+@@ -43,7 +43,14 @@ G_BEGIN_DECLS
+ static inline
+ const char *glnx_basename (const char *path)
+ {
+-  return (basename) (path);
++  gchar *base;
++
++  base = strrchr (path, G_DIR_SEPARATOR);
++
++  if (base)
++    return base + 1;
++
++  return path;
+ }
+ 
+ gboolean
+diff --git a/libostree-2017.2/libglnx/glnx-fdio.h b/libostree-2017.2/libglnx/glnx-fdio.h
+--- a/libostree-2017.2/libglnx/glnx-fdio.h	2017-11-08 13:53:45.655413948 -0500
++++ b/libostree-2017.2/libglnx/glnx-fdio.h	2017-11-08 13:53:57.543285889 -0500
+@@ -43,7 +43,14 @@ G_BEGIN_DECLS
+ static inline
+ const char *glnx_basename (const char *path)
+ {
+-  return (basename) (path);
++  gchar *base;
++
++  base = strrchr (path, G_DIR_SEPARATOR);
++
++  if (base)
++    return base + 1;
++
++  return path;
+ }
+ 
+ gboolean
diff --git a/SOURCES/no-user-systemd.patch b/SOURCES/no-user-systemd.patch
new file mode 100644
index 0000000..5ba1864
--- /dev/null
+++ b/SOURCES/no-user-systemd.patch
@@ -0,0 +1,32 @@
+Drop the SystemdService line in dbus service files
+
+I don't think we can dbus activate systemd services on
+the session bus (we don't have a user bus, and I'm
+pretty sure we don't have systemd --user)
+
+This commit just drops the lines, so dbus can activate on
+its own.
+diff --git a/permission-store/org.freedesktop.impl.portal.PermissionStore.service.in b/permission-store/org.freedesktop.impl.portal.PermissionStore.service.in
+--- a/permission-store/org.freedesktop.impl.portal.PermissionStore.service.in	2017-11-08 13:26:55.759617833 -0500
++++ b/permission-store/org.freedesktop.impl.portal.PermissionStore.service.in	2017-11-08 13:27:00.382567509 -0500
+@@ -1,4 +1,3 @@
+ [D-BUS Service]
+ Name=org.freedesktop.impl.portal.PermissionStore
+ Exec=@libexecdir@/xdg-permission-store
+-SystemdService=xdg-permission-store.service
+diff -up flatpak-0.8.8/document-portal/org.freedesktop.portal.Documents.service.in.no-user-systemd flatpak-0.8.8/document-portal/org.freedesktop.portal.Documents.service.in
+--- flatpak-0.8.8/document-portal/org.freedesktop.portal.Documents.service.in.no-user-systemd	2017-11-08 16:33:31.652110888 -0500
++++ flatpak-0.8.8/document-portal/org.freedesktop.portal.Documents.service.in	2017-11-08 16:33:45.688957745 -0500
+@@ -1,4 +1,3 @@
+ [D-BUS Service]
+ Name=org.freedesktop.portal.Documents
+ Exec=@libexecdir@/xdg-document-portal
+-SystemdService=xdg-document-portal.service
+diff -up flatpak-0.8.8/session-helper/org.freedesktop.Flatpak.service.in.no-user-systemd flatpak-0.8.8/session-helper/org.freedesktop.Flatpak.service.in
+--- flatpak-0.8.8/session-helper/org.freedesktop.Flatpak.service.in.no-user-systemd	2017-11-08 16:33:21.045226761 -0500
++++ flatpak-0.8.8/session-helper/org.freedesktop.Flatpak.service.in	2017-11-08 16:33:38.361037649 -0500
+@@ -1,4 +1,3 @@
+ [D-BUS Service]
+ Name=org.freedesktop.Flatpak
+ Exec=@libexecdir@/flatpak-session-helper
+-SystemdService=flatpak-session-helper.service
diff --git a/SOURCES/ostree-bundle.patch b/SOURCES/ostree-bundle.patch
new file mode 100644
index 0000000..4a78c0d
--- /dev/null
+++ b/SOURCES/ostree-bundle.patch
@@ -0,0 +1,37 @@
+Make sure rpath gets added for ostree libs
+
+We bundle ostree in the package, so we need to make sure an rpath
+gets set pointing to our bundled library.
+
+The Makefiles are missing the OSTREE_LIBS (and so the -Wl,-rpath),
+so add them in.
+diff --git a/document-portal/Makefile.am.inc b/document-portal/Makefile.am.inc
+--- a/document-portal/Makefile.am.inc	2017-11-08 16:52:40.135530435 -0500
++++ b/document-portal/Makefile.am.inc	2017-11-08 16:52:53.340379677 -0500
+@@ -34,5 +34,5 @@ xdg_document_portal_SOURCES = \
+ 	document-portal/xdp-fuse.c		\
+ 	$(NULL)
+ 
+-xdg_document_portal_LDADD = $(BASE_LIBS) $(FUSE_LIBS) libflatpak-common.la
++xdg_document_portal_LDADD = $(BASE_LIBS) $(FUSE_LIBS) $(OSTREE_LIBS) libflatpak-common.la
+ xdg_document_portal_CFLAGS = $(BASE_CFLAGS) $(OSTREE_CFLAGS) $(SOUP_CFLAGS) $(JSON_CFLAGS) $(FUSE_CFLAGS) -I$(srcdir)/document-portal -I$(builddir)/document-portal -I$(srcdir)/permission-store -I$(builddir)/permission-store -DFLATPAK_COMPILATION
+diff --git a/permission-store/Makefile.am.inc b/permission-store/Makefile.am.inc
+--- a/permission-store/Makefile.am.inc	2017-11-09 14:34:13.681220097 -0500
++++ b/permission-store/Makefile.am.inc	2017-11-09 14:34:20.578146811 -0500
+@@ -30,5 +30,5 @@ xdg_permission_store_SOURCES = \
+ 	permission-store/xdg-permission-store.h	\
+ 	$(NULL)
+ 
+-xdg_permission_store_LDADD = $(BASE_LIBS) libflatpak-common.la
++xdg_permission_store_LDADD = $(BASE_LIBS) $(OSTREE_LIBS) libflatpak-common.la
+ xdg_permission_store_CFLAGS = $(BASE_CFLAGS) $(SOUP_CFLAGS) $(JSON_CFLAGS) $(OSTREE_CFLAGS) $(GSYSTEM_CFLAGS) -I$(srcdir)/permission-store -I$(builddir)/permission-store
+diff --git a/session-helper/Makefile.am.inc b/session-helper/Makefile.am.inc
+--- a/session-helper/Makefile.am.inc	2017-11-09 14:34:47.849857022 -0500
++++ b/session-helper/Makefile.am.inc	2017-11-09 14:35:01.377713286 -0500
+@@ -12,5 +12,5 @@ flatpak_session_helper_SOURCES = \
+ 	session-helper/flatpak-session-helper.c	\
+ 	$(NULL)
+ 
+-flatpak_session_helper_LDADD = $(BASE_LIBS) libflatpak-common.la
++flatpak_session_helper_LDADD = $(BASE_LIBS) $(OSTREE_LIBS) libflatpak-common.la
+ flatpak_session_helper_CFLAGS = $(BASE_CFLAGS) $(SOUP_CFLAGS) $(OSTREE_CFLAGS) $(GSYSTEM_CFLAGS) $(JSON_CFLAGS)
diff --git a/SPECS/flatpak.spec b/SPECS/flatpak.spec
index 6cb50cc..692c0a5 100644
--- a/SPECS/flatpak.spec
+++ b/SPECS/flatpak.spec
@@ -2,7 +2,7 @@
 
 Name:           flatpak
 Version:        0.8.7
-Release:        1%{?dist}
+Release:        3%{?dist}
 Summary:        Application deployment framework for desktop apps
 
 Group:          Development/Tools
@@ -11,6 +11,9 @@ URL:            http://flatpak.org/
 Source0:        https://github.com/flatpak/flatpak/releases/download/%{version}/%{name}-%{version}.tar.xz
 Source1:        https://github.com/ostreedev/ostree/releases/download/v%{ostree_version}/libostree-%{ostree_version}.tar.xz
 Patch0:         ostree-soup-Hold-a-ref-to-the-pending-URI-during-completion.patch
+Patch1:         no-user-systemd.patch
+Patch2:         fix-build.patch
+Patch3:         ostree-bundle.patch
 BuildRequires:  pkgconfig(fuse)
 BuildRequires:  pkgconfig(gio-unix-2.0)
 BuildRequires:  pkgconfig(gobject-introspection-1.0) >= 1.40.0
@@ -23,6 +26,7 @@ BuildRequires:  pkgconfig(libseccomp)
 BuildRequires:  pkgconfig(liblzma)
 BuildRequires:  pkgconfig(xau)
 BuildRequires:  pkgconfig(e2p)
+BuildRequires:  automake, autoconf, libtool, gettext-devel, gtk-doc
 BuildRequires:  bison
 BuildRequires:  docbook-dtds
 BuildRequires:  docbook-style-xsl
@@ -38,6 +42,21 @@ BuildRequires:  /usr/bin/xsltproc
 # Needed for the document portal.
 Requires:       /usr/bin/fusermount
 
+# libostree bundling
+# https://fedoraproject.org/wiki/EPEL:Packaging_Autoprovides_and_Requires_Filtering
+# We're using RPATH to pick up our bundled version
+%filter_from_requires /libostree-1/d
+
+# And ensure we don't add a Provides
+%{?filter_setup:
+%filter_provides_in %{_libdir}/%{name}/.*
+%filter_setup
+}
+# And for now we manually inject this dep; surprisingly the
+# command line doesn't currently link to the public libflatpak
+# library.
+Requires:       %{name}-libs = %{version}-%{release}
+
 %description
 flatpak is a system for building, distributing and running sandboxed desktop
 applications on Linux. See https://wiki.gnome.org/Projects/SandboxedApps for
@@ -94,6 +113,11 @@ This package contains libflatpak.
 %setup -q -a 1
 cd libostree-%{ostree_version}
 %patch0 -p1
+cd ..
+%patch1 -p1
+%patch2 -p1
+%patch3 -p1
+
 
 %build
 cd libostree-%{ostree_version}
@@ -113,9 +137,8 @@ cd ..
 mkdir -p root/lib/pkgconfig
 ROOT=`pwd`/root
 
-cp libostree-%{ostree_version}/.libs/libostree-1.so.1.0.0 root/lib/libostree-flatpak-1.so.1.0.0
-ln -s libostree-flatpak-1.so.1.0.0 root/lib/libostree-flatpak-1.so.1
-ln -s libostree-flatpak-1.so.1.0.0 root/lib/libostree-flatpak-1.so
+mv libostree-%{ostree_version}/.libs/libostree-1.so* root/lib
+ls -al root/lib/libostree*
 ln -s `pwd`/libostree-%{ostree_version}/src/libostree root/include
 
 cat > root/lib/pkgconfig/ostree-1.pc <<EOF
@@ -123,10 +146,12 @@ Name: OSTree
 Description: Git for operating system binaries
 Version: %{ostree_version}
 Requires: gio-unix-2.0
-Libs: -L$ROOT/lib -lostree-flatpak-1
+Libs: -L$ROOT/lib -Wl,-rpath=%{_libdir}/flatpak -lostree-1
 Cflags: -I$ROOT/include
 EOF
-
+rm -f configure
+gtkdocize
+autoreconf -f -i -s
 export PKG_CONFIG_PATH=$ROOT/lib/pkgconfig
 (if ! test -x configure; then NOCONFIGURE=1 ./autogen.sh; CONFIGFLAGS=--enable-gtk-doc; fi;
  # User namespace support is sufficient.
@@ -135,13 +160,16 @@ export PKG_CONFIG_PATH=$ROOT/lib/pkgconfig
  %configure \
             --with-dwarf-header=%{_includedir}/libdwarf --with-priv-mode=none \
             --enable-docbook-docs \
+            --docdir=%{_pkgdocdir} \
             --disable-introspection $CONFIGFLAGS)
 %make_build V=1
 sed -i s/ostree-1// %{name}.pc
 
 %install
+mkdir -p %{buildroot}%{_datadir}/gtk-doc/html/flatpak
 %make_install
-install root/lib/libostree-flatpak-1.so.1.0.0 %{buildroot}%{_libdir}
+install -d %{buildroot}%{_libdir}/flatpak
+mv root/lib/libostree-1.so* %{buildroot}%{_libdir}/flatpak
 install -pm 644 NEWS README.md %{buildroot}/%{_pkgdocdir}
 # The system repo is not installed by the flatpak build system.
 install -d %{buildroot}%{_localstatedir}/lib/flatpak
@@ -184,7 +212,6 @@ flatpak remote-list --system &> /dev/null || :
 %{_libexecdir}/xdg-document-portal
 %{_libexecdir}/xdg-permission-store
 %attr(04755,root,root) %{_libexecdir}/flatpak-bwrap
-%{_libdir}/libostree-flatpak-1.so.1.0.0
 
 %dir %{_localstatedir}/lib/flatpak
 %{_mandir}/man1/%{name}*.1*
@@ -214,10 +241,28 @@ flatpak remote-list --system &> /dev/null || :
 
 %files libs
 %license COPYING
+%{_libdir}/flatpak/libostree-1.so*
 %{_libdir}/libflatpak.so.*
 
 
 %changelog
+* Fri Nov 10 2017 David King <dking@redhat.com> - 0.8.7-3
+- Fix crasher in xdg-desktop-portal
+- Tweak spec file so it still builds even though we need to
+  autoreconf.
+
+* Tue Aug 01 2017 Colin Walters <walters@verbum.org> - 0.8.7-2
+- Fix libostree bundling:
+  Ensure we do not Provide or Require libostree.
+  Move the shared library into flatpak-libs so flatpak always
+  depends on it.
+  Keep the shared library filename as libostree, but put it
+  under a private directory.  Renaming the file on disk did not
+  really do much since the dynamic linker and RPM work from the
+  soname.
+  Resolves: #1476905
+- Tweak build to work both with and without BZ#1392354
+
 * Tue Jun 20 2017 Kalev Lember <klember@redhat.com> - 0.8.7-1
 - Update to 0.8.7
 - Resolves: #1391018