##
##  subversion.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_dist      1.14.5
%define       V_opkg      1.14.5

#   package information
Name:         subversion
Summary:      Subversion Source Revision Control System
URL:          http://subversion.apache.org/
Vendor:       Apache Software Foundation
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        BASE
Group:        SCM
License:      Apache/BSD
Version:      %{V_opkg}
Release:      20241209

#   package options
%option       with_fsl      yes
%option       with_db       no
%option       with_sasl     no

#   list of sources
Source0:      http://www.apache.org/dist/subversion/subversion-%{V_dist}.tar.bz2
Source1:      subversion.config
Source2:      subversion.servers
Source3:      rc.subversion
Source4:      fsl.subversion
Patch0:       subversion.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, make, libtool, sed, pkgconfig
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  diffutils, zlib, libiconv, expat, serf, apr, sqlite, libutf8proc, lz4
PreReq:       diffutils, zlib, libiconv, expat, serf, apr, sqlite, libutf8proc, lz4
%if "%{with_fsl}" == "yes"
BuildPreReq:  fsl
PreReq:       fsl
%endif
%if "%{with_db}" == "yes"
BuildPreReq:  db
PreReq:       db
%endif
%if "%{with_sasl}" == "yes"
BuildPreReq:  sasl
PreReq:       sasl
%endif

%description
    Subversion is a modern Version Control System (VCS) providing most
    current CVS features, versioned directories/renames/meta-data,
    atomic commits, cheap branching and tagging, native client/server
    architecture, and the choice of database or plain-file repository
    implementations.

%track
    prog subversion = {
        version   = %{V_dist}
        url       = http://subversion.apache.org/download/
        regex     = subversion-(\d+\.\d+.\d+)\.tar\.bz2
    }

%prep
    %setup -q
    %patch -p0

    #   patch: correctly pass --disable-shared to sub-directories
    %{l_shtool} subst \
        -e 's;\($0. $ac_configure_args\);\1 --disable-shared;' \
        configure

    #   patch: adjust path to configuration directory
    %{l_shtool} subst \
        -e 's;/etc/subversion;%{l_prefix}/etc/subversion;g' \
        subversion/libsvn_subr/config_impl.h \
        subversion/libsvn_subr/config_file.c

%build
    #   configure package
    CC="%{l_cc}" \
    CFLAGS="%{l_cflags -O}" \
    CPPFLAGS="%{l_cppflags libxml2 openssl10 .} -DSQLITE_THREADSAFE=1" \
%if "%{with_sasl}" == "yes" && "%{with_db}" == "yes"
    LDFLAGS="%{l_ldflags openssl10 .} `pkg-config serf-1 db sasl --libs-only-L` %{l_fsl_ldflags}" \
    LIBS="`pkg-config serf-1 db sasl --libs-only-l` -lz %{l_fsl_libs} -lm" \
%else
%if "%{with_sasl}" == "yes"
    LDFLAGS="%{l_ldflags openssl10 .} `pkg-config serf-1 sasl --libs-only-L` %{l_fsl_ldflags}" \
    LIBS="`pkg-config serf-1 sasl --libs-only-l` `pkg-config apr-util-1 --libs-only-l` -lz %{l_fsl_libs} -lm" \
%else
    LDFLAGS="%{l_ldflags openssl10 .} %{l_fsl_ldflags}" \
    LIBS="`pkg-config serf-1 apr-util-1 --libs-only-l` -lz %{l_fsl_libs} -lm" \
%endif
%endif
    CONFIG_SHELL="%{l_bash}" \
    ./configure \
        --prefix=%{l_prefix} \
        --mandir=%{l_prefix}/man \
%if "%{with_db}" == "yes"
        --with-berkeley-db="db.h:%{l_prefix}/include:%{l_prefix}/lib:db" \
%else
        --without-berkeley-db \
%endif
        --with-sqlite=%{l_prefix} \
        --with-zlib=%{l_prefix} \
        --with-apr=%{l_prefix} \
        --with-apr-util=%{l_prefix} \
        --with-serf=%{l_prefix} \
%if "%{with_sasl}" == "yes"
        --with-sasl \
%else
        --without-sasl \
%endif
        --without-apxs \
        --disable-nls \
        --disable-shared \
        --enable-static

    #   build package
    %{l_make} %{l_mflags}

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

    #   strip down installation
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/lib
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/include
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/share/doc
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/share/pkgconfig
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/build-1
    strip  $RPM_BUILD_ROOT%{l_prefix}/bin/* >/dev/null 2>&1 || true

    #   install Subversion default global configuration files
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/subversion
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE subversion.config} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/subversion/config
    %{l_shtool} install -c -m 644 \
        %{SOURCE subversion.servers} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/subversion/servers

    #   install Subversion Bash command completion
    %{l_shtool} install -c -m 644 \
        tools/client-side/bash_completion \
        $RPM_BUILD_ROOT%{l_prefix}/etc/subversion/bashrc

    #   install run-command script
    %{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.subversion} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/

    #   install OSSP fsl configuration
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/fsl
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE fsl.subversion} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/fsl/

    #   create directory for default repository and svnserve pidfile
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var/subversion

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

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   on intial installation, create default repository
        if [ ! -d $RPM_INSTALL_PREFIX/var/subversion/default ]; then
            su - %{l_rusr} -c \
                "$RPM_INSTALL_PREFIX/bin/svnadmin create \
                    $RPM_INSTALL_PREFIX/var/subversion/default"
        fi
    elif [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} subversion status 2>/dev/null`
        [ ".$subversion_active" = .yes ] && %{l_rc} subversion restart
    fi
    exit 0

%preun
    if [ $1 -eq 0 ]; then
        #   before erase, stop service
        %{l_rc} subversion stop 2>/dev/null
    fi
    exit 0

