%define _unpackaged_files_terminate_build 0
%define distribution %(/usr/lib/rpm/redhat/dist.sh --distnum)

#-------------------------------------------------------------------------------
# Custom strip command for CC7
#-------------------------------------------------------------------------------
%if 0%{distribution} == 7
%global __strip /opt/rh/devtoolset-8/root/bin/strip
%define xrootd_jemalloc 1
%else
%define xrootd_jemalloc 0
%endif

# Define required dependency versions
%define xrootd_version 1:5.6.3
%define eos_rocksdb_version 0:8.8.1

#-------------------------------------------------------------------------------
# Package definitions
#-------------------------------------------------------------------------------
Summary: A highly available key-value store
Name: quarkdb
Version: @VERSION_FULL@
Release: 1%{dist}
Prefix: /usr
License: none
Group: Applications/File

Source: %{name}-%{version}.tar.gz
BuildRoot: %{_tmppath}/%{name}-root

BuildRequires: xrootd-server-devel >= %{xrootd_version}
BuildRequires: xrootd-devel >= %{xrootd_version}
BuildRequires: xrootd-private-devel >= %{xrootd_version}
BuildRequires: eos-rocksdb = %{eos_rocksdb_version}

%if 0%{distribution} == 7
BuildRequires: centos-release-scl
BuildRequires: devtoolset-8
%define cmake cmake3
%else
%define cmake cmake
%endif

BuildRequires: %{cmake} >= 3.17
BuildRequires: python3
BuildRequires: libuuid-devel
BuildRequires: zlib-static
BuildRequires: zlib-devel
BuildRequires: openssl-devel
BuildRequires: libzstd-devel
BuildRequires: bzip2-devel
BuildRequires: lz4-devel
BuildRequires: snappy-devel
BuildRequires: fmt-devel
BuildRequires: elfutils-devel
Requires: elfutils-devel
BuildRequires: jemalloc-devel
Requires: jemalloc
Requires: libzstd
Requires: lz4
Requires: snappy
Requires: bzip2
Requires: xrootd-server >= %{xrootd_version}

%description
A highly available key-value store.

%prep
%setup -n %{name}-%{version}

%build

%if 0%{distribution} == 7
source /opt/rh/devtoolset-8/enable
%endif

mkdir -p build
cd build

%{cmake} ../ -DXROOTD_JEMALLOC=%{xrootd_jemalloc} -DCMAKE_BUILD_TYPE=RelWithDebInfo
%{__make} %{_smp_mflags}

%install

# "make" will remember to use devtoolset, but find-debuginfo.sh apears to be
# an implicit part of the install section, and we definitely want to use updated
# binutils when generating debuginfo.
%if 0%{distribution} == 7
source /opt/rh/devtoolset-8/enable
%endif

cd build
%{__make} install DESTDIR=$RPM_BUILD_ROOT

%clean
rm -rf $RPM_BUILD_ROOT

%files
%defattr(-, root, root, -)
%{_libdir}/libXrdQuarkDB.so
%{_bindir}/quarkdb-tests
%{_bindir}/quarkdb-stress-tests
%{_bindir}/quarkdb-sudo-tests
%{_bindir}/quarkdb-bench
%{_bindir}/quarkdb-create
%{_bindir}/quarkdb-ldb
%{_bindir}/quarkdb-recovery
%{_bindir}/quarkdb-server
%{_bindir}/quarkdb-sst-inspect
%{_bindir}/quarkdb-validate-checkpoint
