##
##  cratedb.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 information
Name:         cratedb
Summary:      IoT Database
URL:          https://crate.io/products/cratedb/
Vendor:       Crate.io
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Database
License:      Apache
Version:      6.0.3
Release:      20251014

#   list of sources
Source0:      https://cdn.crate.io/downloads/releases/crate-%{version}.tar.gz
Source1:      rc.cratedb

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

%description
    CrateDB is a distributed SQL database for IoT time-series data,
    built on top of a NoSQL foundation. It combines the familiarity of
    SQL with the scalability and data flexibility of NoSQL, enabling
    developers to: Use SQL to process any type of data, structured or
    unstructured; perform SQL queries at realtime speed, even JOINs
    and aggregates; scale simply. Customers often use CrateDB to store
    and query machine data. This is because CrateDB makes it easy and
    economical to handle the velocity, volume, and diversity of machine
    and log data.

%track
    prog cratedb = {
        version   = %{version}
        url       = https://cdn.crate.io/downloads/releases/
        regex     = crate-(__VER__)\.tar\.gz
    }

%prep
    %setup -q -n crate-%{version}

%build

%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/cratedb \
        $RPM_BUILD_ROOT%{l_prefix}/lib/cratedb \
        $RPM_BUILD_ROOT%{l_prefix}/var/cratedb/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/cratedb/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/cratedb/db

    #   install program
    rm -f bin/*.bat
    cp -rp bin lib plugins \
        $RPM_BUILD_ROOT%{l_prefix}/lib/cratedb/
    ln -s ../../etc/cratedb \
        $RPM_BUILD_ROOT%{l_prefix}/lib/cratedb/config
    ln -s ../../var/cratedb/log \
        $RPM_BUILD_ROOT%{l_prefix}/lib/cratedb/logs

    #   install configuration
    %{l_shtool} install -c -m 644 \
        -e 's;^#path\.data: /path/to/data$;path.data: %{l_prefix}/var/cratedb/db;' \
        -e 's;^#network\.host: 192\.168\.0\.1$;network.host: 127.0.0.1;' \
        config/* $RPM_BUILD_ROOT%{l_prefix}/etc/cratedb/

    #   install wrapper for shell
    (   echo "#!/bin/sh"
        echo "PYTHON_EXECUTABLE=%{l_prefix}/bin/python; export PYTHON_EXECUTABLE"
        echo "exec \$PYTHON_EXECUTABLE %{l_prefix}/lib/cratedb/bin/crash_standalone \"\${1+\"\$@\"}\""
    ) >crash.sh
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        crash.sh $RPM_BUILD_ROOT%{l_prefix}/bin/crash

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

%files -f files

%clean

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

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

