##
##  surrealdb.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  1.0.0
%define       V_snap  20231108

#   package information
Name:         surrealdb
Summary:      Modern Database Server
URL:          https://surrealdb.com/
Vendor:       SurrealDB Ltd.
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Database
License:      Business Source License (BSL)
Version:      %{V_opkg}.%{V_snap}
Release:      20231108

#   list of sources
Source0:      http://download.openpkg.org/components/versioned/surrealdb/surrealdb-%{V_snap}.tar.xz
Source1:      surrealdb.conf
Source2:      surrealdb-tls.sh
Source3:      rc.surrealdb

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, rust
PreReq:       OpenPKG, openpkg >= 20160101

%description
    SurrealDB is an end-to-end cloud native NewSQL database for web,
    mobile, serverless, jamstack, backend, and traditional applications.
    SurrealDB reduces the development time of modern applications by
    simplifying your database and API stack, removing the need for most
    server-side components, allowing you to build secure, performant
    apps quicker and cheaper. SurrealDB acts as both a database and a
    modern, realtime, collaborative API backend layer. SurrealDB can
    run as a single server or in a highly-available, highly-scalable
    distributed mode - with support for SQL querying from client
    devices, GraphQL, ACID transactions, WebSocket connections,
    structured and unstructured data, graph querying, full-text
    indexing, geospatial querying, and row-by-row permissions-based
    access.

%track
    prog surrealdb = {
        version   = %{V_opkg}
        url       = https://github.com/surrealdb/surrealdb/releases
        regex     = v(\d+\.\d+\.\d+(?:-beta\.\d+)?)\.tar\.gz
    }

%prep
    %setup -q -n surrealdb

%build
    #   build program
    HOME=`pwd`
    cargo fetch -v
    export RUSTFLAGS="-lstdc++"
    cargo build --release

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

    #   install program
    %{l_shtool} install -c -s -m 755 \
        target/release/surreal $RPM_BUILD_ROOT%{l_prefix}/bin/

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

    #   install configuration file
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE surrealdb.conf} $RPM_BUILD_ROOT%{l_prefix}/etc/surrealdb/

    #   install TLS support file
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE surrealdb-tls.sh} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/surrealdb/

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

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   on initial installation, generate initial credentials
        echo "Generate initial TLS credentials..." | %{l_rpmtool} msg -b -t notice
        ( cd $RPM_INSTALL_PREFIX/etc/surrealdb && %{l_bash} surrealdb-tls.sh ) || exit $?

        #   on initial installation, display information about first steps
        ( echo "Your next steps should be:"
          echo "1. optionally (re)configure and (re)generate your TLS credentials:"
          echo "   \$ cd $RPM_INSTALL_PREFIX/etc/surrealdb"
          echo "   \$ vi surrealdb-tls.sh"
          echo "   \$ sh surrealdb-tls.sh"
          echo "2. start SurrealDB server:"
          echo "   \$ $RPM_INSTALL_PREFIX/bin/openpkg rc surrealdb start"
        ) | %{l_rpmtool} msg -b -t notice
    elif [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} surrealdb status 2>/dev/null`
        [ ".$surrealdb_active" = .yes ] && %{l_rc} surrealdb restart
    fi
    exit 0

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

