##
##  nats-streaming-server.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
%define       V_nats_streaming_server_base  0.25.6
%define       V_nats_streaming_server_snap  20240204

#   package information
Name:         nats-streaming-server
Summary:      NATS Streaming Server
URL:          http://nats.io/
Vendor:       Apcera Inc.
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Networking
License:      Apache
Version:      %{V_nats_streaming_server_base}.%{V_nats_streaming_server_snap}
Release:      20240204

#   package options
%option       with_pgsql  no

#   list of sources
Source0:      http://download.openpkg.org/components/versioned/nats/nats-streaming-server-%{V_nats_streaming_server_snap}.tar.xz
Source1:      rc.nats-streaming-server
Source2:      nats-streaming-server.conf
Source3:      nats-streaming-server-setup.sh

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, go
PreReq:       OpenPKG, openpkg >= 20160101, nats-server
%if "%{with_pgsql}" == "yes"
PreReq:       postgresql
%endif

%description
    NATS is an open source, lightweight, high-performance cloud native
    infrastructure messaging system. It implements a highly scalable and
    elegant publish-subscribe (pub/sub) model. The performant nature of
    NATS make it an ideal base for building modern, reliable, scalable
    cloud native distributed systems. This is the high performance
    streaming server for NATS.

%track
    prog nats-streaming-server:base = {
        version   = %{V_nats_streaming_server_base}
        url       = https://github.com/nats-io/nats-streaming-server/releases
        regex     = nats-streaming-server-v(\d+\.\d+\.\d+)
    }
    prog nats-streaming-server:snap = {
        version   = %{V_nats_streaming_server_snap}
        url       = http://download.openpkg.org/components/versioned/nats/
        regex     = nats-streaming-server-(__VER__)\.tar\.xz
    }

%prep
    %setup -q -n nats-streaming-server

%build
    #   build program
    (   export GOPATH=`pwd`
        cd $GOPATH/src/github.com/nats-io/nats-streaming-server
        go build -v
    ) || exit $?

%install
    #   create directory 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/nats-streaming-server \
%if "%{with_pgsql}" == "yes"
        $RPM_BUILD_ROOT%{l_prefix}/share/nats-streaming-server \
        $RPM_BUILD_ROOT%{l_prefix}/var/nats-streaming-server/data/db \
%endif
        $RPM_BUILD_ROOT%{l_prefix}/var/nats-streaming-server/data/store \
        $RPM_BUILD_ROOT%{l_prefix}/var/nats-streaming-server/data/raft \
        $RPM_BUILD_ROOT%{l_prefix}/var/nats-streaming-server/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/nats-streaming-server/log

    #   install program
    %{l_shtool} install -c -s -m 755 \
        src/github.com/nats-io/nats-streaming-server/nats-streaming-server \
        $RPM_BUILD_ROOT%{l_prefix}/sbin/nats-streaming-server

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

    #   install default configuration
%if "%{with_pgsql}" == "yes"
    store="sql"
%else
    store="file"
%endif
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        -e "s;@store@;$store;g" \
        %{SOURCE nats-streaming-server.conf} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/nats-streaming-server/

%if "%{with_pgsql}" == "yes"
    #   install PostgreSQL schema definition and setup script
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE nats-streaming-server-setup.sh} \
        $RPM_BUILD_ROOT%{l_prefix}/sbin/nats-streaming-server-setup
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        src/github.com/nats-io/nats-streaming-server/postgres.db.sql \
        $RPM_BUILD_ROOT%{l_prefix}/share/nats-streaming-server/postgresql.sql
%endif

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

%files -f files

%clean

%post
%if "%{with_pgsql}" == "yes"
    if [ $1 -eq 1 ]; then
        #   display final hints on initial installation
        ( echo "To complete this installation of NATS Streaming Server with"
          echo "PostgreSQL support, initialize the database like this:"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/openpkg rc postgresql start"
          echo "    \$ $RPM_INSTALL_PREFIX/sbin/nats-streaming-server-setup install"
        ) | %{l_rpmtool} msg -b -t notice
    fi
%endif
    if [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} nats-streaming-server status 2>/dev/null`
        [ ".$nats_streaming_server_active" = .yes ] && %{l_rc} nats-streaming-server restart
    fi
    exit 0

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

