##
##  dalmatinerdb.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  0.3.4p1
%define       V_dist  20200119

#   package information
Name:         dalmatinerdb
Summary:      Distributed Time-Series Database
URL:          http://dalmatinerdb.io/
Vendor:       Heinz N. Gies
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Database
License:      MIT
Version:      %{V_opkg}.%{V_dist}
Release:      20200119

#   list of sources
Source0:      http://download.openpkg.org/components/versioned/dalmatinerdb/dalmatinerdb-%{V_dist}.tar.xz
Source1:      rc.dalmatinerdb
Source2:      ddb.conf
Source3:      ddb-rules.ot
Patch0:       dalmatinerdb.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, erlang19, gcc, gcc::with_cxx = yes
PreReq:       OpenPKG, openpkg >= 20160101

%description
    DalmatinerDB is a distributed Time-Series/Metric Database based on
    the Riak Core platform and Erlang. It provides: a highly optimized
    network protocol, sequential reads/writes for metrics and a
    client-server model that takes as much load from the actual store as
    possible.

%track
    prog dalmatinerdb:release = {
        version   = %{V_opkg}
        url       = https://github.com/dalmatinerdb/dalmatinerdb/releases
        regex     = (__VER__)\.tar\.gz
    }
    prog dalmatinerdb:snapshot = {
        version   = %{V_dist}
        url       = http://download.openpkg.org/components/versioned/dalmatinerdb/
        regex     = dalmatinerdb-(__VER__)\.tar\.xz
    }

%prep
    %setup -q -n dalmatinerdb
    %patch -p0

%build
    #   build program
    export CC="%{l_cc}"
    export CXX="%{l_cxx}"
    export CFLAGS="%{l_cflags -O} -Wno-unused-function -Wno-maybe-uninitialized"
    export CXXFLAGS="%{l_cxxflags -O} -Wno-unused-function -Wno-maybe-uninitialized"
    export CPPFLAGS="%{l_cppflags}"
    export LDFLAGS="%{l_ldflags}"
    export PATH=%{l_prefix}/lib/erlang19/erlang/bin:$PATH
    export HOME=`pwd`
    ./rebar3 as smartos release

%install
    #   create installation hierarchy
    %{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/dalmatinerdb \
        $RPM_BUILD_ROOT%{l_prefix}/lib/dalmatinerdb \
        $RPM_BUILD_ROOT%{l_prefix}/var/dalmatinerdb/db \
        $RPM_BUILD_ROOT%{l_prefix}/var/dalmatinerdb/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/dalmatinerdb/run

    #   install program
    rm -rf _build/smartos/rel/ddb/lib/*/c_src
    cp -rp _build/smartos/rel/ddb/* \
        $RPM_BUILD_ROOT%{l_prefix}/lib/dalmatinerdb/
    for prog in ddb ddb-admin; do
        (   echo "#!/bin/sh"
            echo "RUNNER_USER=\"\`id -u -n\`\""
            echo "export RUNNER_USER"
            echo "RUNNER_ETC_DIR=%{l_prefix}/etc/dalmatinerdb"
            echo "export RUNNER_ETC_DIR"
            echo "RUNNER_LOG_DIR=%{l_prefix}/var/dalmatinerdb/log"
            echo "export RUNNER_LOG_DIR"
            echo "RUNNER_GEN_DIR=%{l_prefix}/var/dalmatinerdb/db"
            echo "export RUNNER_GEN_DIR"
            echo "exec %{l_prefix}/lib/dalmatinerdb/bin/$prog \${1+\"\$@\"}"
        ) >$RPM_BUILD_ROOT%{l_prefix}/sbin/$prog
        chmod 755 $RPM_BUILD_ROOT%{l_prefix}/sbin/$prog
    done

    #   install default configuration
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE ddb.conf} \
        %{SOURCE ddb-rules.ot} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/dalmatinerdb/
    rm -f $RPM_BUILD_ROOT%{l_prefix}/lib/dalmatinerdb/etc/ddb.conf
    ln -s ../../../etc/dalmatinerdb/ddb.conf \
        $RPM_BUILD_ROOT%{l_prefix}/lib/dalmatinerdb/etc/

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

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

%files -f files

%clean

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

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

