##
##  emitter.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_emitter_base 3.1
%define       V_emitter_snap 20250730

#   package information
Name:         emitter
Summary:      MQTT Broker
URL:          https://emitter.io
Vendor:       Misakai Ltd.
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Networking
License:      AGPL
Version:      %{V_emitter_base}.%{V_emitter_snap}
Release:      20250730

#   list of sources
Source0:      http://download.openpkg.org/components/versioned/emitter/emitter-%{V_emitter_snap}.tar.xz
Source1:      rc.emitter
Source2:      emitter.conf

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

%description
    Emitter is a real-time communication service for connecting
    online devices. Infrastructure and APIs for IoT, gaming, apps and
    real-time web. At its core, Emitter is a distributed, scalable and
    fault-tolerant publish-subscribe messaging platform based on MQTT
    protocol and featuring message storage.

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

%prep
    %setup -q -n emitter

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

    #   generate secrets
    ./src/github.com/emitter-io/emitter/emitter >tmp 2>&1
    license=`grep "generated new license:" tmp | sed -e 's;^.*: ;;'`
    secret=`grep "generated new secret key:" tmp | sed -e 's;^.*: ;;'`
    (   echo "##"
        echo "##  emitter.sh -- Emitter secrets"
        echo "##"
        echo ""
        echo "EMITTER_LICENSE=\"$license\""
        echo "EMITTER_SECRET=\"$secret\""
        echo ""
    ) >emitter.sh
    rm -f tmp

%install
    #   create directory 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/emitter \
        $RPM_BUILD_ROOT%{l_prefix}/var/emitter/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/emitter/run

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

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

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

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

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   display final hints on initial installation
        (   echo "Start Emitter by running:"
            echo "   \$ $RPM_INSTALL_PREFIX/bin/openpkg rc emitter start"
            echo "Then use the EMITTER_SECRET from..."
            echo "   \$ $RPM_INSTALL_PREFIX/etc/emitter/emitter.sh"
            echo "...to generate your channel keys on:"
            echo "   http://127.0.0.1:9080/keygen"
        ) | %{l_rpmtool} msg -b -t notice
    elif [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} emitter status 2>/dev/null`
        [ ".$emitter_active" = .yes ] && %{l_rc} emitter restart
    fi
    exit 0

%preun
    #   before erase, stop service and remove log files
    if [ $1 -eq 0 ]; then
        #   stop service
        %{l_rc} emitter stop 2>/dev/null

        #   remove log files
        rm -f  $RPM_INSTALL_PREFIX/var/emitter/log/* >/dev/null 2>&1 || true
        rm -f  $RPM_INSTALL_PREFIX/var/emitter/run/* >/dev/null 2>&1 || true
    fi
    exit 0

