##
##  activemq.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:         activemq
Summary:      Enterprise Messaging and Integration Patterns Provider
URL:          http://activemq.apache.org/
Vendor:       Apache Foundation
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        PLUS
Group:        Networking
License:      Apache
Version:      5.17.4
Release:      20230226

#   list of sources
Source0:      http://www.apache.org/dist/activemq/%{version}/apache-activemq-%{version}-bin.tar.gz
Source1:      activemq.txt
Source2:      rc.activemq

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

%description
    ActiveMQ is the most popular and powerful Open-Source Enterprise
    Messaging and Integration Patterns provider. ActiveMQ supports
    OpenWire, STOMP and XMPP transports. ActiveMQ supports JMS 1.1
    including support for transient, persistent, transactional and XA
    messaging.

%track
    prog activemq = {
        version   = %{version}
        url       = https://activemq.apache.org/components/classic/download/
        regex     = apache-activemq-(\d+\.\d+\.\d+)-bin\.tar\.gz
    }

%prep
    %setup -q -n apache-activemq-%{version}

%build

%install
    #   create installation hierarchy
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d \
        $RPM_BUILD_ROOT%{l_prefix}/etc/apache/apache.d \
        $RPM_BUILD_ROOT%{l_prefix}/etc/activemq \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/activemq/endorsed \
        $RPM_BUILD_ROOT%{l_prefix}/share/activemq/service/WEB-INF \
        $RPM_BUILD_ROOT%{l_prefix}/var/activemq/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/activemq/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/activemq/data

    #   install program components
    %{l_shtool} install -c -m 644 \
        bin/activemq* $RPM_BUILD_ROOT%{l_prefix}/libexec/activemq/
    cp -rp lib \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/activemq/
    cp -rp webapps/* \
        $RPM_BUILD_ROOT%{l_prefix}/share/activemq/

    #   post-adjust installation
    %{l_shtool} subst \
        -e 's;${activemq.conf}/credentials\.properties;%{l_prefix}/etc/activemq/activemq.properties;' \
        $RPM_BUILD_ROOT%{l_prefix}/share/activemq/admin/WEB-INF/webconsole-embedded.xml

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

    #   install default configuration
    for name in `grep "^<file" %{SOURCE activemq.txt} | sed -e 's;^.*name=";;' -e 's;".*$;;'`; do
        (echo ""; cat %{SOURCE activemq.txt}; echo "") |\
            sed -e "1,/^<file name=\"$name\">/d" -e "/<\/file>/,\$d" >$name
        if [ ".$name" = ".web.xml" ]; then
            %{l_shtool} install -c -m 644 %{l_value -s -a} \
                $name $RPM_BUILD_ROOT%{l_prefix}/share/activemq/service/WEB-INF/
        elif [ ".$name" = ".activemq.conf" ]; then
            %{l_shtool} install -c -m 644 %{l_value -s -a} \
                $name $RPM_BUILD_ROOT%{l_prefix}/etc/apache/apache.d/
        else
            %{l_shtool} install -c -m 644 %{l_value -s -a} \
                $name $RPM_BUILD_ROOT%{l_prefix}/etc/activemq/
        fi
    done

    #   install example Java trust and key store
    keytool \
        -importcert \
        -noprompt \
        -alias example \
        -keystore example-server.ts \
        -file %{l_prefix}/etc/x509/example-server.crt.pem \
        -deststorepass example \
        -destkeypass example
    openssl pkcs12 \
        -export \
        -in %{l_prefix}/etc/x509/example-server.crt.pem \
        -inkey %{l_prefix}/etc/x509/example-server.key.pem \
        -CAfile %{l_prefix}/etc/x509/example-ca.crt.pem \
        -chain \
        -caname example-ca \
        -out example-server.p12 \
        -name example \
        -passout pass:example
    keytool \
        -importkeystore \
        -srckeystore example-server.p12 \
        -srcstoretype PKCS12 \
        -srcstorepass example \
        -destkeystore example-server.ks \
        -deststorepass example \
        -destkeypass example \
        -alias example
    %{l_shtool} install -c -m 644 \
        example-server.ks example-server.ts \
        $RPM_BUILD_ROOT%{l_prefix}/etc/activemq/

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

%files -f files

%clean

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

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

