##
##  zookeeper.spec -- OpenPKG RPM Package Specification
##  Copyright (c) 2000-2025 OpenPKG Project <http://openpkg.org/>
##
##  Permission to use, copy, modify, and distribute this software for
##  any purpose with or without fee is hereby granted, provided that
##  the above copyright notice and this permission notice appear in all
##  copies.
##
##  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
##  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
##  SUCH DAMAGE.
##

#   package version
%define       V_opkg 3.8.5
%define       V_dist 3.8.5

#   package information
Name:         zookeeper
Summary:      Distributed Fault-Tolerant Real-Time Computation
URL:          http://zookeeper.apache.org/
Vendor:       Apache Software Foundation
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Networking
License:      Apache
Version:      %{V_opkg}
Release:      20250920

#   list of sources
Source0:      http://www.apache.org/dist/zookeeper/stable/apache-zookeeper-%{V_dist}-bin.tar.gz
Source1:      rc.zookeeper
Source2:      zookeeper.txt

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  java, JAVA-JDK
PreReq:       java, JAVA-JDK

%description
    ZooKeeper is a centralized service for maintaining configuration
    information, naming, providing distributed synchronization, and
    providing group services. All of these kinds of services are used in
    some form or another by distributed applications. Each time they are
    implemented there is a lot of work that goes into fixing the bugs
    and race conditions that are inevitable. Because of the difficulty
    of implementing these kinds of services, applications initially
    usually skimp on them, which make them brittle in the presence of
    change and difficult to manage. Even when done correctly, different
    implementations of these services lead to management complexity when
    the applications are deployed.

%track
    prog zookeeper = {
        version   = %{V_dist}
        url       = http://www.apache.org/dist/zookeeper/stable/
        regex     = apache-zookeeper-(__VER__)-bin\.tar\.gz
    }

%prep
    %setup -q -n apache-zookeeper-%{V_dist}-bin

%build
    #   remove unnecessary stuff
    rm -f [A-Z]*
    rm -f bin/*.cmd
    rm -f build.xml
    rm -f ivy*
    rm -f *.asc *.md5 *.sha1
    rm -rf docs
    rm -rf recipes
    rm -rf src
    rm -f lib/*LICENSE.txt
    rm -rf lib/cobertura
    rm -rf lib/jdiff
    rm -rf dist-maven
    rm -rf contrib
    rm -f conf/zoo_sample.cfg

    #   remove problematic stuff
    rm -f "zookeeper-contrib/zookeeper-contrib-zooinspector/licences/Apache Software Licence v2.0.txt"

%install
    #   create install directories
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/sbin \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d \
        $RPM_BUILD_ROOT%{l_prefix}/etc/zookeeper \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/zookeeper \
        $RPM_BUILD_ROOT%{l_prefix}/var/zookeeper/db \
        $RPM_BUILD_ROOT%{l_prefix}/var/zookeeper/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/zookeeper/run

    #   install default configuration
    cp -rp conf/* \
        $RPM_BUILD_ROOT%{l_prefix}/etc/zookeeper/
    rm -rf conf
    ln -s ../../etc/zookeeper \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/zookeeper/conf
    for name in `grep "^<file" %{SOURCE zookeeper.txt} | sed -e 's;^.*name=";;' -e 's;".*$;;'`; do
        name_escaped=`echo "$name" | sed -e 's;/;\\\\/;g'`
        (echo ""; cat %{SOURCE zookeeper.txt}; echo "") |\
            sed -e "1,/^<file name=\"$name_escaped\">/d" -e "/<\/file>/,\$d" >tmp.txt
        %{l_shtool} install -c -m 644 %{l_value -s -a} \
            tmp.txt $RPM_BUILD_ROOT%{l_prefix}/etc/zookeeper/$name
    done

    #   install remaining application server code
    cp -rp * \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/zookeeper/

    #   install rc script
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE rc.zookeeper} $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %{l_prefix}/etc/zookeeper/*' \
        '%attr(-,%{l_nusr},%{l_ngrp}) %{l_prefix}/var/zookeeper/*'

%files -f files

%clean

%post
    #   after upgrade, restart service
    [ $1 -eq 2 ] || exit 0
    eval `%{l_rc} zookeeper status 2>/dev/null`
    [ ".$zookeeper_active" = .yes ] && %{l_rc} zookeeper restart
    exit 0

%preun
    #   before erase, stop service and remove log files
    [ $1 -eq 0 ] || exit 0
    %{l_rc} zookeeper stop 2>/dev/null
    rm -rf $RPM_INSTALL_PREFIX/var/zookeeper/db/*  >/dev/null 2>&1 || true
    rm -f  $RPM_INSTALL_PREFIX/var/zookeeper/run/* >/dev/null 2>&1 || true
    rm -f  $RPM_INSTALL_PREFIX/var/zookeeper/log/* >/dev/null 2>&1 || true
    exit 0

