%if 0%{?rhel}
%bcond_with ronn
%else
%bcond_without ronn
%endif
%if 0%{?rhel} && 0%{?rhel} <= 9
%bcond_with autoconf
%else
%bcond_without autoconf
%endif

Name:               carbon-c-relay
Version:            3.8.1
Release:            %autorelease
Summary:            Enhanced C implementation of Carbon relay, aggregator and rewriter
License:            Apache-2.0
URL:                https://github.com/grobian/carbon-c-relay
Source0:            https://github.com/grobian/carbon-c-relay/archive/v%{version}/%{name}-%{version}.tar.gz
Source1:            carbon-c-relay.service
Source2:            carbon-c-relay.conf
Source3:            carbon-c-relay.sysusers
Source4:            carbon-c-relay.sysconfig.systemd
Patch0001:          https://github.com/grobian/carbon-c-relay/commit/7b8ddf9d9707ab23f40e5f225ec08b7e8188e5f7.patch#/0001-load-whole-ssl-certificate-chain-when-loading-certif.patch

%if %{with autoconf}
BuildRequires:      autoconf >= 2.71
BuildRequires:      automake
%endif
BuildRequires:      libtool
BuildRequires:      gcc
BuildRequires:      make
BuildRequires:      bison
BuildRequires:      flex

%if %{with ronn}
BuildRequires:      /usr/bin/ronn
%endif

# --with-gzip
BuildRequires:      zlib-devel
# --with-lz4
BuildRequires:      lz4-devel
# --with-snappy
BuildRequires:      snappy-devel
# --with-ssl
BuildRequires:      openssl-devel
# --with-pcre2
BuildRequires:      pcre2-devel

BuildRequires:      systemd-rpm-macros
%{?sysusers_requires_compat}

# carbon-c-relay ships a bundled md5 library for which an exception exists
# see: https://fedoraproject.org/wiki/Packaging:No_Bundled_Libraries
Provides:           bundled(md5-peslyak)

%description
Carbon-like Graphite line mode relay. This project aims to be a replacement of
the original Carbon relay. The main reason to build a replacement is
performance and configurability. Carbon is single threaded, and sending
metrics to multiple consistent-hash clusters requires chaining of relays. This
project provides a multithreaded relay which can address multiple targets and
clusters for each and every metric based on pattern matches.

%prep
%autosetup -p1
# remove pregenerated bison and flex files
rm conffile.tab.c conffile.tab.h conffile.yy.c

%build
%if %{with autoconf}
autoreconf -vfi
%endif
%configure \
  --with-gzip \
  --with-lz4 \
  --with-snappy \
  --with-ssl \
  --with-oniguruma=no \
  --with-pcre2 \
  --with-pcre=no \
  %{nil}
%make_build

%if %{with ronn}
ronn --roff carbon-c-relay.md > carbon-c-relay.1
%else
cp -a relay.1 carbon-c-relay.1
%endif

%install
install -Dpm0755 relay %{buildroot}%{_bindir}/carbon-c-relay
install -Dpm0644 %{SOURCE2} %{buildroot}%{_sysconfdir}/carbon-c-relay.conf
install -Dpm0644 carbon-c-relay.1 %{buildroot}%{_mandir}/man1/carbon-c-relay.1
install -Dpm0644 %{SOURCE1} %{buildroot}%{_unitdir}/carbon-c-relay.service
install -Dpm0644 %{SOURCE4} %{buildroot}%{_sysconfdir}/sysconfig/carbon-c-relay
install -Dpm0644 %{SOURCE3} %{buildroot}%{_sysusersdir}/carbon-c-relay.conf

%check
# https://github.com/grobian/carbon-c-relay/issues/403
#make test

%pre
%sysusers_create_compat %{SOURCE3}

%post
%systemd_post carbon-c-relay.service

%preun
%systemd_preun carbon-c-relay.service

%postun
%systemd_postun_with_restart carbon-c-relay.service

%files
%license LICENSE.md
%doc carbon-c-relay.md ChangeLog.md
%{_bindir}/carbon-c-relay
%{_mandir}/man1/carbon-c-relay.1*
%config(noreplace) %{_sysconfdir}/carbon-c-relay.conf
%config(noreplace) %{_sysconfdir}/sysconfig/carbon-c-relay
%{_unitdir}/carbon-c-relay.service
%{_sysusersdir}/carbon-c-relay.conf

%changelog
%autochangelog