##
##  cvs.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.
##

#   the involved versions
%define       V_cvs       1.12.13
%define       V_cvslock   0.2

#   package information
Name:         cvs
Summary:      Concurrent Versions Systems (CVS)
URL:          http://www.nongnu.org/cvs/
Vendor:       B. Berliner, D.D. Zuhn, J. Polk
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        BASE
Group:        SCM
License:      GPL
Version:      %{V_cvs}
Release:      20160305

#   package options
%option       with_fsl         yes
%option       with_rse         yes
%option       with_rse_cvsuser cvs

#   list of sources
Source0:      ftp://ftp.gnu.org/non-gnu/cvs/source/feature/%{V_cvs}/cvs-%{V_cvs}.tar.bz2
Source1:      http://www.does-not-exist.org/roessler/cvslock-%{V_cvslock}.tar.gz
Source2:      rc.cvs
Source3:      fsl.cvs
Patch0:       cvs.patch
Patch1:       cvs.patch.rse

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, perl, patch, zlib
PreReq:       OpenPKG, openpkg >= 20160101, perl, patch, zlib
%if "%{with_fsl}" == "yes"
BuildPreReq:  fsl
PreReq:       fsl
%endif

%description
    CVS is a version control system, which allows you to keep old
    versions of files (usually source code), keep a log of who,
    when, and why changes occurred, etc., like RCS or SCCS. Unlike
    the simpler systems, CVS does not just operate on one file at a
    time or one directory at a time, but operates on hierarchical
    collections of directories consisting of version controlled files.
    CVS helps to manage releases and to control the concurrent editing
    of source files among multiple authors. CVS allows triggers to
    enable/log/control various operations and works well over a wide
    area network. CVS keeps a single copy of the master sources. This
    copy is called the source repository.
%if "%{with_rse}" == "yes"
    This version of CVS has the large set of patches applied from
    Ralf S. Engelschall <rse@engelschall.com> (-DRSE_PATCHES). This
    means that (still undocumented) extra functionality is available.
%endif

%track
    prog cvs = {
        version   = %{version}
        url       = ftp://ftp.gnu.org/non-gnu/cvs/source/feature/
        regex     = (\d+\.\d+\.\d+)
    }
    prog cvs:cvslock = {
        version   = %{V_cvslock}
        url       = http://www.does-not-exist.org/roessler/bits.html
        regex     = cvslock-(__VER__)\.tar\.gz
    }

%prep
    %setup -q
    %setup -q -T -D -a 1

    #   apply patches
%if "%{with_rse}" == "yes"
    cat %{PATCH cvs.patch.rse} |\
    sed -e 's;\(#define RSE_PATCH_CVSUSER_CALLER "\)cvs\("\);\1%{with_rse_cvsuser}\2;' |\
    %{l_patch} -p0
%endif
    %patch -p0

%build
    #   configure CVS package
    rm -f config.cache
    ( echo "ac_cv_header_getopt_h=no"
      echo "ac_cv_func_getopt_long_only=no"
      echo "ac_cv_func_getline=no"
      echo "ac_cv_func_getdelim=no"
    ) >config.cache
    CC="%{l_cc}" \
%if "%{with_rse}" == "yes"
    CFLAGS="%{l_cflags -O} -DRSE_PATCHES" \
%else
    CFLAGS="%{l_cflags -O}" \
%endif
    LDFLAGS="%{l_fsl_ldflags}" \
    LIBS="%{l_fsl_libs}" \
    ./configure \
        --cache-file=./config.cache \
        --prefix=%{l_prefix} \
        --with-patch=%{l_prefix}/bin/patch \
        --with-tmpdir=%{l_prefix}/var/cvs/tmp \
        --with-external-zlib=%{l_prefix} \
        --without-krb4 \
        --without-gssapi \
        --enable-encryption \
        --enable-server \
        --disable-nls
    rm -rf zlib
    %{l_make} %{l_mflags}

    #   configure CVSlock utility
    ( cd cvslock-%{V_cvslock}
      CC="%{l_cc}" \
      CFLAGS="%{l_cflags -O}" \
      ./configure \
          --prefix=%{l_prefix}
      %{l_make} %{l_mflags}
    ) || exit $?

%install

    #   install CVS package
    %{l_make} %{l_mflags} install AM_MAKEFLAGS="DESTDIR=$RPM_BUILD_ROOT"
    rm -f $RPM_BUILD_ROOT%{l_prefix}/bin/rcs2log
    rm -f $RPM_BUILD_ROOT%{l_prefix}/info/dir
    mv $RPM_BUILD_ROOT%{l_prefix}/share/cvs/contrib/* \
       $RPM_BUILD_ROOT%{l_prefix}/share/cvs/
    rmdir $RPM_BUILD_ROOT%{l_prefix}/share/cvs/contrib
    rm -f $RPM_BUILD_ROOT%{l_prefix}/bin/cvsbug
    rm -f $RPM_BUILD_ROOT%{l_prefix}/man/man8/cvsbug.8

    #   install CVSlock package
    ( cd cvslock-%{V_cvslock}
      %{l_make} %{l_mflags} install AM_MAKEFLAGS="DESTDIR=$RPM_BUILD_ROOT"
    ) || exit $?
    strip $RPM_BUILD_ROOT%{l_prefix}/bin/* >/dev/null 2>&1 || true
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var/cvs/tmp

    #   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.cvs} $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.cvs} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/fsl/

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %{l_prefix}/etc/fsl/fsl.cvs' \
        '%attr(1777,%{l_musr},%{l_mgrp}) %{l_prefix}/var/cvs/tmp'

%files -f files

%clean

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

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

