##
##  flowscan.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_flowscan     1.006
%define       V_cuflow       1.7
%define       V_configreader 0.5

#   package information
Name:         flowscan
Summary:      NetFlow Scanning and Graphing Frontend
URL:          http://net.doit.wisc.edu/~plonka/FlowScan/
Vendor:       Dave Plonka
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Auditing
License:      GPL
Version:      %{V_flowscan}
Release:      20080101

#   list of sources
Source0:      http://net.doit.wisc.edu/~plonka/FlowScan/FlowScan-%{V_flowscan}.tar.gz
Source1:      http://www.columbia.edu/acis/networks/advanced/CUFlow/CUFlow-%{V_cuflow}.tgz
Source2:      http://www.cpan.org/modules/by-module/ConfigReader/ConfigReader-%{V_configreader}.tar.gz
Source3:      rc.flowscan
Source4:      flowscan-apache.conf
Source5:      flowscan-cuflow.cf
Source6:      flowscan-queue.sh
Source7:      flowscan.cf
Patch0:       flowscan.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, perl-openpkg
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  perl, perl-net, perl-xml, perl-sys, perl-crypto, perl-www
PreReq:       perl, perl-net, perl-xml, perl-sys, perl-crypto, perl-www
BuildPreReq:  rrdtool, flowtools, flowtools::with_perl = yes
PreReq:       rrdtool, flowtools, flowtools::with_perl = yes

%description
    FlowScan analyzes and reports on Internet Protocol (IP) flow data
    exported by network components. FlowScan binds together a NetFlow
    collection engine, a high performance database, and a visualization
    tool. FlowScan produces graph images that provide a continuous, near
    real-time view of the network traffic.

%track
    prog flowscan = {
        version   = %{V_flowscan}
        url       = http://net.doit.wisc.edu/~plonka/FlowScan/
        regex     = FlowScan-(__VER__)\.tar\.gz
    }
    prog flowscan:cuflow = {
        version   = %{V_cuflow}
        url       = http://www.columbia.edu/acis/networks/advanced/CUFlow/
        regex     = CUFlow-(__VER__)\.tgz
    }
    prog flowscan:configreader = {
        version   = %{V_configreader}
        url       = http://www.cpan.org/modules/by-module/ConfigReader/
        regex     = ConfigReader-(__VER__)\.tar\.gz
    }

%prep
    %setup -q -c
    %setup -q -D -T -a 1
    %setup -q -D -T -a 2
    %patch -p0 -d FlowScan-%{V_flowscan}

%build

%install
    #   create installation hierarchy
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/bin \
        $RPM_BUILD_ROOT%{l_prefix}/cgi \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d \
        $RPM_BUILD_ROOT%{l_prefix}/etc/flowscan \
        $RPM_BUILD_ROOT%{l_prefix}/lib/flowscan/ConfigReader \
        $RPM_BUILD_ROOT%{l_prefix}/var/flowscan/queue \
        $RPM_BUILD_ROOT%{l_prefix}/var/flowscan/cuflow/rrd \
        $RPM_BUILD_ROOT%{l_prefix}/var/flowscan/cuflow/web \
        $RPM_BUILD_ROOT%{l_prefix}/man/man1

    #   install ConfigReader module
    ( cd ConfigReader-%{V_configreader}
      %{l_shtool} install -c -m 644 \
          ConfigReader.pod $RPM_BUILD_ROOT%{l_prefix}/lib/flowscan/
      %{l_shtool} install -c -m 644 \
          DirectiveStyle.pm Spec.pm Values.pm \
          $RPM_BUILD_ROOT%{l_prefix}/lib/flowscan/ConfigReader/
    ) || exit $?

    #   install FlowScan components
    ( cd FlowScan-%{V_flowscan}
      perllib=$RPM_BUILD_ROOT%{l_prefix}/lib/flowscan \
      ./configure --prefix=%{l_prefix}
      %{l_shtool} install -c -m 755 \
          -e 's;use lib $FindBin::Bin;use lib "%{l_prefix}/lib/flowscan";' \
          -e 's;..FindBin::Bin./..FindBin::Script.\.cf;%{l_prefix}/etc/flowscan/flowscan.cf;' \
          flowscan $RPM_BUILD_ROOT%{l_prefix}/bin/
      %{l_shtool} install -c -m 755 \
          util/ip2hostname $RPM_BUILD_ROOT%{l_prefix}/bin/
      %{l_shtool} install -c -m 644 \
          FlowScan.pm $RPM_BUILD_ROOT%{l_prefix}/lib/flowscan/
      %{l_shtool} install -c -m 644 \
          -e 's;..FindBin::Bin./CampusIO\.cf;%{l_prefix}/etc/flowscan/flowscan-campusio.cf;' \
          CampusIO.pm $RPM_BUILD_ROOT%{l_prefix}/lib/flowscan/
      %{l_shtool} install -c -m 644 \
          -e 's;..FindBin::Bin./SubNetIO\.cf;%{l_prefix}/etc/flowscan/flowscan-subnetio.cf;' \
          SubNetIO.pm $RPM_BUILD_ROOT%{l_prefix}/lib/flowscan/
      %{l_shtool} install -c -m 644 %{l_value -s -a} \
          %{SOURCE flowscan.cf} $RPM_BUILD_ROOT%{l_prefix}/etc/flowscan/
      %{l_shtool} install -c -m 644 \
          cf/SubNetIO.cf $RPM_BUILD_ROOT%{l_prefix}/etc/flowscan/flowscan-subnetio.cf
      %{l_shtool} install -c -m 644 \
          cf/CampusIO.cf $RPM_BUILD_ROOT%{l_prefix}/etc/flowscan/flowscan-campusio.cf
    ) || exit $?

    #   install CUFlow reporting plugin
    ( cd CUFlow-%{V_cuflow}
      %{l_shtool} install -c -m 644 \
          -e 's;..FindBin::Bin./CUFlow\.cf;%{l_prefix}/etc/flowscan/flowscan-cuflow.cf;' \
          CUFlow.pm $RPM_BUILD_ROOT%{l_prefix}/lib/flowscan/
      %{l_shtool} install -c -m 644 %{l_value -s -a} \
          %{SOURCE flowscan-cuflow.cf} $RPM_BUILD_ROOT%{l_prefix}/etc/flowscan/
      %{l_shtool} install -c -m 755 \
          -e 's;#! /usr/bin/perl -w;#!%{l_prefix}/bin/perl;' \
          -e 's;/cflow/reports/rrds;%{l_prefix}/var/flowscan/cuflow/rrd;' \
          -e 's;Estimated Columbia University Campus;OpenPKG;' \
          CUGrapher.pl $RPM_BUILD_ROOT%{l_prefix}/cgi/flowscan-cuflow.cgi
    ) || exit $?

    #   install queuing program
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE flowscan-queue.sh} $RPM_BUILD_ROOT%{l_prefix}/bin/flowscan-queue

    #   install Apache configuration
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE flowscan-apache.conf} $RPM_BUILD_ROOT%{l_prefix}/etc/flowscan/

    #   install run-command script
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE rc.flowscan} $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/

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

%files -f files

%clean

%post
    if [ ".$1" = .1 ]; then
        #   add hook to Apache configuration
        apacheconf="$RPM_INSTALL_PREFIX/etc/apache/apache.conf"
        if [ -f $apacheconf ]; then
            ( echo "Include $RPM_INSTALL_PREFIX/etc/flowscan/flowscan-apache.conf"
            ) | $RPM_INSTALL_PREFIX/lib/openpkg/rpmtool config \
                    -a -i "$RPM_INSTALL_PREFIX:flowscan" $apacheconf
        fi
    fi
    #   after upgrade, restart service
    [ $1 -eq 2 ] || exit 0
    eval `%{l_rc} flowscan status 2>/dev/null`
    [ ".$flowscan_active" = .yes ] && %{l_rc} flowscan restart
    exit 0

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

%postun
    if [ ".$1" = .0 ]; then
        #   remove hook from Apache configuration
        apacheconf="$RPM_INSTALL_PREFIX/etc/apache/apache.conf"
        if [ -f $apacheconf ]; then
            $RPM_INSTALL_PREFIX/lib/openpkg/rpmtool config \
                -r -i "$RPM_INSTALL_PREFIX:flowscan" $apacheconf
        fi
    fi

