##
##  actordb.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.10.27
%define       V_dist  20200306

#   package information
Name:         actordb
Summary:      Distributed SQL Database
URL:          http://www.actordb.com/
Vendor:       Biokoda, Sergej Jurecko
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Database
License:      MPL
Version:      %{V_opkg}.%{V_dist}
Release:      20200306

#   list of sources
Source0:      http://download.openpkg.org/components/versioned/actordb/actordb-%{V_dist}.tar.xz
Source1:      rc.actordb
Patch0:       actordb.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, erlang
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  readline, ncurses
PreReq:       readline, ncurses

%description
    ActorDB is a distributed SQL database with the scalability of a
    KV store, while keeping the query capabilities of a relational
    database. ActorDB is based on the actor model of computation. Unlike
    traditional monolithic databases, ActorDB is made out of any number
    of small independent and concurrent SQL databases called actors. You
    can think of ActorDB is a maximally sharded SQL database. Instead of
    splitting a database into N shards of M users, every user has his
    own shard in ActorDB. ActorDB is ideal as a server side database for
    apps. ActorDB internally is based on SQLite running on top of a
    RAFT-synchronized distributed LMDB KV store. ActorDB is connectable
    over MySQL protocol and Thrift.

%track
    prog actordb:release = {
        version   = %{V_opkg}
        url       = http://www.actordb.com/downloads.html
        regex     = /(__VER__)\.tar\.gz
    }
    prog actordb:snapshot = {
        version   = %{V_dist}
        url       = http://download.openpkg.org/components/versioned/actordb/
        regex     = actordb-(__VER__)\.tar\.xz
    }

%prep
    %setup -q -n actordb
    %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}"
    %{l_make} %{l_mflags} compile generate

%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/actordb \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/actordb \
        $RPM_BUILD_ROOT%{l_prefix}/var/actordb/run

    #   install program
    rm -f rel/actordb/data/*
    cp -rp rel/actordb/* \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/actordb/

    #   post-adjust installation
    mv  $RPM_BUILD_ROOT%{l_prefix}/libexec/actordb/log \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/actordb/data \
        $RPM_BUILD_ROOT%{l_prefix}/var/actordb/
    ln -s ../../var/actordb/log \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/actordb/log
    ln -s ../../var/actordb/data \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/actordb/data
    mv  $RPM_BUILD_ROOT%{l_prefix}/libexec/actordb/etc/* \
        $RPM_BUILD_ROOT%{l_prefix}/etc/actordb/
    rmdir $RPM_BUILD_ROOT%{l_prefix}/libexec/actordb/etc
    ln -s ../../etc/actordb \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/actordb/etc

    #   provide executable wrappers
    for tool in actordb actordb_console actordb_tool; do
        (   echo "#!/bin/sh"
            echo "exec %{l_prefix}/libexec/actordb/bin/$tool \${1+\"\$@\"}"
        ) >$RPM_BUILD_ROOT%{l_prefix}/bin/$tool
        chmod 755 $RPM_BUILD_ROOT%{l_prefix}/bin/$tool
    done

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

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   give hint on initial installation
        ( echo "To complete your ActorDB installation, perform the following actions."
          echo "First, set an individual \"-name\" in your server configuration:"
          echo "    \$ vi $RPM_INSTALL_PREFIX/etc/actordb/vm.args"
          echo "Second, please start the server:"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/openpkg rc actordb start"
          echo "Third, initialize the database:"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/actordb_console -f \\%{l_nil}"
          echo "      $RPM_INSTALL_PREFIX/etc/actordb/init.sql"
          echo "Now you can connect to ActorDB via its CLI:"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/actordb_console"
        ) | %{l_rpmtool} msg -b -t notice
    elif [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} actordb status 2>/dev/null`
        [ ".$actordb_active" = .yes ] && %{l_rc} actordb restart
    fi
    exit 0

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

