##
##  cassandra.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_cassandra        3.11.10
%define       V_cassandra_driver 3.14.0

#   package information
Name:         cassandra
Summary:      Wide-Column Database
URL:          http://cassandra.apache.org/
Vendor:       Facebook, Apache Software Foundation
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Database
License:      Apache
Version:      %{V_cassandra}
Release:      20210201

#   list of sources
Source0:      http://www.apache.org/dist/cassandra/%{V_cassandra}/apache-cassandra-%{V_cassandra}-bin.tar.gz
Source1:      https://files.pythonhosted.org/packages/source/c/cassandra-driver/cassandra-driver-%{V_cassandra_driver}.tar.gz
Source2:      rc.cassandra
Patch0:       cassandra.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101
PreReq:       OpenPKG, openpkg >= 20160101
PreReq:       java, JAVA-JDK, jna
BuildPreReq:  python, python-sys, python-util, python-setup
PreReq:       python, python-sys, python-util
BuildPreReq:  libev
PreReq:       libev

%description
    Apache Cassandra is a distributed database management system
    designed to handle large amounts of data across many commodity
    servers, providing high availability with no single point of
    failure. Cassandra offers robust support for clusters spanning
    multiple datacenters, with asynchronous masterless replication
    allowing low latency operations for all clients.

%track
    prog cassandra:cassandra = {
        version   = %{V_cassandra}
        url       = http://cassandra.apache.org/download/
        regex     = apache-cassandra-(\d+\.\d+(?:\.\d+)*)-bin\.tar\.gz
    }
    prog cassandra:cassandra-driver = {
        version   = %{V_cassandra_driver}
        url       = https://pypi.python.org/pypi/cassandra-driver
        regex     = "/pypi/cassandra-driver/(\d+\.\d+\.\d+)">
    }

%prep
    %setup -q -n apache-cassandra-%{version}
    %setup -q -n apache-cassandra-%{version} -T -D -a 1
    %patch -p0

%build
    #   substitute some paths
    %{l_shtool} subst %{l_value -s -a} \
         bin/cqlsh \
         conf/cassandra.yaml \
         conf/logback.xml \
         cassandra-driver-%{V_cassandra_driver}/setup.py

    #   build driver
    ( cd pylib
      PYTHONPATH=$RPM_BUILD_ROOT%{l_prefix}/lib/python/site-packages \
      %{l_prefix}/bin/python \
          setup.py build
    ) || exit $?
    ( cd cassandra-driver-%{V_cassandra_driver}
      PYTHONPATH=$RPM_BUILD_ROOT%{l_prefix}/lib/python/site-packages \
      %{l_prefix}/bin/python \
          setup.py build
    ) || exit $?

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

    #   strip down installation files
    rm -f lib/jna-*.jar
    rm -rf lib/licenses
    rm -f bin/*.bat bin/*.orig
    rm -f conf/README.txt
    rm -f conf/cassandra-env.ps1
    rm -f conf/cassandra-env.sh.orig

    #   install program components
    %{l_shtool} install -c -m 644 \
        lib/*.jar $RPM_BUILD_ROOT%{l_prefix}/lib/cassandra/
    ln -s ../jna.jar \
        $RPM_BUILD_ROOT%{l_prefix}/lib/cassandra/jna.jar
    %{l_shtool} install -c -m 755 \
        bin/* $RPM_BUILD_ROOT%{l_prefix}/libexec/cassandra/
    %{l_shtool} install -c -m 644 \
        conf/* $RPM_BUILD_ROOT%{l_prefix}/etc/cassandra/

    #   install driver
    ( cd pylib
      PYTHONPATH=$RPM_BUILD_ROOT%{l_prefix}/lib/python/site-packages \
      %{l_prefix}/bin/python \
          setup.py install \
          --root=$RPM_BUILD_ROOT \
          --prefix=%{l_prefix}
    ) || exit $?
    ( cd cassandra-driver-%{V_cassandra_driver}
      PYTHONPATH=$RPM_BUILD_ROOT%{l_prefix}/lib/python/site-packages \
      %{l_prefix}/bin/python \
          setup.py install \
          --root=$RPM_BUILD_ROOT \
          --prefix=%{l_prefix}
    ) || exit $?

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

    #   install wrapper scripts
    (   echo "#!/bin/sh"
        echo "exec %{l_prefix}/libexec/cassandra/cqlsh \${1+\"\$@\"}"
    ) >cqlsh.sh
    %{l_shtool} install -c -m 755 \
        cqlsh.sh $RPM_BUILD_ROOT%{l_prefix}/bin/cqlsh
    (   echo "#!/bin/sh"
        echo "exec %{l_prefix}/libexec/cassandra/nodetool \${1+\"\$@\"}"
    ) >nodetool.sh
    %{l_shtool} install -c -m 755 \
        nodetool.sh $RPM_BUILD_ROOT%{l_prefix}/sbin/cassandra-nodetool

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

%files -f files

%clean

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

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

