%define major 0

# The naming scheme in non-Mandriva-based distros
%if 0%{!?mklibname:1}
%define mklibname(ds)  lib%{1}%{?2:%{2}}%{-s:-static}%{-d:-devel}
%endif
%if 0%{!?mkrel:1}
%define mkrel(c) %{1}%{?dist}
%endif

%define libname %mklibname repo %major
%define libname_devel %mklibname repo -d

%global libcurl_version 7.52.0

%if 0%{?rhel}
%bcond_with zchunk
%else
%bcond_without zchunk
%endif

%if 0%{?fedora} >= 39
%bcond_with use_gpgme
%bcond_with use_selinux
%elif 0%{?mageia}
%bcond_without use_gpgme
%bcond_with use_selinux
%else
%bcond_without use_gpgme
%bcond_without use_selinux
%endif

# Needs to match how gnupg2 is compiled
%bcond_with run_gnupg_user_socket

%if %{with use_gpgme} && %{with use_selinux}
%bcond_without selinux
%else
%bcond_with selinux
%endif

Name:           librepo
Version:        1.19.0
Release:        %mkrel 1
Summary:        Repodata downloading library

Group:          System/Libraries
License:        LGPL-2.1-or-later
URL:            https://github.com/rpm-software-management/librepo
Source0:        %{url}/archive/%{version}/%{name}-%{version}.tar.gz

BuildRequires:  cmake
BuildRequires:  gcc
BuildRequires:  check-devel
BuildRequires:  doxygen
BuildRequires:  pkgconfig(glib-2.0) >= 2.66
%if %{with use_gpgme}
BuildRequires:  gpgme-devel
%else
BuildRequires:  pkgconfig(rpm) >= 4.18.0
%endif
BuildRequires:  libattr-devel
BuildRequires:  libcurl-devel >= %{libcurl_version}
BuildRequires:  pkgconfig(libxml-2.0)
BuildRequires:  pkgconfig(libcrypto)
%if %{with selinux}
BuildRequires:  pkgconfig(libselinux)
%endif
BuildRequires:  pkgconfig(openssl)
%if %{with zchunk}
BuildRequires:  pkgconfig(zck) >= 0.9.11
%endif

%description
A library providing C and Python (libcURL like) API to downloading repository
metadata.

%package -n %{libname}
Summary:        Libraries for %{name}
Group:          System/Libraries
Provides:       %{name} = %{version}-%{release}
Provides:       %{name}%{?_isa} = %{version}-%{release}

%description -n %{libname}
Libraries for %{name}.

%package -n %{libname_devel}
Summary:        Repodata downloading library
Group:          Development/C
Provides:       %{name}-devel = %{version}-%{release}
Requires:       %{name}%{?_isa} = %{version}-%{release}

%description -n %{libname_devel}
Development files for %{name}.

%package -n python3-%{name}
Summary:        Python 3 bindings for the librepo library
Group:          Development/Python
%{?python_provide:%python_provide python3-%{name}}
BuildRequires:  pkgconfig(python3)
BuildRequires:  python3dist(gpg)
BuildRequires:  python3dist(pyxattr)
BuildRequires:  python3dist(requests)
BuildRequires:  python3dist(sphinx)
BuildRequires:  python3dist(sphinx-rtd-theme)
Requires:       %{name}%{?_isa} = %{version}-%{release}

%description -n python3-%{name}
Python 3 bindings for the librepo library.

%prep
%autosetup -p1

%build
%cmake \
    -DWITH_ZCHUNK=%{?with_zchunk:ON}%{!?with_zchunk:OFF} \
    -DUSE_GPGME=%{?with_use_gpgme:ON}%{!?with_use_gpgme:OFF} \
    -DUSE_RUN_GNUPG_USER_SOCKET=%{?with_run_gnupg_user_socket:ON}%{!?with_run_gnupg_user_socket:OFF} \
    -DENABLE_SELINUX=%{?with_selinux:ON}%{!?with_selinux:OFF}
%cmake_build

%check
%ctest

%install
%cmake_install

%files -n %{libname}
%license COPYING
%doc README.md
%{_libdir}/%{name}.so.%{major}

%files -n %{libname_devel}
%{_libdir}/%{name}.so
%{_libdir}/pkgconfig/%{name}.pc
%{_includedir}/%{name}/

%files -n python3-%{name}
%{python3_sitearch}/%{name}/