%bcond_with check
# No Qt5 on RHEL 10 and higher
%bcond_without qt5
%bcond_without qt6
%bcond_with python

%global base_name gpgme
%global gnupg2_min_ver 2.2.24
%global libgpg_error_min_ver 1.40

# we are doing out of source build
%global _configure ../configure

# brpldconfig creates links that conflict with original gpgme
%undefine __brp_ldconfig

# We are using python3.11, make sure sitearch is correct
%global python3_pkgversion 3.11

%global _syslibdir  %{_libdir}
%global _sysincludedir  %{_includedir}

%global _prefix     /opt/rx
%global _sysconfdir %{_prefix}/etc
%global _includedir %{_prefix}/include
%global _libdir     %{_prefix}/%{_lib}
%global _sbindir    %{_prefix}/sbin
%global _datadir    %{_prefix}/share
%global _mandir     %{_datadir}/man
%global _docdir     %{_datadir}/doc
%global _infodir    %{_datadir}/info

Name:           rx-gpgme
Summary:        GnuPG Made Easy - high level crypto API - version 1.23
Version:        1.23.2
Release:        1%{?dist}

# MIT: src/cJSON.{c,h} (used by gpgme-json)
License:        LGPL-2.1-or-later AND MIT
URL:            https://gnupg.org/related_software/gpgme/
Source0:        https://gnupg.org/ftp/gcrypt/gpgme/gpgme-%{version}.tar.bz2
Source2:        gpgme-multilib.h

## downstream patches
# Don't add extra libs/cflags in gpgme-config/cmake equivalent
Patch1001:      0001-don-t-add-extra-libraries-for-linking.patch
# add -D_FILE_OFFSET_BITS... to gpgme-config, upstreamable
Patch1002:      gpgme-1.3.2-largefile.patch
# Let's fix stupid AX_PYTHON_DEVEL
Patch1003:      0001-fix-stupid-ax_python_devel.patch
# Allow extra options to be passed to setup.py during installation
Patch1004:      0002-setup_py_extra_opts.patch
 
## temporary downstream fixes
# Skip lang/qt/tests/t-remarks on gnupg 2.4+
Patch3001:      1001-qt-skip-test-remarks-for-gnupg2-2.4.patch

BuildRequires:  make
BuildRequires:  cmake
#BuildRequires:  gcc
BuildRequires:  clang
BuildRequires:  gawk
BuildRequires:  texinfo
BuildRequires:  rx-gnupg2 >= %{gnupg2_min_ver}
BuildRequires:  rx-gnupg2-smime
BuildRequires:  pkgconfig(gpg-error) >= %{libgpg_error_min_ver}
BuildRequires:  libassuan-devel >= 2.4.2

# For python bindings
BuildRequires:  swig

# to remove RPATH
BuildRequires:  chrpath

# For AutoReq cmake-filesystem
BuildRequires:  cmake

Requires:       rx-gnupg2 >= %{gnupg2_min_ver}

Provides:       %{name}%{?_isa} = %{?epoch:%{epoch}:}%{version}-%{release}

# On the following architectures workaround multiarch conflict of -devel packages:
%define multilib_arches %{ix86} x86_64 ia64 ppc ppc64 s390 s390x %{sparc}

%description
GnuPG Made Easy (GPGME) is a library designed to make access to GnuPG
easier for applications.  It provides a high-level crypto API for
encryption, decryption, signing, signature verification and key
management.

%package devel
Summary:        Development headers and libraries for %{name}
Requires:       %{name}%{?_isa} = %{?epoch:%{epoch}:}%{version}-%{release}
Requires:       pkgconfig(gpg-error) >= %{libgpg_error_min_ver}

# Conflicts with files in gpgme devel packages
Conflicts:      gpgme-devel


%description devel
%{summary}.


%package -n %{name}pp
Summary:        C++ bindings/wrapper for GPGME
Provides:       %{name}-pp = %{?epoch:%{epoch}:}%{version}-%{release}
Provides:       %{name}-pp%{?_isa} = %{?epoch:%{epoch}:}%{version}-%{release}
Provides:       %{name}pp%{?_isa} = %{?epoch:%{epoch}:}%{version}-%{release}
Requires:       %{name}%{?_isa} = %{?epoch:%{epoch}:}%{version}-%{release}

%description -n %{name}pp
%{summary}.

%package -n %{name}pp-devel
Summary:        Development libraries and header files for %{name}-pp
Provides:       %{name}-pp-devel = %{?epoch:%{epoch}:}%{version}-%{release}
Provides:       %{name}-pp-devel%{?_isa} = %{?epoch:%{epoch}:}%{version}-%{release}
Requires:       %{name}pp%{?_isa} = %{?epoch:%{epoch}:}%{version}-%{release}
Requires:       %{name}-devel%{?_isa}

# Conflicts with files in gpgme devel packages
Conflicts:      gpgmepp-devel <= 1.16

%description -n %{name}pp-devel
%{summary}

%if %{with qt5}
%package -n rx-q%{base_name}-qt5
Summary:        Qt5 API bindings/wrapper for GPGME
Requires:       %{name}pp%{?_isa} = %{?epoch:%{epoch}:}%{version}-%{release}
BuildRequires:  pkgconfig(Qt5Core)
BuildRequires:  pkgconfig(Qt5Test)
Obsoletes:      rx-q%{base_name} < %{?epoch:%{epoch}:}%{version}-%{release}
Provides:       rx-q%{base_name} = %{?epoch:%{epoch}:}%{version}-%{release}

%description -n rx-q%{base_name}-qt5
%{summary}.
%endif

%if %{with qt6}
%package -n rx-q%{base_name}-qt6
Summary:        Qt6 API bindings/wrapper for GPGME
Requires:       %{name}pp%{?_isa} = %{?epoch:%{epoch}:}%{version}-%{release}
BuildRequires:  pkgconfig(Qt6Core)
BuildRequires:  pkgconfig(Qt6Test)

%description -n rx-q%{base_name}-qt6
%{summary}.
%endif

%if %{with qt5} || %{with qt6}
%package -n rx-q%{base_name}-common-devel
Summary:        Common development header files for %{name}-qt5 and %{name}-qt6
Requires:       %{name}pp-devel%{?_isa}

# Conflicts with files in gpgme devel packages
Conflicts:      qgpgme-devel

%description -n rx-q%{base_name}-common-devel
%{summary}.
%endif

%if %{with qt5}
%package -n rx-q%{base_name}-qt5-devel
Summary:        Development libraries and header files for %{name}-qt5
# before libqgpgme.so symlink was moved to avoid conflict
Conflicts:      kdepimlibs-devel < 4.14.10-17
Requires:       rx-q%{base_name}-qt5%{?_isa} = %{?epoch:%{epoch}:}%{version}-%{release}
Requires:       rx-q%{base_name}-common-devel%{?_isa}
Provides:       rx-q%{base_name}-devel = %{?epoch:%{epoch}:}%{version}-%{release}
Provides:       rx-q%{base_name}-devel%{?_isa} = %{?epoch:%{epoch}:}%{version}-%{release}

# Conflicts with files in gpgme devel packages
Conflicts:      qgpgme-devel

%description -n rx-q%{base_name}-qt5-devel
%{summary}.
%endif

%if %{with qt6}
%package -n rx-q%{base_name}-qt6-devel
Summary:        Development libraries and header files for %{name}-qt6
Requires:       rx-q%{base_name}-qt6%{?_isa} = %{?epoch:%{epoch}:}%{version}-%{release}
Requires:       rx-q%{base_name}-common-devel%{?_isa}

%description -n rx-q%{base_name}-qt6-devel
%{summary}.
%endif

%if %{with python}
%package -n python%{python3_pkgversion}-gpg
Summary:        %{name} bindings for Python 3
BuildRequires:  python%{python3_pkgversion}-devel
# Needed since Python 3.12+ drops distutils
BuildRequires:  python%{python3_pkgversion}-setuptools
Requires:       %{name}%{?_isa} = %{?epoch:%{epoch}:}%{version}-%{release}

%description -n python%{python3_pkgversion}-gpg
%{summary}.
%endif

%prep
%autosetup -p1 -n gpgme-%{version}

## HACK ALERT
# The config script already suppresses the -L if it's /usr/lib, so cheat and
# set it to a value which we know will be suppressed.
sed -i -e 's|^libdir=@libdir@$|libdir=@exec_prefix@/lib|g' src/gpgme-config.in

# The build machinery does not support Python 3.9+ yet
# https://github.com/gpg/gpgme/pull/4
sed -i 's/3.8/%{python3_pkgversion}/g' configure

%build
export CC=clang
export CXX=clang++
# People neeed to learn that you can't run autogen.sh anymore
#./autogen.sh

# Since 1.16.0, we need to explicitly pass -D_LARGEFILE_SOURCE and
# -D_FILE_OFFSET_BITS=64 for the QT binding to build successfully on 32-bit
# platforms.
export CFLAGS='-I%{_sysincludedir} -I%{_sysincludedir}/Qt6 %{optflags} -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64'
export CXXFLAGS='-I%{_sysincludedir} -I%{_sysincludedir}/Qt6 %{optflags} -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64'
# Explicit new lines in C(XX)FLAGS can break naive build scripts
export CFLAGS="$(echo ${CFLAGS} | tr '\n\\' '  ')"
export CXXFLAGS="$(echo ${CXXFLAGS} | tr '\n\\' '  ')"
export SETUPTOOLS_USE_DISTUTILS=local

export PKG_CONFIG_PATH=%{_libdir}/pkgconfig${PKG_CONFIG_PATH:+:${PKG_CONFIG_PATH}}
# Link statically
export LDFLAGS="-L%{_libdir} -L%{_syslibdir} -Wl,-rpath=%{_libdir} -lgpg-error $LDFLAGS"
export PATH="%{_bindir}:$PATH"

# Force to use rx-gnupg2
sed 's@GPG_DEFAULT=no@GPG_DEFAULT="/opt/rx/bin/gpg"@g' -i configure
sed 's@GPGSM_DEFAULT=no@GPGSM_DEFAULT="/opt/rx/bin/gpgsm"@g' -i configure
sed 's@GPGCONF_DEFAULT=no@GPGCONF_DEFAULT="/opt/rx/bin/gpgconf"@g' -i configure
sed 's@G13_DEFAULT=no@G13_DEFAULT="/opt/rx/bin/g13"@g' -i configure

# Also build either qt5 or qt6
mkdir build
pushd build
%configure --disable-static --disable-silent-rules --enable-languages=cpp,%{?with_python:python,}%{?with_qt5:qt,}%{!?with_qt5:%{?with_qt6:qt6,}}
%make_build
popd

# Build qt6 in extra step if qt5 has been build
%if %{with qt5} && %{with qt6}
mkdir build-qt6
pushd build-qt6
%configure --disable-static --disable-silent-rules --enable-languages=cpp,qt6%{?with_python:,python}
%make_build
popd
%endif

%install
# When using distutils from setuptools 60+, ./setup.py install use
# the .egg format. This forces setuptools to use .egg-info format.
# SETUP_PY_EXTRA_OPTS is introduced by the Patch1004 above.
export SETUPTOOLS_USE_DISTUTILS=local
export SETUP_PY_EXTRA_OPTS="--single-version-externally-managed --root=/"
# Aliso install either qt5 or qt6
pushd build
%make_install
popd
# Install qt6 in extra step if qt5 has been installed
%if %{with qt5} && %{with qt6}
pushd build-qt6
%make_install
popd
%endif

# unpackaged files
rm -fv %{buildroot}%{_infodir}/dir
rm -fv %{buildroot}%{_libdir}/lib*.la

# Hack to resolve multiarch conflict (#341351)
%ifarch %{multilib_arches}
mv %{buildroot}%{_bindir}/gpgme-config{,.%{_target_cpu}}
cat > gpgme-config-multilib.sh <<__END__
#!/bin/sh
exec %{_bindir}/gpgme-config.\$(arch) \$@
__END__
install -D -p gpgme-config-multilib.sh %{buildroot}%{_bindir}/gpgme-config
mv %{buildroot}%{_includedir}/gpgme.h \
   %{buildroot}%{_includedir}/gpgme-%{__isa_bits}.h
install -m644 -p -D %{SOURCE2} %{buildroot}%{_includedir}/gpgme.h
%endif
chrpath -d %{buildroot}%{_bindir}/%{base_name}-tool
chrpath -d %{buildroot}%{_bindir}/%{base_name}-json
chrpath -d %{buildroot}%{_libdir}/lib%{base_name}pp.so*
# qt5
%if %{with qt5}
chrpath -d %{buildroot}%{_libdir}/libq%{base_name}.so*
%endif
# qt6
%if %{with qt6}
chrpath -d %{buildroot}%{_libdir}/libq%{base_name}qt6.so*
%endif

# autofoo installs useless stuff for uninstall
rm -vf %{buildroot}%{python2_sitelib}/gpg/install_files.txt
rm -vf %{buildroot}%{python3_sitelib}/gpg/install_files.txt

# Remove duplicate stuff
rm -rf %{buildroot}%{_bindir}/%{base_name}-json
#rm -fv %{buildroot}%{_libdir}/lib%{base_name}.so.11
#rm -fv %{buildroot}%{_libdir}/lib%{base_name}pp.so.6

mkdir -p %{buildroot}%{_syslibdir}/cmake
ln -sf %{_libdir}/cmake/Gpgmepp %{buildroot}%{_syslibdir}/cmake/Gpgmepp
%if %{with qt5}
ln -sf %{_libdir}/cmake/QGpgme %{buildroot}%{_syslibdir}/cmake/QGpgme
%endif
%if %{with qt6}
ln -sf %{_libdir}/cmake/QGpgmeQt6 %{buildroot}%{_syslibdir}/cmake/QGpgmeQt6
%endif

%if %{with check}
%check
pushd build
make check
popd
%endif

%files
%license COPYING* LICENSES
%doc AUTHORS NEWS README*
%{_libdir}/lib%{base_name}.so.11*

%files devel
%{_bindir}/%{base_name}-config
%{_bindir}/%{base_name}-tool
%ifarch %{multilib_arches}
%{_bindir}/%{base_name}-config.%{_target_cpu}
%{_includedir}/%{base_name}-%{__isa_bits}.h
%endif
%{_includedir}/%{base_name}.h
%{_libdir}/lib%{base_name}.so
%{_datadir}/aclocal/%{base_name}.m4
%{_infodir}/%{base_name}.info*
%{_libdir}/pkgconfig/%{base_name}*.pc

%files -n %{name}pp
%doc lang/cpp/README
%{_libdir}/lib%{base_name}pp.so.6*

%files -n %{name}pp-devel
%{_includedir}/%{base_name}++/
%{_libdir}/lib%{base_name}pp.so
%{_libdir}/cmake/Gpgmepp/
%{_syslibdir}/cmake/Gpgmepp

%if %{with qt5}
%files -n rx-q%{base_name}-qt5
%doc lang/qt/README
%{_libdir}/libq%{base_name}.so.15*
%endif

%if %{with qt6}
%files -n rx-q%{base_name}-qt6
%{_libdir}/libq%{base_name}qt6.so.15*
%endif

%if %{with qt5} || %{with qt6}
%files -n rx-q%{base_name}-common-devel
%{_includedir}/q%{base_name}/
%{_includedir}/QGpgME/
%endif

%if %{with qt5}
%files -n rx-q%{base_name}-qt5-devel
%{_libdir}/libq%{base_name}.so
%{_libdir}/cmake/QGpgme/
%{_syslibdir}/cmake/QGpgme
%endif

%if %{with qt6}
%files -n rx-q%{base_name}-qt6-devel
%{_libdir}/libq%{base_name}qt6.so
%{_libdir}/cmake/QGpgmeQt6/
%{_syslibdir}/cmake/QGpgmeQt6
%endif

%if %{with python}
%files -n python%{python3_pkgversion}-gpg
%doc lang/python/README
%{python3_sitearch}/gpg-*.egg-info/
%{python3_sitearch}/gpg/
%endif

%changelog
* Fri Mar 15 2024 Raven <raven@sysadmins.ws> - 1.23.2-1
- update to 1.23.2

* Fri Mar 15 2024 Raven <raven@sysadmins.ws> - 1.22.0-1
- converted from el9 epel package
