##
##  mosquitto.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
Name:         mosquitto
Summary:      MQTT Message Broker
URL:          http://mosquitto.org/
Vendor:       Roger Light
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Networking
License:      EPL
Version:      2.0.22
Release:      20250712

#   list of sources
Source0:      http://mosquitto.org/files/source/mosquitto-%{version}.tar.gz
Source1:      mosquitto.conf
Source2:      mosquitto-acl.txt
Source3:      mosquitto-pwd.txt
Source4:      rc.mosquitto
Patch0:       mosquitto.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, cmake, gcc, gcc::with_cxx = yes
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  openssl, libwebsockets, ares, uthash, cjson
PreReq:       openssl, libwebsockets, ares, uthash, cjson

%description
    Mosquitto is a message broker that implements the MQ Telemetry
    Transport (MQTT) protocol versions 3.1.1 and 5.0. MQTT provides
    a lightweight method of carrying out messaging using a
    publish/subscribe model. This makes it suitable for "machine to
    machine" messaging such as with low power sensors or mobile devices
    such as phones, embedded computers or microcontrollers.

%track
    prog mosquitto = {
        version   = %{version}
        url       = http://mosquitto.org/download/
        regex     = mosquitto-(__VER__)\.tar\.gz
    }

%prep
    %setup -q
    %patch -p0

%build
    #   configure program
    %{l_shtool} subst %{l_value -s -a} \
        CMakeLists.txt
    DESTDIR=$RPM_BUILD_ROOT%{l_prefix} \
    cmake \
        -DCMAKE_BUILD_TYPE="Release" \
        -DCMAKE_INSTALL_PREFIX="%{l_prefix}" \
        -DCMAKE_C_COMPILER="%{l_cc}" \
        -DCMAKE_C_FLAGS="%{l_cflags} %{l_cppflags} -Wno-deprecated-declarations -pthread" \
        -DCMAKE_CXX_COMPILER="%{l_cxx}" \
        -DCMAKE_CXX_FLAGS="%{l_cxxflags} %{l_cppflags} -Wno-deprecated-declarations" \
        -DCMAKE_EXE_LINKER_FLAGS="%{l_ldflags}" \
        -DBUILD_SHARED_LIBS=OFF \
        -DWITH_STATIC_LIBRARIES=ON \
        -DSTATIC_WEBSOCKETS=ON \
        -DWITH_ADNS=ON \
        -DWITH_PIC=ON \
        -DWITH_WEBSOCKETS=ON \
        -DWITH_TLS=ON \
        -DWITH_TLS_PSK=ON \
        -DWITH_EC=ON \
        -DWITH_SRV=ON \
        .

    #   build program
    %{l_make} %{l_mflags}

%install
    #   install program
    %{l_make} %{l_mflags} install DESTDIR=$RPM_BUILD_ROOT

    #   strip down program
    rm -f $RPM_BUILD_ROOT%{l_prefix}/etc/mosquitto/*
    strip $RPM_BUILD_ROOT%{l_prefix}/bin/* >/dev/null 2>&1 || true
    strip $RPM_BUILD_ROOT%{l_prefix}/sbin/* >/dev/null 2>&1 || true

    #   post-adjust installation
    mv  $RPM_BUILD_ROOT%{l_prefix}/lib/libmosquitto_static.a \
        $RPM_BUILD_ROOT%{l_prefix}/lib/libmosquitto.a
    mv  $RPM_BUILD_ROOT%{l_prefix}/lib/libmosquittopp_static.a \
        $RPM_BUILD_ROOT%{l_prefix}/lib/libmosquittopp.a

    #   install default configuration
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/mosquitto \
        $RPM_BUILD_ROOT%{l_prefix}/etc/mosquitto/mosquitto.d
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE mosquitto.conf} \
        %{SOURCE mosquitto-acl.txt} \
        %{SOURCE mosquitto-pwd.txt} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/mosquitto/

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

    #   install run-time directories
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var/mosquitto/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/mosquitto/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/mosquitto/db

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

%files -f files

%clean

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

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

