##
##  avis.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 versions
%define       V_avis          1.2.2
%define       V_avis_server   1.3.0beta1
%define       V_avis_client   1.1.1
%define       V_avis_client_c 1.2.4

#   package information
Name:         avis
Summary:      Event Router
URL:          http://avis.sourceforge.net/
Vendor:       Matthew Phillip
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        InstantMessaging
License:      GPL
Version:      %{V_avis}
Release:      20091216

#   list of sources
Source0:      http://download.sourceforge.net/avis/avis-src-%{V_avis_server}.zip
Source1:      http://download.sourceforge.net/avis/avis-client-%{V_avis_client}.zip
Source2:      http://download.sourceforge.net/avis/avis-client-%{V_avis_client_c}.tar.gz
Source3:      rc.avis

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

%description
    Avis is a multicast event bus. It provides a fast, publish/subscribe
    event routing service compatible with the commercial Elvin
    implementation developed by Mantara Software. Elvin routers can be
    federated together to form wide-area event notification networks.
    Clients can exchange events with other clients anywhere on the bus,
    subscribing to messages using pattern-matching expressions that
    select messages based on their content.

%track
    prog avis:server = {
        version   = %{V_avis_server}
        url       = http://sourceforge.net/projects/avis/files/
        regex     = avis-src-(__VER__)\.zip
    }
    prog avis:client = {
        version   = %{V_avis_client}
        url       = http://sourceforge.net/projects/avis/files/
        regex     = avis-client-(__VER__)\.zip
    }
    prog avis:client_c = {
        version   = %{V_avis_client_c}
        url       = http://sourceforge.net/projects/avis/files/
        regex     = avis-client-(__VER__)\.tar\.gz
    }

%prep
    %setup -q -T -c
    %{l_prefix}/bin/unzip -q -x %{SOURCE0}
    %{l_prefix}/bin/unzip -q -x %{SOURCE1}
    %setup -q -T -D -a 2

%build
    #   build Avis client C library
    ( cd avis-client-%{V_avis_client_c}
      CC="%{l_cc}" \
      CFLAGS="%{l_cflags -O}" \
      LIBS="-lm" \
      ./configure \
          --prefix=%{l_prefix} \
          --disable-shared
      %{l_make} %{l_mflags -O}
    ) || exit $?

%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/avis \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/avis \
        $RPM_BUILD_ROOT%{l_prefix}/var/avis

    #   install Java parts
    %{l_shtool} install -c -m 644 \
        avis-%{V_avis_server}/server/lib/avis-router.jar \
        avis-client-%{V_avis_client}/client/lib/avis-client.jar \
        avis-client-%{V_avis_client}/client/lib/avis-tools.jar \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/avis/

    #   install C parts
    ( cd avis-client-%{V_avis_client_c}
      %{l_make} %{l_mflags} install AM_MAKEFLAGS="DESTDIR=$RPM_BUILD_ROOT"
    ) || exit $?

    #   install default server configuration
    %{l_shtool} install -c -m 644 \
        -e 's;# Listen=elvin://0\.0\.0\.0;Listen=elvin://127.0.0.1:2917;' \
        avis-%{V_avis_server}/server/etc/avisd.config \
        $RPM_BUILD_ROOT%{l_prefix}/etc/avis/avisd.config

    #   install client wrapper scripts
    ( echo "#!/bin/sh"
      echo "exec %{l_prefix}/bin/java -Vsun-jdk -Xverify:none \\%{l_nil}"
      echo "    -cp %{l_prefix}/libexec/avis/avis-tools.jar:%{l_prefix}/libexec/avis/avis-client.jar \\%{l_nil}"
      echo "    org.avis.tools.Ec \${1+\"\$@\"}"
    ) >avis-ec
    ( echo "#!/bin/sh"
      echo "exec %{l_prefix}/bin/java -Vsun-jdk -Xverify:none \\%{l_nil}"
      echo "    -cp %{l_prefix}/libexec/avis/avis-tools.jar:%{l_prefix}/libexec/avis/avis-client.jar \\%{l_nil}"
      echo "    org.avis.tools.Ep \${1+\"\$@\"}"
    ) >avis-ep
    %{l_shtool} install -c -m 755 \
        avis-ec avis-ep $RPM_BUILD_ROOT%{l_prefix}/bin/

    #   install run-command script
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE rc.avis} $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/avis/avisd.config' \
        '%dir %attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/avis'

%files -f files

%clean

%post
    #   after upgrade, restart service
    [ $1 -eq 2 ] || exit 0
    eval `%{l_rc} avis status 2>/dev/null`
    [ ".$avis_active" = .yes ] && %{l_rc} avis restart
    exit 0

%preun
    #   before erase, stop service and remove log files
    [ $1 -eq 0 ] || exit 0
    %{l_rc} avis stop 2>/dev/null
    rm -f $RPM_INSTALL_PREFIX/var/avis/avis.log* >/dev/null 2>&1 || true
    exit 0

