##
##  rabbitmq.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:         rabbitmq
Summary:      AMQP Server
URL:          http://www.rabbitmq.com/
Vendor:       Rabbit Technologies Ltd.
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        PLUS
Group:        Networking
License:      MPL
Version:      3.12.4
Release:      20230828

#   list of sources
Source0:      https://github.com/rabbitmq/rabbitmq-server/releases/download/v%{version}/rabbitmq-server-generic-unix-%{version}.tar.xz
Source1:      rc.rabbitmq
Source2:      rabbitmq-env.conf
Source3:      rabbitmq.config

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

%description
    The RabbitMQ server is an implementation of an AMQP (Advanced
    Message Queuing Protocol) broker. It is written on top of the
    widely-used Erlang-based Open Telecom Platform (OTP), and is
    platform neutral.

%track
    prog rabbitmq = {
        version   = %{version}
        url       = http://www.rabbitmq.com/download.html
        regex     = rabbitmq-server-generic-unix-(__VER__)\.tar\.xz
    }

%prep
    %setup -q -n rabbitmq_server-%{version}

%build

%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/rabbitmq \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/rabbitmq \
        $RPM_BUILD_ROOT%{l_prefix}/man/man1 \
        $RPM_BUILD_ROOT%{l_prefix}/man/man5 \
        $RPM_BUILD_ROOT%{l_prefix}/var/rabbitmq/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/rabbitmq/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/rabbitmq/db

    #   configure paths
    %{l_shtool} subst \
        -e 's;\(CONF_ENV_FILE\)=.*;\1=%{l_prefix}/etc/rabbitmq/rabbitmq-env.conf;g' \
        sbin/rabbitmq-defaults

    #   install programs and manpages
    for prg in rabbitmqctl rabbitmq-server rabbitmq-env rabbitmq-defaults rabbitmq-plugins; do
        ( echo "#!/bin/sh"
          echo "exec %{l_prefix}/libexec/rabbitmq/sbin/$prg \${1+\"\$@\"}"
        ) >$prg
        %{l_shtool} install -c -m 755 \
            $prg $RPM_BUILD_ROOT%{l_prefix}/bin/$prg
        if [ -f "share/man/man1/$prg.1.gz" ]; then
            %{l_gzip} -d -c share/man/man1/$prg.1.gz \
                >$RPM_BUILD_ROOT%{l_prefix}/man/man1/$prg.1
        fi
    done
    %{l_gzip} -d -c share/man/man5/rabbitmq-env.conf.5.gz \
        >$RPM_BUILD_ROOT%{l_prefix}/man/man5/rabbitmq-env.conf.5

    #   install program components
    cp -r escript sbin plugins \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/rabbitmq/

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

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

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

%files -f files

%clean

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

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

