%bcond_with netfilteracct

%bcond_without cups
%global  _hardened_build 1
# Build release candidate
	
%global plugin_go_ver 0.52.2
 
%global netdata_conf_stock %{_prefix}/lib/%{name}

Name:                netdata
Version:             1.39.1
Release:             3
Summary:             Real-time performance monitoring
License:             GPLv3 and GPLv3+ and ASL 2.0 and CC-BY-4.0 and MIT and WTFPL
URL:                 https://github.com/%{name}/%{name}/
Source0:             https://github.com/%{name}/%{name}/releases/download/v%{version}/%{name}-v%{version}.tar.gz
Source1:             netdata.tmpfiles.conf
Source2:             netdata.init
Source3:             netdata.conf
Source4:             netdata.profile
Source5:             README-packager.md
Source6:             https://github.com/netdata/go.d.plugin/releases/download/v%{plugin_go_ver}/config.tar.gz
Source7:             netdata-install-go-plugins.sh
Patch0:              netdata-fix-shebang-1.39.0.patch
# Remove embedded font
Patch10:             netdata-remove-fonts-1.38.0.patch
Patch11:             trim-dbengine-test-frequency.patch

BuildRequires:       zlib-devel
BuildRequires:       git
BuildRequires:       autoconf
BuildRequires:       autoconf-archive
BuildRequires:       automake
BuildRequires:       pkgconfig
BuildRequires:       libuuid-devel
BuildRequires:       freeipmi-devel
BuildRequires:       httpd
BuildRequires:       gcc
BuildRequires:       libuv-devel
BuildRequires:       Judy-devel
BuildRequires:       lz4-devel
BuildRequires:       openssl-devel
BuildRequires:       libmnl-devel
BuildRequires:       make
BuildRequires:       libcurl-devel
BuildRequires:       systemd
BuildRequires:       openssl-devel
BuildRequires:       libpfm-devel
BuildRequires:       libyaml-devel
BuildRequires:       autogen

# Prometheus
BuildRequires:       snappy-devel
BuildRequires:       protobuf-devel
BuildRequires:       protobuf-c-devel
BuildRequires:       findutils

# Cloud client
BuildRequires:       cmake
BuildRequires:       gcc-c++
BuildRequires:       json-c-devel
BuildRequires:       libcap-devel

# For tests
BuildRequires:       libcmocka-devel

%if %{with cups}
BuildRequires:       cups-devel >= 1.7
%endif
%if %{with netfilteracct}
BuildRequires:       libnetfilter_acct-devel
%endif
BuildRequires:       python3


Requires:            nodejs
Requires:            curl
Requires:            nc
Requires:            snappy
Requires:            protobuf-c
Requires:            protobuf
Requires:            logrotate
Requires:            %{name}-data = %{version}-%{release}
Requires:            %{name}-conf = %{version}-%{release}
%description
netdata is the fastest way to visualize metrics. It is a resource
efficient, highly optimized system for collecting and visualizing any
type of realtime time-series data, from CPU usage, disk activity, SQL
queries, API calls, web site visitors, etc.
netdata tries to visualize the truth of now, in its greatest detail,
so that you can get insights of what is happening now and what just
happened, on your systems and applications.

%package data
BuildArch:           noarch
Summary:             Data files for netdata
Requires:            /usr/sbin/useradd
Requires:            /usr/sbin/groupadd
Requires:            /usr/bin/systemctl
%description data
Data files for netdata

%package conf
BuildArch:           noarch
Summary:             Configuration files for netdata
Requires:            logrotate
%description conf
Configuration files for netdata

%package freeipmi
Summary:             FreeIPMI plugin for netdata
Requires:            %{name}%{?_isa} = %{version}-%{release}
License:             GPLv3
%description freeipmi
freeipmi plugin for netdata

%prep
%setup -qn %{name}-v%{version}
%patch0 -p1
# Remove embedded font(added in requires)
%patch10 -p1
%patch11 -p1
rm -rf web/fonts web/gui/dashboard/static/media
cp %{SOURCE5} .

%build
autoreconf -ivf
%configure \
    --enable-plugin-freeipmi \
%if %{with netfilteracct}
    --enable-plugin-nfacct \
%endif
%if %{with cups}
    --enable-plugin-cups \
%endif
    --with-zlib \
    --with-math \
    --with-user=netdata
    
%make_build

# Integrate go plugins
mkdir conf.d
tar -xf %{SOURCE6} -C conf.d/
 

%install
%make_install
find %{buildroot} -name '.keep' -delete
# Unit file
mkdir -p %{buildroot}%{_unitdir}
mkdir -p %{buildroot}%{_tmpfilesdir}
mkdir -p %{buildroot}%{_sysconfdir}/logrotate.d
install -Dp -m 0644 system/systemd/netdata.service %{buildroot}%{_unitdir}/%{name}.service
install -p -m 0644 %{SOURCE1} %{buildroot}%{_tmpfilesdir}/%{name}.conf
install -Dp -m 0644 system/logrotate/netdata %{buildroot}%{_sysconfdir}/logrotate.d/netdata

mkdir -p %{buildroot}%{_localstatedir}/lib/%{name}
mkdir -p %{buildroot}%{_localstatedir}/log/%{name}
mkdir -p %{buildroot}%{_localstatedir}/cache/%{name}

install -p -m 0644 %{SOURCE3} %{buildroot}%{_sysconfdir}/%{name}/
mkdir -p %{buildroot}%{netdata_conf_stock}/conf.d
mv %{buildroot}%{_libdir}/%{name}/conf.d/* %{buildroot}%{netdata_conf_stock}/conf.d/
sed -i -e '/NETDATA_STOCK_CONFIG_DIR/ s/lib64/lib/' %{buildroot}%{_sysconfdir}/%{name}/edit-config
sed -i -e '/^script_dir/s;=.*;="\$\{NETDATA_USER_CONFIG_DIR:-%{_sysconfdir}/netdata\}";' \
    %{buildroot}%{_sysconfdir}/%{name}/edit-config

# Scripts must not be in /etc, /usr/libexec is a better place
mv %{buildroot}%{_sysconfdir}/%{name}/edit-config %{buildroot}%{_libexecdir}/%{name}/edit-config
# Fix EOL
sed -i -e 's/\r//' %{buildroot}%{_datadir}/%{name}/web/lib/tableExport-1.6.0.min.js
# Delete system dir with init scripts or unit files
rm -rf %{buildroot}%{_libdir}/%{name}/system
# Delete useless hidden dir
rm -rf %{buildroot}%{_datadir}/%{name}/web/.well-known
# Delete useless file (ubuntu)
rm -f %{buildroot}%{_sysconfdir}/%{name}/conf.d/ebpf.d/ebpf_kernel_reject_list.txt

for dir in charts.d health.d python.d statsd.d ; do
  mkdir -p %{buildroot}%{_sysconfdir}/%{name}/${dir}
done

mkdir -p %{buildroot}%{_sysconfdir}/profile.d
install -p -m 0644 %{SOURCE4} %{buildroot}%{_sysconfdir}/profile.d/netdata.sh
sed -i -e '/NETDATA_STOCK_CONFIG_DIR/s;@STOCK_CONFIG_DIR@;%{netdata_conf_stock};' %{buildroot}%{_sysconfdir}/profile.d/netdata.sh

# Integrate go plugins
mkdir -p %{buildroot}%{_sysconfdir}/%{name}/go.d
install -p conf.d/go.d.conf %{buildroot}%{netdata_conf_stock}/conf.d/go.d.conf
cp -rp conf.d/go.d %{buildroot}%{netdata_conf_stock}/conf.d/go.d
install -p -m 0644 packaging/go.d.checksums %{buildroot}%{_datadir}/%{name}/go.d.checksums
install -p -m 0750 %{SOURCE7} %{buildroot}%{_sbindir}/netdata-install-go-plugins.sh
sed -i \
    -e 's;@PLUGIN_GO_VERSION@;%{plugin_go_ver};' \
    -e 's;@DATADIR@;%{_datadir};' \
    -e 's;@LIBEXEC@;%{_libexecdir};' \
    %{buildroot}%{_sbindir}/netdata-install-go-plugins.sh

%check
make tests

%pre data
getent group netdata > /dev/null || groupadd -r netdata
getent passwd netdata > /dev/null || useradd -r -g netdata -c "NetData User" -s /sbin/nologin -d /var/log/%{name} netdata

%post
sed -i -e '/web files group/ s/root/netdata/' /etc/netdata/netdata.conf ||:
sed -i -e '/stock config directory/ s;/etc/netdata/conf.d;/usr/lib/netdata/conf.d;' /etc/netdata/netdata.conf ||:
sed -i -e '/stock health configuration directory/ s;/etc/netdata/conf.d/health.d;/usr/lib/netdata/conf.d/health.d;' /etc/netdata/netdata.conf ||:
%systemd_post %{name}.service
echo "Netdata config should be edited with %{_libexecdir}/%{name}/edit-config"
echo "Netdata go plugin can be easily installed with %{_sbindir}/netdata-install-go-plugins.sh script"

%preun
%systemd_preun %{name}.service

%postun
%systemd_postun_with_restart %{name}.service

%files
%doc README.md CHANGELOG.md README-packager.md
%license LICENSE REDISTRIBUTED.md
%{_sbindir}/%{name}
%{_sbindir}/%{name}-claim.sh
%{_sbindir}/%{name}cli
%{_libexecdir}/%{name}/*
%{_unitdir}/%{name}.service
%{_tmpfilesdir}/%{name}.conf
%caps(cap_dac_read_search,cap_sys_ptrace=ep) %attr(0750,root,netdata) %{_libexecdir}/%{name}/plugins.d/apps.plugin
%caps(cap_setuid=ep) %attr(4750,root,netdata) %{_libexecdir}/%{name}/plugins.d/cgroup-network
%attr(0750,root,netdata) %{_libexecdir}/%{name}/plugins.d/cgroup-network-helper.sh
%caps(cap_setuid=ep) %attr(4750,root,netdata) %{_libexecdir}/%{name}/plugins.d/perf.plugin
%caps(cap_setuid=ep) %attr(4750,root,netdata) %{_libexecdir}/%{name}/plugins.d/slabinfo.plugin
%if %{with cups}
%attr(0750,root,netdata) %{_libexecdir}/%{name}/plugins.d/cups.plugin
%endif
%exclude %{_libexecdir}/%{name}/edit-config
%exclude %{_libexecdir}/%{name}/plugins.d/freeipmi.plugin
%attr(0755, netdata, netdata) %{_localstatedir}/lib/%{name}
%attr(0755, netdata, netdata) %dir %{_localstatedir}/cache/%{name}
%attr(0755, netdata, netdata) %dir %{_localstatedir}/log/%{name}
%config(noreplace) %{_sysconfdir}/logrotate.d/%{name}
%attr(0750,root,netdata)%{_sbindir}/netdata-install-go-plugins.sh

%files conf
%doc README.md
%license LICENSE REDISTRIBUTED.md
%dir %{_sysconfdir}/%{name}
%dir %{_sysconfdir}/%{name}/charts.d
%dir %{_sysconfdir}/%{name}/health.d
%dir %{_sysconfdir}/%{name}/python.d
%dir %{_sysconfdir}/%{name}/statsd.d
%dir %{_sysconfdir}/%{name}/go.d
%config(noreplace) %{_sysconfdir}/%{name}/%{name}.conf
%dir %{netdata_conf_stock}/conf.d
%{netdata_conf_stock}/conf.d/*
%config %{_sysconfdir}/logrotate.d/netdata
%config %{_sysconfdir}/profile.d/netdata.sh
%dir %{_libexecdir}/%{name}
%{_libexecdir}/%{name}/edit-config
%{_sysconfdir}/netdata/.install-type

%files data
%doc README.md
%license LICENSE REDISTRIBUTED.md
%dir %{_datadir}/%{name}
%attr(-, root, netdata) %{_datadir}/%{name}/web
%{_datadir}/%{name}/go.d.checksums

%files freeipmi
%doc README.md
%license LICENSE REDISTRIBUTED.md
%caps(cap_setuid=ep) %attr(4750,root,netdata) %{_libexecdir}/%{name}/plugins.d/freeipmi.plugin

%changelog
* Thu Jan 04 2024 Ge Wang <wang__ge@126.com> - 1.39.1-3
- recover check number printing and trim dbengine test frequency
 
* Thu Sep 28 2023 xu_ping <707078654@qq.com> - 1.39.1-2
- fix testcase failed.

* Tue May 23 2023 liyanan <thistleslyn@163.com> - 1.39.1-1
- update to 1.39.1

* Mon Jan 30 2023 wangkai <wangkai385@h-partners.com> - 1.37.1-1
- Upgrade to version 1.37.1 for fix CVE-2023-22496,CVE-2023-22497

* Thu Sep 30 2021 xuping <xuping33@huawei.com> - 1.16.0-4
- remove install requires glyphicons-halfings-fonts

* Thu Aug 19 2021 baizhonggui <baizhonggui@huawei.com> - 1.16.0-3
- Fix python2 and python3 syntax is incompatible

* Wed Aug 04 2021 sunguoshuai <sunguoshuai@huawei.com> - 1.16.0-2
- Fix missing extern in some global variables

* Thu Jun 24 2021 baizhonggui <baizhonggui@huawei.com> - 1.16.0-1
- package init
