##
##  spread.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_spread                      5.0.1
%define       V_spread_perl                 3.17.4.4
%define       V_spread_perl_client_constant 0.02
%define       V_spread_perl_client          0.03_02

#   package information
Name:         spread
Summary:      Group Communication Toolkit
URL:          http://www.spread.org/
Vendor:       Spread Concepts LLC & CNDS
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        PLUS
Group:        RPC
License:      BSD-style
Version:      %{V_spread}
Release:      20180516

#   package options
%option       with_perl   no
%option       with_java   no

#   list of sources
Source0:      http://www.spread.org/download/::/spread-src-%{V_spread}.tar.gz
Source1:      http://www.cpan.org/authors/id/J/JE/JESUS/Spread-%{V_spread_perl}.tar.gz
Source2:      http://www.cpan.org/authors/id/M/MB/MBAILEY/Spread-Client-Constant-%{V_spread_perl_client_constant}.tar.gz
Source3:      http://www.cpan.org/authors/id/M/MB/MBAILEY/Spread-Client-%{V_spread_perl_client}.tar.gz
Source4:      rc.spread
Patch0:       spread.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, gcc, flex, bison, sed, perl, groff
PreReq:       OpenPKG, openpkg >= 20160101
%if "%{with_perl}" == "yes"
BuildPreReq:  perl, perl-openpkg >= 5.8.3-20040126
PreReq:       perl, perl-openpkg >= 5.8.3-20040126
%endif
%if "%{with_java}" == "yes"
BuildPreReq:  java, ant
PreReq:       java
%endif

%description
    Spread is a group communication system that provides a number of
    messaging services to applications. These services can then be used
    by applications to make it easy to write full-featured reliable
    groupware, distributed servers, client-server applications, chat and
    news services, and many other networked enabled tools.

%track
    prog spread = {
        version   = %{V_spread}
        url       = http://www.spread.org/download.html
        regex     = spread-src-(\d+(.\d+)+)\.tar\.gz
    }
    prog spread:perl = {
        version   = %{V_spread_perl}
        url       = http://www.cpan.org/authors/id/J/JE/JESUS/
        regex     = Spread-(__VER__)\.tar\.gz
    }

%prep
    %setup -q -n spread-src-%{version}
%if "%{with_perl}" == "yes"
    %setup -q -n spread-src-%{version} -D -T -a 1
    %setup -q -n spread-src-%{version} -D -T -a 2
    %setup -q -n spread-src-%{version} -D -T -a 3
%endif
    %patch -p0

    #   adjust paths in toolkit
    %{l_shtool} subst \
        -e "s;^[# \t]*\(DaemonUser\).*;\1 = %{l_rusr};" \
        -e "s;^[# \t]*\(DaemonGroup\).*;\1 = %{l_mgrp};" \
        -e "s;^[# \t]*\(RuntimeDir\).*;\1 = %{l_prefix}/var/spread;" \
        -e "s;^[# \t]*\(EventLogFile\).*;\1 = %{l_prefix}/var/spread/spread.log;" \
        -e "s;^[ \t]*localhost[ \t]*127.0.0.1;\t`%{l_shtool} echo -e %h%d`;" \
        docs/sample.spread.conf
    %{l_shtool} subst \
        -e "s;/etc\(/spread.access_ip\);%{l_prefix}/etc/spread\1;g" \
        daemon/auth-ip.c
%if "%{with_perl}" == "yes"
    %{l_shtool} subst \
        -e 's;\(SPLIB_LIB.*\)\.\./spread-src-3.17.0/;\1../libspread;' \
        -e 's;\(SPLIB_INCLUDE.*\)\.\./spread-src-3.17.0/;\1../include;' \
        Spread-%{V_spread_perl}/Makefile.PL
    %{l_shtool} subst \
        -e 's;\(LIBS.*\[.\)\(.\);\1-L../libspread -lspread\2;' \
        -e 's;-I\.;-I../include;' \
        Spread-Client-Constant-%{V_spread_perl_client_constant}/Makefile.PL
%endif

%build
    #   configure toolkit
    %{l_shtool} subst \
        -e "s;if test ! -d \$piddir;if false;" \
        configure
    %{l_shtool} subst \
        -e "s;/usr/bin/perl;%{l_prefix}/bin/perl;g" \
        buildtools/fixpaths \
        buildtools/mdoc2man.pl
    CC="%{l_cc}" \
    CFLAGS="%{l_cflags -O}" \
    PERL="%{l_prefix}/bin/perl" \
    ./configure \
        --prefix=%{l_prefix} \
        --sysconfdir=%{l_prefix}/etc/spread \
        --with-pid-dir=%{l_prefix}/var/spread \
        --with-mantype=man \
        --disable-shared

    #   build toolkit
    %{l_make} %{l_mflags -O}

    #   build Perl extension
%if "%{with_perl}" == "yes"
    %{l_prefix}/bin/perl-openpkg -d Spread-%{V_spread_perl} prepare configure build
    %{l_prefix}/bin/perl-openpkg -d Spread-Client-Constant-%{V_spread_perl_client_constant} prepare configure build
    %{l_prefix}/bin/perl-openpkg -d Spread-Client-%{V_spread_perl_client} prepare configure build
%endif

    #   build Java extension
%if "%{with_java}" == "yes"
    ( cd java
      ant
    ) || exit $?
%endif

%install

    #   install Perl extension
%if "%{with_perl}" == "yes"
    %{l_prefix}/bin/perl-openpkg -d Spread-%{V_spread_perl} install
    %{l_prefix}/bin/perl-openpkg -d Spread-Client-Constant-%{V_spread_perl_client_constant} install
    %{l_prefix}/bin/perl-openpkg -d Spread-Client-%{V_spread_perl_client} install
    %{l_prefix}/bin/perl-openpkg -F perl-openpkg-files fixate cleanup
%else
    >perl-openpkg-files
%endif

    #   build Java extension
%if "%{with_java}" == "yes"
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/lib/java
    %{l_shtool} install -c -m 644 \
        java/dest/spread-*.jar \
        $RPM_BUILD_ROOT%{l_prefix}/lib/java/
%endif

    #   install toolkit
    %{l_make} %{l_mflags} install DESTDIR=$RPM_BUILD_ROOT

    #   strip down installation
    rm -f $RPM_BUILD_ROOT%{l_prefix}/lib/*.s[ol]* >/dev/null 2>&1 || true
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/share/doc >/dev/null 2>&1 || true

    #   create additional run-time directory
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var/spread

    #   install the IP address ACL file
    %{l_shtool} install -c -m 644 \
        docs/sample.spread.access_ip \
        $RPM_BUILD_ROOT%{l_prefix}/etc/spread/spread.access_ip

    #   install run command file
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE rc.spread} $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} `cat perl-openpkg-files` \
        "%config %{l_prefix}/etc/spread/*"

%files -f files

%clean

%pre
    #   before upgrade, save status and stop service
    [ $1 -eq 2 ] || exit 0
    eval `%{l_rc} spread status 2>/dev/null | tee %{l_tmpfile}`
    %{l_rc} spread stop 2>/dev/null
    exit 0

%post
    if [ $1 -eq 1 ]; then
        #   install bogus log file, to avoid first time run failure
        cp /dev/null $RPM_INSTALL_PREFIX/var/spread/spread.log
        chmod 644 $RPM_INSTALL_PREFIX/var/spread/spread.log
        chown %{l_rusr}:%{l_mgrp} $RPM_INSTALL_PREFIX/var/spread/spread.log
    fi
    if [ $1 -eq 2 ]; then
        #   after upgrade, restore status
        eval `cat %{l_tmpfile}`; rm -f %{l_tmpfile}
        [ ".$spread_active" = .yes ] && %{l_rc} spread start
    fi
    exit 0

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

