##
##  inn.spec -- OpenPKG RPM Specification
##  Copyright (c) 2000-2003 Cable & Wireless Deutschland GmbH
##  Copyright (c) 2000-2003 The OpenPKG Project <http://www.openpkg.org/>
##  Copyright (c) 2000-2003 Ralf S. Engelschall <rse@engelschall.com>
##
##  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 versions
%define       V_inn        2.3.4
%define       V_cleanfeed  20010805

#   package information
Name:         inn
Summary:      InterNetNews Usenet Server
URL:          http://www.isc.org/products/INN/
Vendor:       Internet Software Consortium
Packager:     The OpenPKG Project
Distribution: OpenPKG [BASE]
Group:        News
License:      ISC
Version:      %{V_inn}
Release:      20030101

#   list of sources
Source0:      ftp://ftp.isc.org/isc/inn/inn-%{V_inn}.tar.gz
Source1:      http://www.bofh.it/~md/cleanfeed/cleanfeed-%{V_cleanfeed}.tgz
Source2:      rc.inn
Source3:      fsl.inn
Patch0:       inn.patch

#   build information
Prefix:       %{l_prefix}
BuildRoot:    %{l_buildroot}
BuildPreReq:  OpenPKG, openpkg >= 20020206, fsl, make, perl, db, openssl, bison, flex
PreReq:       OpenPKG, openpkg >= 20020206, fsl, perl, perl-crypto, MTA
AutoReq:      no
AutoReqProv:  no

%description
    InterNetNews (INN) is a complete Usenet system. It includes innd, an
    NNTP server, and nnrpd, a newsreading server. INN separates hosts
    that feed you news from those that have users reading news.

%prep
    %setup0 -q -c
    %setup1 -q -T -D -a 1
    %patch0 -p0

%build
    #   build inn
    cd inn-%{V_inn}

    #   adjust some too strange INN paths
    %{l_shtool} subst \
        -e "s;^\\(PATHBIN.*=\\).*;\\1 \$(PATHNEWS)/libexec/inn;" \
        Makefile.global.in
    %{l_shtool} subst \
        -e "s;^\\(pathbin:.*\\)@prefix@.*;\\1@prefix@/libexec/inn;" \
        samples/inn.conf.in
    %{l_shtool} subst \
        -e 's;\(ExtUtils::Embed.*ldopts.*tail -1\);\1 | sed -e "s/ -lc/ /";' \
        configure

    #   configure the source tree
    CC="%{l_cc}" \
    CFLAGS="%{l_cflags -O} -I%{l_prefix}/include" \
    LDFLAGS="`%{l_prefix}/bin/fsl-config --ldflags --all`" \
    LIBS="`%{l_prefix}/bin/fsl-config --libs --all`" \
    ./configure \
        --prefix=%{l_prefix} \
        --bindir=%{l_prefix}/libexec/inn \
        --with-etc-dir=%{l_prefix}/etc/inn \
        --with-db-dir=%{l_prefix}/var/inn/db \
        --with-control-dir=%{l_prefix}/libexec/inn/control \
        --with-filter-dir=%{l_prefix}/libexec/inn/filter \
        --with-lib-dir=%{l_prefix}/lib/inn \
        --with-log-dir=%{l_prefix}/var/inn/log \
        --with-run-dir=%{l_prefix}/var/inn/run \
        --with-spool-dir=%{l_prefix}/var/inn/spool \
        --with-tmp-path=%{l_prefix}/var/inn/tmp \
        --with-openssl=%{l_prefix} \
        --with-berkeleydb=%{l_prefix} \
        --with-news-user=%{l_musr} \
        --with-news-group=%{l_musr} \
        --with-news-master=%{l_musr} \
        --with-sendmail=%{l_prefix}/sbin/sendmail \
        --enable-uucp-rnews \
        --with-perl

    #   build the INN system
    %{l_make} %{l_mflags}

%install
    rm -rf $RPM_BUILD_ROOT

    #   install INN
    ( cd inn-%{V_inn}

      #   determine current user and group
      thisuser=`%{l_shtool} echo -e '%u'`
      thisgroup=`%{l_shtool} echo -e '%g'`
      if [ ".$thisgroup" = . ]; then
          thisgroup="$thisuser"
      fi

      #   adjust INN installation procedure
      %{l_shtool} subst \
          -e "s;^\\(prefix.*=\\).*;\\1 $RPM_BUILD_ROOT%{l_prefix};" \
          -e "s;^\\(PATH.*=[    ]*\\)\\(%{l_prefix}\\);\\1 $RPM_BUILD_ROOT\\2;" \
          -e "s;^\\(NEWSUSER.*=\\).*;\\1 $thisuser;" \
          -e "s;^\\(NEWSGROUP.*=\\).*;\\1 $thisgroup;" \
          -e "s;^\\(OWNER.*=\\).*;\\1;" \
          -e "s;^\\(ROWNER.*=\\).*;\\1;" \
          Makefile.global

      #   perform installation procedure
      %{l_make} %{l_mflags} install

      #   make some programs manually accessible
      %{l_shtool} mkdir -f -p -m 755 $RPM_BUILD_ROOT%{l_prefix}/bin/
      for bin in controlbatch controlchan news2mail startinnfeed overchan \
          archive innmail innconfval ctlinnd inews rnews nntpsend; do
          ln $RPM_BUILD_ROOT%{l_prefix}/libexec/inn/$bin \
             $RPM_BUILD_ROOT%{l_prefix}/bin/$bin
      done

      #   strip installation
      rm -f $RPM_BUILD_ROOT%{l_prefix}/libexec/inn/filter/*.py
      rm -f $RPM_BUILD_ROOT%{l_prefix}/lib/inn/*.tcl
      rm -f $RPM_BUILD_ROOT%{l_prefix}/lib/inn/lib*.a
      rm -f $RPM_BUILD_ROOT%{l_prefix}/libexec/inn/ovdb_upgrade
      ln $RPM_BUILD_ROOT%{l_prefix}/libexec/inn/ovdb_recover \
         $RPM_BUILD_ROOT%{l_prefix}/libexec/inn/ovdb_upgrade
      strip $RPM_BUILD_ROOT%{l_prefix}/bin/* >/dev/null 2>&1 || true

      #   adjust configuration
      ( echo ''
        echo '# dummy entry to make INN happy as long as'
        echo '# this server has no real newsfeeds configured.'
        echo 'dummy-feed:!*::'
        echo ''
      ) >>$RPM_BUILD_ROOT%{l_prefix}/etc/inn/newsfeeds
      ( echo ''
        echo '# use the traditional spool area for storage'
        echo 'method tradspool {'
        echo '       newsgroups: *'
        echo '       class: 1'
        echo '}'
      ) >>$RPM_BUILD_ROOT%{l_prefix}/etc/inn/storage.conf

      #   create initial history database
      ( cd $RPM_BUILD_ROOT%{l_prefix}/var/inn/db
        touch history
        INNCONF=$RPM_BUILD_ROOT%{l_prefix}/etc/inn/inn.conf \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/inn/makedbz -i -f ./history
        chmod 644 history*
      )

      #   adjust permissions
      chmod 664 $RPM_BUILD_ROOT%{l_prefix}/var/inn/db/active

      #   create empty logfiles
      touch $RPM_BUILD_ROOT%{l_prefix}/var/inn/log/news.log

      #   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 \
          -e 's;@l_prefix@;%{l_prefix};g' \
          -e 's;@l_musr@;%{l_musr};g' \
          -e 's;@l_mgrp@;%{l_mgrp};g' \
          %{SOURCE rc.inn} \
          $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/

      #   OSSP fsl support
      %{l_shtool} mkdir -f -p -m 755 $RPM_BUILD_ROOT%{l_prefix}/etc/fsl
      %{l_shtool} install -c -m 644 \
          -e 's;@l_prefix@;%{l_prefix};g' \
          %{SOURCE fsl.inn} \
          $RPM_BUILD_ROOT%{l_prefix}/etc/fsl/
    )

    #   install cleanfeed
    ( cd cleanfeed-%{V_cleanfeed}
      %{l_shtool} subst \
          -e 's;^\($config_dir.*=\).*;\1 "%{l_prefix}/etc/inn"\;;' \
          cleanfeed
      ( echo "#!%{l_prefix}/bin/perl"
        cat cleanfeed
      ) >filter_innd.pl

      #   FIXME: provide a suitable default config
      cp cleanfeed.local.sample cleanfeed.local

      %{l_shtool} install -c -m 755 \
          filter_innd.pl \
          $RPM_BUILD_ROOT%{l_prefix}/libexec/inn/filter/
      %{l_shtool} install -c -m 644 \
          cleanfeed.local \
          $RPM_BUILD_ROOT%{l_prefix}/etc/inn/
    )

    #   strip binaries
    strip $RPM_BUILD_ROOT%{l_prefix}/bin/*         2> /dev/null || true
    strip $RPM_BUILD_ROOT%{l_prefix}/libexec/inn/* 2> /dev/null || true

    #   determine file list
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %{l_prefix}/etc/inn/*' \
        '%config %{l_prefix}/etc/fsl/fsl.inn' \
        '%not %dir %{l_prefix}/etc/fsl' \
        '%attr(4550,root,%{l_mgrp}) %{l_prefix}/bin/rnews' \
        '%attr(4755,root,%{l_mgrp}) %{l_prefix}/libexec/inn/inndstart' \
        '%attr(4755,root,%{l_mgrp}) %{l_prefix}/libexec/inn/startinnfeed' \
        '%config %{l_prefix}/var/inn/db/*'

%files -f files

%clean
    rm -rf $RPM_BUILD_ROOT

