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

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

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

%description
    ActiveMQ Apollo is a faster, more reliable, easier to maintain
    messaging broker built from the foundations of the original
    ActiveMQ. It accomplishes this using a radically different threading
    and message dispatching architecture. Like ActiveMQ, Apollo is a
    multi-protocol broker and supports STOMP, AMQP, MQTT, Openwire, SSL,
    and WebSockets.

%track
    prog apollo = {
        version   = %{version}
        url       = https://activemq.apache.org/apollo/download.html
        regex     = apache-apollo-(\d+\.\d+\.\d+)-unix-distro\.tar\.gz
    }

%prep
    %setup -q -n apache-apollo-%{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/apollo \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/apollo/bin \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/apollo/lib \
        $RPM_BUILD_ROOT%{l_prefix}/var/apollo/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/apollo/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/apollo/data \
        $RPM_BUILD_ROOT%{l_prefix}/var/apollo/tmp

    #   install program components
    %{l_shtool} install -c -m 755 \
        bin/apollo $RPM_BUILD_ROOT%{l_prefix}/libexec/apollo/bin/
    %{l_shtool} install -c -m 644 \
        lib/* $RPM_BUILD_ROOT%{l_prefix}/libexec/apollo/lib/

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

    #   install default configuration
    ln -s ../../etc/apollo $RPM_BUILD_ROOT%{l_prefix}/var/apollo/etc
    for name in `grep "^<file" %{SOURCE apollo.txt} | sed -e 's;^.*name=";;' -e 's;".*$;;'`; do
        (echo ""; cat %{SOURCE apollo.txt}; echo "") |\
            sed -e "1,/^<file name=\"$name\">/d" -e "/<\/file>/,\$d" >$name
        %{l_shtool} install -c -m 644 %{l_value -s -a} \
            $name $RPM_BUILD_ROOT%{l_prefix}/etc/apollo/
    done
    (   cd $RPM_BUILD_ROOT%{l_prefix}/etc/apollo/
        uudecode keystore.uu
        rm keystore.uu
    ) || exit $?

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

%files -f files

%clean

%post
    #   after upgrade, restart service
    if [ $1 -eq 2 ]; then
        eval `%{l_rc} apollo status 2>/dev/null`
        [ ".$apollo_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} apollo stop 2>/dev/null
        rm -f  $RPM_INSTALL_PREFIX/var/apollo/log/*  >/dev/null 2>&1 || true
        rm -f  $RPM_INSTALL_PREFIX/var/apollo/run/*  >/dev/null 2>&1 || true
        rm -rf $RPM_INSTALL_PREFIX/var/apollo/data/* >/dev/null 2>&1 || true
        rm -rf $RPM_INSTALL_PREFIX/var/apollo/tmp/*  >/dev/null 2>&1 || true
    fi
    exit 0

