##
##  liftbridge.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_liftbridge_base  1.7.1
%define       V_liftbridge_snap  20220123

#   package information
Name:         liftbridge
Summary:      Lightweight, Fault-Tolerant Message Streams on NATS
URL:          https://liftbridge.io/
Vendor:       Tyler Treat
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Networking
License:      Apache
Version:      %{V_liftbridge_base}.%{V_liftbridge_snap}
Release:      20220124

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

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

%description
    Liftbridge provides lightweight, fault-tolerant message streams by
    implementing a durable stream augmentation for the NATS messaging
    system. It extends NATS with a Kafka-like publish-subscribe log API
    that is highly available and horizontally scalable. Use Liftbridge
    as a simpler and lighter alternative to systems like Kafka and
    Pulsar or use it to add streaming semantics to an existing NATS
    deployment.

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

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

%build
    #   build program
    (   export GOPATH=`pwd`
        cd $GOPATH/src/github.com/liftbridge-io/liftbridge
        go build -v -o liftbridge main.go
    ) || 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/liftbridge \
        $RPM_BUILD_ROOT%{l_prefix}/var/liftbridge/db \
        $RPM_BUILD_ROOT%{l_prefix}/var/liftbridge/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/liftbridge/log

    #   install program
    %{l_shtool} install -c -s -m 755 \
        src/github.com/liftbridge-io/liftbridge/liftbridge \
        $RPM_BUILD_ROOT%{l_prefix}/sbin/liftbridge
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        -e 's;@l_bash@;%{l_bash};g' \
        %{SOURCE liftbridge-tls.sh} $RPM_BUILD_ROOT%{l_prefix}/sbin/liftbridge-tls

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

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

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

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   on initial installation, generate TLS certificate/key pair
        echo "Generating TLS Certificates/Keys" | %{l_rpmtool} msg -b -t notice
        $RPM_INSTALL_PREFIX/sbin/liftbridge-tls localhost 127.0.0.1
        ( echo "Liftbridge was configured with a standard TLS certificate/key pair."
          echo "for \"localhost\" and \"127.0.0.1\". For production use, you usually let"
          echo "Liftbridge listen on an external IP address. For this the TLS"
          echo "certificate/key pair has to be regenerated with for instance:"
          echo "    \$ $RPM_INSTALL_PREFIX/sbin/liftbridge-tls www.example.com 192.168.0.1"
        ) | %{l_rpmtool} msg -b -t notice
    elif [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} liftbridge status 2>/dev/null`
        [ ".$liftbridge_active" = .yes ] && %{l_rc} liftbridge restart
    fi
    exit 0

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

