# Conditional build:
 %bcond_with    static  # don't use shared libraries
 %bcond_without imode   # don't build interactive mode
-%bcond_without python  # don't build python bindings
+%bcond_with    python  # don't build python bindings
 %bcond_with    tests   # tests
 
 # required versions (forced to avoid SEGV with mixed db used by rpm and poldek)
 Patch6:                rpm-4.15.patch
 Patch7:                db-index-format.patch
 Patch8:                rpm4-uname-deps.patch
+Patch9:                sqlite-rpmdb.patch
+Patch10:       rpm4-cpuinfo-deps.patch
+Patch11:       rpm4-no-dir-deps.patch
+Patch12:       rpm4-rpmvercmp.patch
+Patch13:       trurlib-shared.patch
 URL:           http://poldek.pld-linux.org/
 BuildRequires: %{db_pkg}-devel >= %{ver_db}
 BuildRequires: autoconf >= 2.63
 %patch5 -p1
 %patch6 -p1
 %patch7 -p1
+%patch8 -p1
+%patch9 -p1
+%patch10 -p1
+%patch11 -p1
+%patch12 -p1
+cd trurlib
+%patch13 -p1
+cd ..
 
 %{__rm} doc/poldek.info
 %{__rm} m4/libtool.m4 m4/lt*.m4
 
--- /dev/null
+--- poldek-0.42.2/pm/rpmorg/misc.c~    2020-11-01 10:27:32.000000000 +0100
++++ poldek-0.42.2/pm/rpmorg/misc.c     2020-11-15 10:30:02.296719845 +0100
+@@ -79,6 +79,9 @@
+ #ifdef HAVE_RPMDSUNAME
+         (rpmcap_fn)rpmdsUname,
+ #endif
++#ifdef HAVE_RPMDSCPUINFO
++        (rpmcap_fn)rpmdsCpuinfo,
++#endif
+         NULL,
+     };
+ 
 
--- /dev/null
+--- poldek-0.42.2/pm/rpmorg/Makefile.am~       2020-01-25 23:03:28.000000000 +0100
++++ poldek-0.42.2/pm/rpmorg/Makefile.am        2020-11-15 10:28:44.040052400 +0100
+@@ -8,7 +8,6 @@
+ libpm_rpm_la_SOURCES =                        \
+       mod.c                           \
+       rpmdb_it.c                      \
+-      rpmdb_depdirs.c                 \
+       install.c                       \
+       signature.c                     \
+       misc.c                          \
+--- poldek-0.42.2/pm/rpmorg/mod.c~     2020-01-25 22:59:59.000000000 +0100
++++ poldek-0.42.2/pm/rpmorg/mod.c      2020-11-15 10:28:21.190049426 +0100
+@@ -26,7 +26,7 @@
+     pm_rpm_satisfies,
+     pm_rpm_dbpath, 
+     pm_rpm_dbmtime,
+-    pm_rpm_dbdepdirs,
++    NULL,                       /* pm_rpm_dbdepdirs */
+     
+ (void *(*)(void *, void *, const char *, const char *, mode_t, tn_hash *))pm_rpm_opendb,
+     (void (*)(void *))pm_rpm_closedb,
 
--- /dev/null
+From 70940292e321f1b7f4af217425810ed4110f6333 Mon Sep 17 00:00:00 2001
+From: Jan Palus <atler@pld-linux.org>
+Date: Sat, 31 Oct 2020 00:32:04 +0100
+Subject: [PATCH] implement rpmvercmp for rpm.org
+
+---
+ pm/rpmorg/rpmvercmp.c | 24 ++++++++++++++++--------
+ 1 file changed, 16 insertions(+), 8 deletions(-)
+
+diff --git a/pm/rpmorg/rpmvercmp.c b/pm/rpmorg/rpmvercmp.c
+index 7e40e15..36bf9e5 100644
+--- a/pm/rpmorg/rpmvercmp.c
++++ b/pm/rpmorg/rpmvercmp.c
+@@ -49,18 +49,15 @@ static void parse(const char *evrstr, EVR_t evr)
+ #endif
+ #endif
+ 
+-#ifdef HAVE_RPMORG
+-int main(void)
+-{
+-    printf("not implemented");
+-    exit(EXIT_SUCCESS);
+-}
+-#else
+ int main(int argc, char *argv[])
+ {
+     int cmprc;
+     const char *v1, *v2;
++#ifdef HAVE_RPMORG
++    rpmver evr1, evr2;
++#else
+     EVR_t evr1, evr2;
++#endif
+ 
+     if (argc < 3) {
+         printf("Usage: rpmvercmp VERSION1 VERSION2\n");
+@@ -81,6 +78,12 @@ int main(int argc, char *argv[])
+       exit(2);
+     }
+ 
++#ifdef HAVE_RPMORG
++    evr1 = rpmverParse(v1);
++    evr2 = rpmverParse(v2);
++
++    cmprc = rpmverCmp(evr1, evr2);
++#else
+     evr1 = malloc(sizeof(struct EVR_s));
+     evr2 = malloc(sizeof(struct EVR_s));
+ 
+@@ -93,17 +96,22 @@ int main(int argc, char *argv[])
+ #endif
+ 
+     cmprc = rpmEVRcompare(evr1, evr2);
++#endif
+ 
+     printf("%s %s %s\n", v1, cmprc == 0 ?  "==" : cmprc > 0 ? ">" : "<", v2);
+ 
+     if (cmprc < 0)
+         cmprc = 2;
+ 
++#ifdef HAVE_RPMORG
++    rpmverFree(evr1);
++    rpmverFree(evr2);
++#else
+     free((char *)evr1->str);
+     free((char *)evr2->str);
+     free(evr1);
+     free(evr2);
++#endif
+ 
+     exit(cmprc);
+ }
+-#endif
 
--- /dev/null
+--- poldek-0.42.2/pm/rpmorg/rpm.c.orig 2020-01-25 22:59:59.000000000 +0100
++++ poldek-0.42.2/pm/rpmorg/rpm.c      2020-11-01 10:11:24.892206997 +0100
+@@ -286,6 +286,10 @@
+ 
+     pm_rpm = pm_rpm;
+ 
++    char *backend = rpmExpand("%{_db_backend}", NULL);
++    if (backend != NULL && strncmp(backend, "sqlite", 6) == 0) {
++        file = "rpmdb.sqlite";
++    }
+     snprintf(path, sizeof(path), "%s/%s", dbpath, file);
+ 
+     if (stat(path, &st) != 0)
 
--- /dev/null
+From 67817281c4346832356febca89373742a0cde8b1 Mon Sep 17 00:00:00 2001
+From: Jan Palus <atler@pld-linux.org>
+Date: Sat, 31 Oct 2020 00:04:11 +0100
+Subject: [PATCH] create symbolic link for dynamic lib as well
+
+both tndb and vfile in poldek link against libtrurl and due to the way
+linking is configured it is always linked with static libtrurl (only
+libtrurl.a link provided). this causes two issues:
+
+- mixes dynamically linked libraries with statically linked libtrurl
+  and since nothing enforces -fPIC in the latter it results in error:
+
+libtool: warning: relinking 'libtndb.la'
+libtool: install: ... -o .libs/libtndb.so.0.2.0
+/usr/bin/ld: ./../trurlib/libtrurl.a(ndie.o): relocation R_AARCH64_ADR_PREL_PG_HI21 against symbol `__stack_chk_guard@@GLIBC_2.17' which may bind externally can not be used when making a shared object; recompile with -fPIC
+/usr/bin/ld: ./../trurlib/libtrurl.a(ndie.o)(.text+0x8): unresolvable R_AARCH64_ADR_PREL_PG_HI21 relocation against symbol `__stack_chk_guard@@GLIBC_2.17'
+/usr/bin/ld: final link failed: bad value
+
+- includes copy of libtrurl in dynamic libraries even though poldek
+  provides dynamic libtrurl
+---
+ Makefile.am | 1 +
+ 1 file changed, 1 insertion(+)
+
+diff --git a/Makefile.am b/Makefile.am
+index 7c41374..0d13380 100644
+--- a/Makefile.am
++++ b/Makefile.am
+@@ -146,5 +146,6 @@ tests:  $(EXTRA_PROGRAMS)
+ 
+ all-local:
+       @ln -sf .libs/libtrurl.a libtrurl.a
++      @ln -sf .libs/libtrurl.so libtrurl.so
+ 
+ include Makefile.extra