##
##  mosca.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_mosca      2.8.1
%define       V_mosca_snap 20180406

#   package information
Name:         mosca
Summary:      MQTT Broker
URL:          http://mosca.io
Vendor:       Matteo Collina
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Networking
License:      MIT
Version:      %{V_mosca}
Release:      20180406

#   list of sources
Source0:      http://download.openpkg.org/components/versioned/mosca/mosca-%{V_mosca}-%{V_mosca_snap}.tar.gz
Source1:      mosca.conf.js
Source2:      mosca.cred.json
Source3:      rc.mosca
Patch0:       mosca.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, node, node-openpkg, npm
PreReq:       OpenPKG, openpkg >= 20160101, node, x509, redis
BuildPreReq:  zmq
PreReq:       zmq

%description
    Mosca is a MQTT broker, which can be used standalone or embedded.
    Its features are: MQTT 3.1 compliant; QoS 0 and QoS 1; various
    storage options for QoS 1 offline packets, and subscriptions; as
    fast as it is possible.

%track
    prog mosca = {
        version   = %{version}
        url       = https://github.com/mcollina/mosca/releases
        regex     = v(\d+\.\d+\.\d+)\.tar\.gz
    }

%prep
    %setup -q -c
    %patch -p0

%build
    #   remove optional but run-time problematic module
    rm -rf node_modules/mosca/node_modules/dtrace-provider

    #   remove unnecessary but problematic files
    rm -rf node_modules/mosca/examples
    rm -rf node_modules/st/test
    rm -rf node_modules/mqtt/examples
    rm -rf node_modules/zmq/windows

    #   remove more unneccessary files
    rm -f node_modules/mosca/CONTRIBUTING.md
    rm -f node_modules/mosca/Docker*

    #   rebuild native Node modules
    for dir in \
        fs-ext kerberos leveldown zmq \
    ; do
        (   cd node_modules/$dir || exit $?
            npm-node-gyp clean
            npm-node-gyp rebuild
        ) || exit $?
    done

    #   remove more unneccessary files
    %{l_prefix}/bin/node-openpkg squeeze

%install
    #   create installation directories
    %{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/mosca \
        $RPM_BUILD_ROOT%{l_prefix}/lib/mosca \
        $RPM_BUILD_ROOT%{l_prefix}/var/mosca/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/mosca/log

    #   install program files
    %{l_prefix}/bin/node-openpkg install \
        %{l_prefix}/lib/mosca/

    #   provide wrapper
    ( echo "#!/bin/sh"
      echo "exec %{l_prefix}/bin/node %{l_prefix}/lib/mosca/mosca/bin/mosca \${1+\"\$@\"}"
    ) >mosca
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        mosca $RPM_BUILD_ROOT%{l_prefix}/sbin/mosca

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

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

%files -f files

%clean

%post
    if [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} mosca status 2>/dev/null`
        [ ".$mosca_active" = .yes ] && %{l_rc} mosca restart
    fi
    exit 0

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

