##
##  kafka.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_kafka  4.1.0
%define       V_scala  2.13

#   package information
Name:         kafka
Summary:      Distributed Streaming Platform
URL:          https://kafka.apache.org/
Vendor:       Apache Software Foundation
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Networking
License:      Apache
Version:      %{V_kafka}
Release:      20250905

#   list of sources
Source0:      https://www.apache.org/dist/kafka/%{V_kafka}/kafka_%{V_scala}-%{V_kafka}.tgz
Source1:      rc.kafka

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

%description
    Apache Kafka is used for building real-time data pipelines and
    streaming apps. It is horizontally scalable, fault-tolerant, wicked
    fast, and runs in production in thousands of companies.

%track
    prog kafka = {
        version   = %{V_kafka}
        url       = https://kafka.apache.org/downloads
        regex     = kafka_%{V_scala}-(__VER__)\.tgz
    }

%prep
    %setup -q -n kafka_%{V_scala}-%{V_kafka}

%build
    #   adjust paths
    %{l_shtool} subst \
        -e 's;/bin/bash;%{l_bash};g' \
        bin/*.sh

%install
    #   create installation hierarchy
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/bin \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d \
        $RPM_BUILD_ROOT%{l_prefix}/etc/kafka \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/kafka/bin \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/kafka/libs \
        $RPM_BUILD_ROOT%{l_prefix}/var/kafka/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/kafka/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/kafka/data

    #   install program
    %{l_shtool} install -c -m 755 \
        bin/* \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/kafka/bin/
    %{l_shtool} install -c -m 644 \
        libs/* \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/kafka/libs/

    #   install configuration
    %{l_shtool} install -c -m 644 \
        -e 's;/tmp/kafka-logs;%{l_prefix}/var/kafka/data;g' \
        config/* \
        $RPM_BUILD_ROOT%{l_prefix}/etc/kafka/
    ln -s ../../etc/kafka \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/kafka/config
    ln -s ../../var/kafka/log \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/kafka/logs

    #   provide wrapper scripts for CLI tools
    for prog in \
        kafka-acls \
        kafka-broker-api-versions \
        kafka-cluster \
        kafka-configs \
        kafka-console-consumer \
        kafka-console-producer \
        kafka-consumer-groups \
        kafka-consumer-perf-test \
        kafka-delegation-tokens \
        kafka-delete-records \
        kafka-dump-log \
        kafka-features \
        kafka-get-offsets \
        kafka-leader-election \
        kafka-log-dirs \
        kafka-metadata-quorum \
        kafka-metadata-shell \
        kafka-mirror-maker \
        kafka-producer-perf-test \
        kafka-reassign-partitions \
        kafka-replica-verification \
        kafka-run-class \
        kafka-server-start \
        kafka-server-stop \
        kafka-storage \
        kafka-streams-application-reset \
        kafka-topics \
        kafka-transactions \
        kafka-verifiable-consumer \
        kafka-verifiable-producer; do
        (   echo "#!/bin/sh"
            echo "exec %{l_prefix}/libexec/kafka/bin/$prog.sh \${1+\"\$@\"}"
        ) >$RPM_BUILD_ROOT%{l_prefix}/bin/$prog
        chmod 755 $RPM_BUILD_ROOT%{l_prefix}/bin/$prog
    done

    #   install run-command script
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE rc.kafka} \
        $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/kafka/*' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/kafka/*'

%files -f files

%clean

%post
    if [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} kafka status 2>/dev/null`
        [ ".$kafka_active" = .yes ] && %{l_rc} kafka restart
    fi
    exit 0

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

