%define rel_ver 3.9.3
%define pkg_ver 4
%define _prefix /opt/zookeeper
%global debug_package %{nil}
%global _bindir /usr/bin
%global __systemctl /usr/bin/systemctl
%global zk_c_includedir /usr/include/zookeeper
%global zk_c_libdir /usr/%{_lib}

Summary: High-performance coordination service for distributed applications.
Name: zookeeper
Version: %{rel_ver}
Release: %{pkg_ver}
License: Apache-2.0 and OpenSSL and SSLeay and MIT and BSD
Group: Applications/Databases
URL: https://www.apache.org/dist/zookeeper/
Source0: https://github.com/apache/zookeeper/archive/refs/tags/%{name}-release-%{rel_ver}.tar.gz
Source1: zoo.cfg
Source2: zookeeper.service
Source3: zookeeper.sysconfig
Source4: log4j.properties 
Source5: xmvn-reactor

Patch1: Check-permissions-individually-during-admin-server-auth.patch
Patch2: Check-permissions-individually-during-admin-server-auth-build-fix.patch

BuildRoot: %{_tmppath}/%{name}-%{rel_ver}-%{release}-root
BuildRequires: gcc gcc-c++ make automake autoconf libtool openssl-devel
BuildRequires: cyrus-sasl-devel
BuildRequires: java-1.8.0-openjdk-devel,maven,hostname,maven-local,systemd
Requires: java-1.8.0-openjdk,systemd
Provides: apache-zookeeper
Provides: mvn(org.apache.zookeeper:zookeeper)

%description
ZooKeeper is a centralized service for maintaining configuration information, naming, providing distributed synchronization, and providing group services.

%package -n libzookeeper-mt2
Summary: Multi threaded C bindings for zookeeper
Group: System Environment/Libraries

%description -n libzookeeper-mt2
This package contains the multi-threaded runtime library for ZooKeeper C bindings.

%package -n libzookeeper-st2
Summary: Single threaded C bindings for zookeeper
Group: System Environment/Libraries

%description -n libzookeeper-st2
This package contains the single-threaded runtime library for ZooKeeper C bindings.

%package -n libzookeeper-mt-devel
Summary: Development files for multi threaded zookeeper C bindings
Group: Development/Libraries
Requires: libzookeeper-mt2%{?_isa} = %{version}-%{release}
Provides: libzookeeper-devel = %{version}-%{release}

%description -n libzookeeper-mt-devel
This package contains headers and development libraries for the multi-threaded
ZooKeeper C bindings.

%package -n libzookeeper-st-devel
Summary: Development files for single threaded zookeeper C bindings
Group: Development/Libraries
Requires: libzookeeper-st2%{?_isa} = %{version}-%{release}

%description -n libzookeeper-st-devel
This package contains headers and development libraries for the single-threaded
ZooKeeper C bindings.

%prep
%setup -q -n zookeeper-release-%{version}
%patch -P 1 -p1
%patch -P 2 -p1

cp %{SOURCE5} ./.xmvn-reactor
echo `pwd` > absolute_prefix.log
sed -i 's/\//\\\//g' absolute_prefix.log
absolute_prefix=`head -n 1 absolute_prefix.log`
sed -i 's/absolute-prefix/'"$absolute_prefix"'/g' .xmvn-reactor

%build
mvn -DskipTests -DskipCppUnit -Dc-test-coverage-arg=--without-cppunit package -Pfull-build
tar xvf zookeeper-assembly/target/apache-%{name}-%{rel_ver}-bin.tar.gz -C .
cp -r apache-%{name}-%{rel_ver}-bin/lib .

%install
%mvn_install

mkdir -p %{buildroot}%{_prefix}/bin
mkdir -p %{buildroot}%{_prefix}/lib
mkdir -p %{buildroot}%{_prefix}/conf
mkdir -p %{buildroot}%{zk_c_includedir}
mkdir -p %{buildroot}%{zk_c_libdir}
mkdir -p %{buildroot}%{_localstatedir}/log/zookeeper
mkdir -p %{buildroot}%{_localstatedir}/lib/zookeeper/data

install -p -D -m 755 bin/*.sh %{buildroot}%{_prefix}/bin
install -p -D -m 644 lib/*.jar %{buildroot}%{_prefix}/lib
install -p -D -m 644 conf/* %{buildroot}%{_prefix}/conf
install -p -D -m 644 %{S:1} %{buildroot}%{_prefix}/conf/zoo.cfg
install -p -D -m 644 %{S:2} %{buildroot}%{_unitdir}/zookeeper.service
install -p -D -m 644 %{S:3} %{buildroot}%{_sysconfdir}/sysconfig/zookeeper
install -p -D -m 644 %{S:4} %{buildroot}%{_prefix}/conf/log4j.properties 

install -p -m 644 zookeeper-client/zookeeper-client-c/generated/*.h %{buildroot}%{zk_c_includedir}/
install -p -m 644 zookeeper-client/zookeeper-client-c/include/*.h %{buildroot}%{zk_c_includedir}/
install -p -m 644 zookeeper-client/zookeeper-client-c/target/c/lib/libzookeeper_mt.a %{buildroot}%{zk_c_libdir}/
install -p -m 644 zookeeper-client/zookeeper-client-c/target/c/lib/libzookeeper_st.a %{buildroot}%{zk_c_libdir}/

for lib_file in zookeeper-client/zookeeper-client-c/target/c/lib/libzookeeper_*.so*; do
    if [ -L "$lib_file" ]; then
        ln -s "$(readlink "$lib_file")" "%{buildroot}%{zk_c_libdir}/$(basename "$lib_file")"
    else
        install -p -m 755 "$lib_file" %{buildroot}%{zk_c_libdir}/
    fi
done

%clean
rm -rf %{buildroot}

%files -f .mfiles
%defattr(-,root,root)
%attr(-,zookeeper,zookeeper) %{_prefix}
%dir %attr(744, zookeeper, zookeeper) %{_localstatedir}/log/zookeeper
%dir %attr(755, zookeeper, zookeeper) %{_localstatedir}/lib/zookeeper
%doc LICENSE.txt NOTICE.txt README.md
%{_unitdir}/zookeeper.service
%config(noreplace) %{_sysconfdir}/sysconfig/zookeeper

%files -n libzookeeper-mt2
%defattr(-,root,root)
%{zk_c_libdir}/libzookeeper_mt.so.*

%files -n libzookeeper-st2
%defattr(-,root,root)
%{zk_c_libdir}/libzookeeper_st.so.*

%files -n libzookeeper-mt-devel
%defattr(-,root,root)
%{zk_c_includedir}/
%{zk_c_libdir}/libzookeeper_mt.so
%{zk_c_libdir}/libzookeeper_mt.a

%files -n libzookeeper-st-devel
%defattr(-,root,root)
%{zk_c_includedir}/
%{zk_c_libdir}/libzookeeper_st.so
%{zk_c_libdir}/libzookeeper_st.a

%pre
getent group zookeeper >/dev/null || groupadd -r zookeeper
getent passwd zookeeper >/dev/null || useradd -r -g zookeeper -d / -s /sbin/nologin zookeeper
exit 0

%post
%systemd_post zookeeper.service

%post -n libzookeeper-mt2 -p /sbin/ldconfig
%postun -n libzookeeper-mt2 -p /sbin/ldconfig

%post -n libzookeeper-st2 -p /sbin/ldconfig
%postun -n libzookeeper-st2 -p /sbin/ldconfig

%preun
%systemd_preun zookeeper.service

%postun
%systemd_postun_with_restart zookeeper.service

%changelog
* Mon Jun 22 2026 lujiajun <lujiajun14@h-partners.com> - 3.9.3-4
- Add ZooKeeper C client runtime and development subpackages.

* Thu May 21 2026 xiexing <xiexing4@hisilicon.com> - 3.9.3-3
- Resolving the systemctl error during the upgrade.

* Thu Oct 23 2025 Deyuan Fan <fandeyuan@kylinos.cn> - 3.9.3-2
- fix CVE-2025-58457

* Mon Nov 18 2024 dongjiao <dongjiao@kylinos.cn> - 3.9.3-1
- update version 3.9.3 to fix CVE-2024-23944 CVE-2024-51504
 
* Fri May 10 2024 Ge Wang <wang__ge@126.com> - 3.9.1-2
- correct package groupId

* Tue Nov 28 2023 xiexing <xiexing4@hisilicon.com> - 3.9.1-1
- update version 3.9.1 to fix CVE-2023-44981

* Thu May 5 2022 xiexing <xiexing4@hisilicon.com> - 3.8.0-1
- update version

* Tue Oct 24 2021 wangyue <wangyue92@huawei.com> - 2.4
- Add systemd to buildrequire because %{_unitdir} can't recognize

* Thu Jun 24 2021 Ge Wang <wangge20@huawei.com> - 2.3
- Add provides item apache-zookeeper and add packages to system default java package directory

* Fri Jun 18 2021 lingsheng <lingsheng@huawei.com> - 2.2
- Fix reload service failure

* Thu Apr 1 2021 zhangshaoning <zhangshaoning@uniontech.com> - 2.1
- Repair status failure after stopping service

* Thu Mar 25 2021 baizhonggui <baizhonggui@huawei.com> - 2.0
- Delete %{dist} in Release

* Sun Jun 28 2020 hao zhang <unioah@isrc.iscas.ac.cn> - 1.0
- Add zookeeper.service