##
##  nntpcache.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 information
Name:         nntpcache
Summary:      NNTP Caching Proxy
URL:          http://www.nntpcache.com/
Vendor:       Julian Assange, Luke Bowker
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        BASE
Group:        News
License:      Commercial
Version:      3.0.2
Release:      20160305

#   package options
%option       with_fsl  yes

#   list of sources
Source0:      http://www.nntpcache.com/pub/nntpcache/nntpcache-%{version}.tar.gz
Source1:      rc.nntpcache
Source2:      fsl.nntpcache
Patch0:       nntpcache.patch

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

%description
    NNTPCache (efficiently) executes on the localhost pretending to
    be an NNRP news reading server. In fact, what it does is pass
    certain NNTP commands through to real (remote and possibly local)
    news-servers based on various pattern matching rules. NNTPCache
    then takes the output from those servers and caches & indexes it in
    funky ways (much case specific magic goes into this). The next time
    such information is asked for, or other information which can be
    logically inferred from the previously collated information, it is
    sent directly from the cache, without consulting the remote servers.

%track
    prog nntpcache = {
        disabled
        comment   = "site is gone"
        version   = %{version}
        url       = http://www.nntpcache.com/source.html
        regex     = nntpcache-(__VER__)\.tar\.gz
    }

%prep
    %setup -q
    %patch -p0

%build
    #   configure package
%if "%{with_fsl}" == "yes"
    %{l_shtool} subst \
        -e 's;\(openlog.*\)LOG_NEWS\(.*\);\1LOG_USER\2;' \
        src/nntpcache.c
%endif
    %{l_shtool} subst \
        -e 's;test -e;test -f;g' \
        Makefile* */Makefile*
    %{l_shtool} subst \
        -e 's;/etc/nntpcache/\(newshound\.conf\);%{l_prefix}/etc/nntpcache/\1;' \
        contrib/newshound/getconf.c
    %{l_shtool} subst \
        -e 's;^\(string adminEmail\) .*;\1 "%{l_musr}@example.com";' \
        -e 's;^\(bool httpServer\) .*;\1 no;' \
        cf/nnconf.cf.in
    %{l_shtool} subst \
        -e 's;$libexecDir/nntpcache-gdb\.sh;true;' \
        configure
    ( echo "nc_cv_UID=%{l_musr}"
      echo "nc_cv_GID=%{l_rgrp}"
    ) >config.cache
    CC="%{l_cc}" \
    CFLAGS="%{l_cflags -O}" \
    LDFLAGS="%{l_fsl_ldflags}" \
    LIBS="%{l_fsl_libs}" \
    ./configure \
        --cache-file=./config.cache \
        --prefix=%{l_prefix} \
        --sysconfdir=%{l_prefix}/etc \
        --libexecdir=%{l_prefix}/libexec \
        --localstatedir=%{l_prefix}/var

    #   build package
    %{l_make} %{l_mflags}

%install

    #   install package
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var
    %{l_shtool} subst \
        -e "s;chown;true;g" -e "s;chgrp;true;g" \
        `find . -type f -name Makefile -print`
    %{l_make} %{l_mflags} install \
        prefix=$RPM_BUILD_ROOT%{l_prefix} \
        exec_prefix=$RPM_BUILD_ROOT%{l_prefix} \
        sysconfdir=$RPM_BUILD_ROOT%{l_prefix}/etc/nntpcache \
        libexecdir=$RPM_BUILD_ROOT%{l_prefix}/libexec/nntpcache \
        configDir=$RPM_BUILD_ROOT%{l_prefix}/etc/nntpcache \
        localstatedir=$RPM_BUILD_ROOT%{l_prefix}/var/nntpcache \
        cacheDir=$RPM_BUILD_ROOT%{l_prefix}/var/nntpcache

    #   strip down and post-adjust installation hierarchy
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/include
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/etc/nntpcache/http
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/bin
    rm -f $RPM_BUILD_ROOT%{l_prefix}/sbin/innreport.sh
    rm -f $RPM_BUILD_ROOT%{l_prefix}/sbin/newshound.pl
    rm -f $RPM_BUILD_ROOT%{l_prefix}/etc/nntpcache/pubring*
    rm -f $RPM_BUILD_ROOT%{l_prefix}/etc/nntpcache/innreport*
    rm -f $RPM_BUILD_ROOT%{l_prefix}/libexec/nntpcache/nntpcache-gdb.sh
    rm -f $RPM_BUILD_ROOT%{l_prefix}/libexec/nntpcache/nntpcache.gdb
    rm -f $RPM_BUILD_ROOT%{l_prefix}/libexec/nntpcache/nntpcached.debug
    strip $RPM_BUILD_ROOT%{l_prefix}/sbin/* >/dev/null 2>&1 || true
    find $RPM_BUILD_ROOT%{l_prefix} -type d -print | xargs chmod 755
    find $RPM_BUILD_ROOT%{l_prefix} -type f -print | grep -v /sbin | xargs chmod 644
    find $RPM_BUILD_ROOT%{l_prefix} -type f -name VERSION -print | xargs rm -f
    ( cd $RPM_BUILD_ROOT%{l_prefix}/etc/nntpcache
      for file in *-dist; do
          mv $file `echo $file | sed -e 's;-dist$;;'`
      done
    ) || exit $?

    #   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.nntpcache} $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.nntpcache} \
        $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/nntpcache/*' \
        '%config %{l_prefix}/etc/fsl/fsl.nntpcache'

%files -f files

%clean

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

%preun
    #   before erase, stop service
    [ $1 -eq 0 ] || exit 0
    %{l_rc} nntpcache stop 2>/dev/null
    exit 0

