##
##  jabberd.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_major 2.7
%define       V_minor 0

#   package information
Name:         jabberd
Summary:      Jabber Instant Messaging Daemon
URL:          http://jabberd2.org/
Vendor:       Jabber Software Foundation
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        PLUS
Group:        InstantMessaging
License:      JOSL/GPL
Version:      %{V_major}.%{V_minor}
Release:      20181204

#   package options
%option       with_sqlite no
%option       with_mysql  no
%option       with_pgsql  no
%option       with_pam    no

#   list of sources
Source0:      https://github.com/jabberd2/jabberd2/releases/download/jabberd-%{version}/jabberd-%{version}.tar.xz
Source1:      rc.jabberd
Patch0:       jabberd.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, make
PreReq:       OpenPKG, openpkg >= 20160101, perl
BuildPreReq:  db, expat, libiconv, libidn, gpg-error, gcrypt, libgsasl, openssl, udns
PreReq:       db, expat, libiconv, libidn, gpg-error, gcrypt, libgsasl, openssl, udns
%if "%{with_sqlite}" == "yes"
BuildPreReq:  sqlite
PreReq:       sqlite
%endif
%if "%{with_mysql}" == "yes"
BuildPreReq:  mysql
PreReq:       mysql
%endif
%if "%{with_pgsql}" == "yes"
BuildPreReq:  postgresql
PreReq:       postgresql
%endif
%if "%{with_pam}" == "yes"
BuildPreReq:  PAM
PreReq:       PAM
%endif

%description
    JabberD is the original server implementation for the Jabber instant
    messaging platform. JabberD 2 is the next generation of the JabberD
    server. It has been rewritten from the ground up to be scalable,
    architecturally sound, and to support the latest protocol extensions
    coming out of the JSF.

%track
    prog jabberd = {
        version   = %{version}
        url       = https://github.com/jabberd2/jabberd2/releases
        regex     = jabberd-(\d+\.\d+(\.\d+)*)\.tar\.gz
    }

%prep
    %setup -q -n jabberd-%{version}
    %{l_sed} <%{PATCH0} -e 's;@l_prefix@;%{l_prefix};g' | %{l_patch} -p0 -b
    %{l_shtool} subst \
        -e 's;exec perl;exec %{l_prefix}/bin/perl;' \
        tools/jabberd.in

%build
    #   configure program
    ( echo "ac_cv_func_kqueue=no"
    ) >config.cache
    export CC="%{l_cc}"
    export CFLAGS="%{l_cflags -O}"
    export CPPFLAGS="%{l_cppflags libidn}"
    export LDFLAGS="%{l_ldflags}"
    export LIBS=""
    case "%{l_platform -t}" in
        *-linux* ) LIBS="$LIBS -ldl"                ;;
        *-sunos* ) LIBS="$LIBS -lsocket -lnsl -lrt" ;;
    esac
    export JHOME=%{l_prefix}/var/jabberd
%if "%{with_mysql}" == "yes"
    CPPFLAGS="$CPPFLAGS %{l_cppflags mysql}"
    LDFLAGS="$LDFLAGS %{l_ldflags mysql}"
    LIBS="$LIBS -lz -lm"
%endif
%if "%{with_pgsql}" == "yes"
    CPPFLAGS="$CPPFLAGS %{l_cppflags postgresql}"
%endif
    ./configure \
        --cache-file=./config.cache \
        --prefix=%{l_prefix} \
        --sysconfdir=%{l_prefix}/etc/jabberd \
        --mandir=%{l_prefix}/man \
        --enable-db \
        --enable-fs \
        --enable-pipe \
        --enable-ssl \
        --with-sasl=gsasl \
%if "%{with_sqlite}" == "yes"
        --enable-sqlite \
%else
        --disable-sqlite \
%endif
%if "%{with_mysql}" == "yes"
        --enable-mysql \
%else
        --disable-mysql \
%endif
%if "%{with_pgsql}" == "yes"
        --enable-pgsql \
%else
        --disable-pgsql \
%endif
%if "%{with_pam}" == "yes"
        --enable-pam \
%else
        --disable-pam \
%endif
        --disable-oracle \
        --enable-static \
        --enable-shared

    #   build program
    %{l_make} %{l_mflags}

%install
    #   create installation filesystem structure
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d \
        $RPM_BUILD_ROOT%{l_prefix}/etc/jabberd \
        $RPM_BUILD_ROOT%{l_prefix}/etc/jabberd/templates \
        $RPM_BUILD_ROOT%{l_prefix}/var/jabberd \
        $RPM_BUILD_ROOT%{l_prefix}/var/jabberd/db \
        $RPM_BUILD_ROOT%{l_prefix}/var/jabberd/pid \
        $RPM_BUILD_ROOT%{l_prefix}/var/jabberd/log

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

    #   remove unnecessary files
    rm -f $RPM_BUILD_ROOT%{l_prefix}/etc/jabberd/*.dist
    rm -f $RPM_BUILD_ROOT%{l_prefix}/etc/jabberd/templates/*.dist
    rm -f $RPM_BUILD_ROOT%{l_prefix}/lib/jabberd/*.a

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

%files -f files

%clean

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

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

