##
##  emqx.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_base  3.1.1
%define       V_snap  20190510

#   package information
Name:         emqx
Summary:      Massively Scalable MQTT Broker
URL:          http://emqx.io/
Vendor:       Feng Lee
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Networking
License:      MIT
Version:      %{V_base}.%{V_snap}
Release:      20190510

#   list of sources
Source0:      http://download.openpkg.org/components/versioned/emqx/emqx-%{V_snap}.tar.xz
Source1:      emqx.txt
Source2:      rc.emqx

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

%description
    emqx is a massively scalable and clusterable MQTT broker written
    in Erlang/OTP. emqx support MQTT V3.1/V3.1.1 and MQTT/5.0 protocol
    specification with extended features.

%track
    prog emqx:base = {
        version   = %{V_base}
        url       = https://github.com/emqx/emqx/releases
        regex     = v(__VER__)\.tar\.gz
    }
    prog emqx:snap = {
        version   = %{V_snap}
        url       = http://download.openpkg.org/components/versioned/emqx/
        regex     = emqx-(__VER__)\.tar\.xz
    }

%prep
    %setup -q -n emqx

%build
    #   remove problematic filed
    rm -f "deps/sqlparse/priv/oracle_keywords/Query Oracle Keywords.sql"
    rm -f "deps/sqlparse/priv/oracle_keywords/Oracle 12c.2 Keywords.xlsx"

    #   build program
    %{l_make} %{l_mflags} app rel

%install
    #   create installation hierarchy
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/bin \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d \
        $RPM_BUILD_ROOT%{l_prefix}/etc/emqx \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/emqx \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/emqx/var \
        $RPM_BUILD_ROOT%{l_prefix}/var/emqx/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/emqx/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/emqx/data \
        $RPM_BUILD_ROOT%{l_prefix}/var/emqx/data/mnesia

    #   install program
    cp -rp _rel/emqx/* \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/emqx/

    #   provide wrapper scripts for executables
    for bin in emqx emqx_ctl emqx_top; do
        ( echo "#!/bin/sh"
          echo "exec %{l_prefix}/libexec/emqx/bin/$bin \${1+\"\$@\"}"
        ) >$bin
        %{l_shtool} install -c -m 755 \
            $bin $RPM_BUILD_ROOT%{l_prefix}/bin/$bin
    done

    #   provide default configuration
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/libexec/emqx/etc
    ln -s ../../etc/emqx \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/emqx/etc
    for name in `grep "^<file" %{SOURCE emqx.txt} | sed -e 's;^.*name=";;' -e 's;".*$;;'`; do
        name_escaped=`echo "$name" | sed -e 's;/;\\\\/;g'`
        ( echo ""; cat %{SOURCE emqx.txt}; echo "" ) |\
            sed -e "1,/^<file name=\"$name_escaped\">/d" -e "/<\/file>/,\$d" >tmp.txt
        %{l_shtool} install -c -m 644 %{l_value -s -a} \
            tmp.txt $RPM_BUILD_ROOT%{l_prefix}/etc/emqx/$name
    done

    #   redirect log directory
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/libexec/emqx/log
    ln -s ../../var/emqx/log \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/emqx/log

    #   redirect var directory
    mv  $RPM_BUILD_ROOT%{l_prefix}/libexec/emqx/data/loaded_plugins \
        $RPM_BUILD_ROOT%{l_prefix}/var/emqx/data/
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/libexec/emqx/data
    ln -s ../../var/emqx/data \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/emqx/data
    ln -s ../../../var/emqx/data \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/emqx/var/data

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

%files -f files

%clean

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

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

