##
##  flowtools.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 versions
%define       V_flowtools   0.68
%define       V_flowextract 2.61
%define       V_cflow       1.053

#   package information
Name:         flowtools
Summary:      NetFlow Tools
URL:          http://www.splintered.net/sw/flow-tools/
Vendor:       Mark Fullmer
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        BASE
Group:        Auditing
License:      BSD
Version:      %{V_flowtools}
Release:      20080101

#   package options
%option       with_fsl    yes
%option       with_perl   yes
%option       with_mysql  no
%option       with_pgsql  no
%option       with_crypto no

#   list of sources
Source0:      ftp://ftp.eng.oar.net/pub/flow-tools/flow-tools-%{V_flowtools}.tar.gz
Source1:      http://security.uchicago.edu/tools/net-forensics/flow-extract-%{V_flowextract}.tar.gz
Source2:      http://net.doit.wisc.edu/~plonka/Cflow/Cflow-%{V_cflow}.tar.gz
Source3:      rc.flowtools
Source4:      fsl.flowtools
Patch0:       flowtools.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, flex, bison
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  zlib
PreReq:       zlib
%if "%{with_fsl}" == "yes"
BuildPreReq:  fsl
PreReq:       fsl
%endif
%if "%{with_perl}" == "yes"
BuildPreReq:  perl, perl-openpkg
PreReq:       perl
%endif
%if "%{with_mysql}" == "yes"
BuildPreReq:  mysql
PreReq:       mysql
%endif
%if "%{with_pgsql}" == "yes"
BuildPreReq:  postgresql
PreReq:       postgresql
%endif
%if "%{with_crypto}" == "yes"
BuildPreReq:  openssl
PreReq:       openssl
%endif

%description
    Flow-Tools is a software toolkit for collecting and processing
    NetFlow data. Flow-Tools is library and a collection of programs
    used to collect, send, process, and generate reports from NetFlow
    data. The tools can be used together on a single server or
    distributed to multiple servers for large deployments. The
    flow-toools library provides an API for development of custom
    applications for NetFlow export versions 1,5,6 and the 14 currently
    defined version 8 subversions. A Perl API is included, too.

%track
    prog flowtools = {
        version   = %{V_flowtools}
        url       = ftp://ftp.eng.oar.net/pub/flow-tools/
        regex     = flow-tools-(\d\.\d\d)\.tar\.gz
    }
    prog flowtools:flowextract = {
        version   = %{V_flowextract}
        url       = http://security.uchicago.edu/tools/net-forensics/
        regex     = flow-extract-(__VER__)\.tar\.gz
    }
    prog flowtools:Cflow = {
        version   = %{V_cflow}
        url       = http://net.doit.wisc.edu/~plonka/Cflow/
        regex     = Cflow-(__VER__)\.tar\.gz
    }

%prep
    %setup -q -c
    %setup -q -D -T -a 1
    %setup -q -D -T -a 2
    %patch -p0

%build
    #   build flow-tools
    ( cd flow-tools-%{V_flowtools}
      %{l_shtool} subst \
          -e 's;-g -Wall;;' \
          Makefile.in */Makefile.in
      %{l_shtool} subst \
          -e 's|@localstatedir@/sym/|%{l_prefix}/share/flowtools/|' \
          -e 's|@localstatedir@/cfg/|%{l_prefix}/etc/flowtools/|' \
          docs/flow-*.1.in
      export CC="%{l_cc}"
      export CFLAGS="%{l_cflags -O}"
      export CPPFLAGS="%{l_cppflags}"
      export LDFLAGS="-L../lib %{l_ldflags} %{l_fsl_ldflags}"
      export LIBS="%{l_fsl_libs}"
%if "%{with_mysql}" == "yes"
      LDFLAGS="$LDFLAGS %{l_ldflags mysql}"
      LIBS="$LIBS -lssl -lcrypto -lm"
%endif
%if "%{with_pgsql}" == "yes"
      LIBS="$LIBS -lssl -lcrypto -lcrypt"
%endif
      ./configure \
%if "%{with_mysql}" == "yes"
          --with-mysql=%{l_prefix} \
%endif
%if "%{with_pgsql}" == "yes"
          --with-pgsql=%{l_prefix} \
%endif
%if "%{with_crypto}" == "yes"
          --with-openssl=%{l_prefix} \
%endif
          --prefix=%{l_prefix} \
          --sysconfdir=%{l_prefix}/etc/flowtools \
          --includedir=%{l_prefix}/include/flowtools \
          --localstatedir=%{l_prefix}/var/flowtools \
          --datadir=%{l_prefix}/share/flowtools
      %{l_make} %{l_mflags -O}
    ) || exit $?

    #   build flow-extract addon
    ( cd flow-extract
      INCLUDES="-I../flow-tools-%{V_flowtools}/lib %{l_cppflags}"
      LIBS="-L../flow-tools-%{V_flowtools}/lib %{l_ldflags} -lft -lz"
      case "%{l_platform -t}" in
          *-sunos* ) LIBS="$LIBS -lsocket -lnsl" ;;
      esac
      %{l_make} %{l_mflags} \
          CC="%{l_cc}" \
          FLAGS="%{l_cflags}" \
          INCLUDES="$INCLUDES" \
          LIBS="$LIBS" \
          YACC="bison -y"
    ) || exit $?

%if "%{with_perl}" == "yes"
    #   build Perl API
    %{l_prefix}/bin/perl-openpkg prepare
    %{l_prefix}/bin/perl-openpkg \
        -d Cflow-%{V_cflow} \
        -A "INC='-DOSU -I../flow-tools-%{V_flowtools}/lib'" \
        -A "LIBS='-L../flow-tools-%{V_flowtools}/lib %{l_ldflags} -lft -lz'" \
        configure build
%endif

%install

    #   install flow-tools
    ( cd flow-tools-%{version}
      %{l_make} %{l_mflags} install AM_MAKEFLAGS="DESTDIR=$RPM_BUILD_ROOT"
    ) || exit $?

    #   install flow-extract addon
    ( cd flow-extract
      %{l_shtool} install -c -s -m 755 \
          flow-extract $RPM_BUILD_ROOT%{l_prefix}/bin/
      %{l_shtool} install -c -m 644 \
          flow-extract.1 $RPM_BUILD_ROOT%{l_prefix}/man/man1/
    ) || exit $?

    #   install Perl API
%if "%{with_perl}" == "yes"
    %{l_prefix}/bin/perl-openpkg -d Cflow-%{V_cflow} install
    %{l_prefix}/bin/perl-openpkg -F perl-openpkg-files fixate cleanup
    ( cd Cflow-%{V_cflow}
      %{l_prefix}/bin/pod2man flowdumper >flowdumper.1
      %{l_shtool} install -c -m 644 \
          flowdumper.1 $RPM_BUILD_ROOT%{l_prefix}/man/man1/
    ) || exit $?
%else
    >perl-openpkg-files
%endif

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

    #   strip down installation
    strip $RPM_BUILD_ROOT%{l_prefix}/bin/* >/dev/null 2>&1 || true

    #   create additional directories
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var/flowtools/db

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} `cat perl-openpkg-files` \
        '%config %{l_prefix}/etc/fsl/fsl.flowtools' \
        '%config %{l_prefix}/etc/flowtools/*.cfg' \
        '%dir %attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/flowtools/db' \
        '%dir %attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/flowtools'

%files -f files

%clean

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

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

