#!/bin/sh

# Install script for Terapaths Netflow monitoring
# modified from JKFlow universal installation script

# TODO: Need a method of stopping script on any error

###############################################################################
# User defined installation paths
###############################################################################

# install directory
export PREFIX=/opt/terapaths-monitoring

# data directory for netflow data and RRD storage
export DATADIR=/var/flows

# directory for scratch disk for installation and compilation
export SCRATCH=`pwd`

# cgi bin directory
export CGIBIN=/var/www/cgi-bin/


###############################################################################
# No need to modify past this point
###############################################################################

# package versions
FLOWSCAN='FlowScan-1.006'
RRDTOOL='rrdtool-1.2.15'
LIBART='libart_lgpl-2.3.17'
LIBPNG='libpng-1.2.10'
FREETYPE='freetype-2.1.10'
BOULDER='Boulder-1.30'
PATRICIA='Net-Patricia-1.010'
CONFIGREADER='ConfigReader-0.5'
JKFLOW='jkflow-v3.5.2'
FLOWTOOLS='flow-tools-0.67'
HTMLTABLE='HTML-Table-2.02'
XMLSIMPLE='XML-Simple-2.14'
DBMDEEP='DBM-Deep-0.983'
GDGRAPH='GDGraph-1.43'
GDTEXT='GDTextUtil-0.86'

# compilation directory
export TEMP=$SCRATCH/packages
mkdir $TEMP

# make the conf directory for config files
mkdir -p $PREFIX/conf

# perl version
export PERLVERSION=`perl -v | grep 'This is perl, ' | awk '{print $4}' | sed s/v//g`
export PERLSTRING=`perl -v | grep 'This is perl, ' | awk '{print $7}'`

echo "======================================================================="
echo "Fetching packages..."
echo "======================================================================="
cd $TEMP
if [ ! -e "$FLOWSCAN.tar.gz" ]
then wget http://net.doit.wisc.edu/~plonka/FlowScan/FlowScan-1.006.tar.gz; fi
if [ ! -e "$RRDTOOL.tar.gz" ]
then wget http://people.ee.ethz.ch/~oetiker/webtools/rrdtool/pub/$RRDTOOL.tar.gz; fi
if [ ! -e "$LIBART.tar.gz" ]
then wget http://people.ee.ethz.ch/~oetiker/webtools/rrdtool/pub/libs/$LIBART.tar.gz; fi
if [ ! -e "$LIBPNG.tar.gz" ]
then wget http://people.ee.ethz.ch/~oetiker/webtools/rrdtool/pub/libs/$LIBPNG.tar.gz; fi
if [ ! -e "$FREETYPE.tar.bz2" ]
then wget http://people.ee.ethz.ch/~oetiker/webtools/rrdtool/pub/libs/$FREETYPE.tar.bz2; fi
if [ ! -e "$BOULDER.tar.gz" ]
then wget http://search.cpan.org/CPAN/authors/id/L/LD/LDS/$BOULDER.tar.gz; fi
if [ ! -e "$PATRICIA.tar.gz" ]
then wget http://search.cpan.org/CPAN/authors/id/P/PL/PLONKA/$PATRICIA.tar.gz; fi
if [ ! -e "$CONFIGREADER.tar.gz" ]
then wget http://search.cpan.org/CPAN/authors/id/A/AM/AMW/$CONFIGREADER.tar.gz; fi
if [ ! -e "$JKFLOW.tgz" ]
then wget http://users.telenet.be/jurgen.kobierczynski/jkflow/$JKFLOW.tgz; fi
if [ ! -e "$FLOWTOOLS.tar.gz" ]
then wget ftp://ftp.eng.oar.net/pub/flow-tools/$FLOWTOOLS.tar.gz; fi
if [ ! -e "$HTMLTABLE.tar.gz" ]
then wget http://search.cpan.org/CPAN/authors/id/A/AJ/AJPEACOCK/$HTMLTABLE.tar.gz; fi
if [ ! -e "$XMLSIMPLE.tar.gz" ]
then wget http://search.cpan.org/CPAN/authors/id/G/GR/GRANTM/$XMLSIMPLE.tar.gz; fi
if [ ! -e "$DBMDEEP.tar.gz" ]
then wget http://search.cpan.org/CPAN/authors/id/R/RK/RKINYON/DBM-Deep-0.983.tar.gz; fi
if [ ! -e "$GDGRAPH.tar.gz" ]
then wget http://search.cpan.org/CPAN/authors/id/M/MV/MVERB/GDGraph-1.43.tar.gz; fi
if [ ! -e "$GDTEXT.tar.gz" ]
then wget http://search.cpan.org/CPAN/authors/id/M/MV/MVERB/GDTextUtil-0.86.tar.gz; fi

# TODO: Need some way of validating the downloaded files are archives, not failed wgets

echo "======================================================================="
echo "Untaring packages..."
echo "======================================================================="

cd $TEMP
# for some reason RRDtool and freetype doesn't like to be retold where to install, so delete the folder altogether before commencing (and recompile)
if [ -e "$RRDTOOL" ]
then rm -rf $RRDTOOL; fi
if [ -e "$FREETYPE" ]
then rm -rf $FREETYPE; fi

echo "$FLOWSCAN..."
tar -zxf $FLOWSCAN.tar.gz
echo "$RRDTOOL..."
tar -zxf $RRDTOOL.tar.gz
echo "$LIBART..."
tar -zxf $LIBART.tar.gz
echo "$LIBPNG..."
tar -zxf $LIBPNG.tar.gz
echo "$FREETYPE..."
tar -jxf $FREETYPE.tar.bz2
echo "$BOULDER..."
tar -zxf $BOULDER.tar.gz
echo "$PATRICIA..."
tar -zxf $PATRICIA.tar.gz
echo "$CONFIGREADER..."
tar -zxf $CONFIGREADER.tar.gz
echo "$JKFLOW..."
tar -zxf $JKFLOW.tgz
echo "$FLOWTOOLS..."
tar -zxf $FLOWTOOLS.tar.gz
echo "$HTMLTABLE..."
tar -zxf $HTMLTABLE.tar.gz
echo "$XMLSIMPLE..."
tar -zxf $XMLSIMPLE.tar.gz
echo "$DBMDEEP..."
tar -zxf $DBMDEEP.tar.gz
#echo "$GDGRAPH..."
#tar -zxf $GDGRAPH.tar.gz
#echo "$GDTEXT..."
#tar -zxf $GDTEXT.tar.gz

# JKFlow likes to use upper case letters, so find out new name
JKFLOW=`ls | grep -i jkflow | grep -v *.tgz`

echo "======================================================================="
echo "Patching packages..."
echo "======================================================================="

cd $TEMP

# TODO: Need an automatic way of determine version of gcc to be used.

# Apply patch to make flow-tools compiling with gcc 3.4 (Fedora Core 3, 4, Centos 4.0, 4.2)
patch -p0 < JKFlow-v3.5.2/flow-tools_gcc34_patch
# Apply patch to make flow-tools compiling with gcc 4.0 (Fedora Core 5)
#patch -p0 < $JKFLOW/flow-tools_gcc40_patch
# Apply patch to resolve error "option '/var/flows/...'"
patch -p0 $FLOWSCAN/FlowScan.pm < $JKFLOW/FlowScan.pm_flow-tools_patch


echo "======================================================================="
echo "Installing Libraries for RRDTool..."
echo "======================================================================="

echo "Installing $LIBART"
cd $TEMP/$LIBART
./configure --prefix=$PREFIX
make
make install
# if configure complains of
#   checking how to run the C++ preprocessor... /lib/cpp 
#   configure: error: C++ preprocessor "/lib/cpp" fails sanity check
# then you need to install gcc-c++-3.4.5-2.i386.rpm (or similar depending on gcc version)

echo "Installing $LIBPNG"
cd $TEMP/$LIBPNG
./configure --prefix=$PREFIX
make
make install

echo "Installing $FREETYPE"
cd $TEMP/$FREETYPE
./configure --prefix=$PREFIX
make
make install

echo "======================================================================="
echo "Installing RRDTool..."
echo "======================================================================="

# pick up the libraries and includes
export CPPFLAGS=" -I$PREFIX/include -I$PREFIX/include/freetype2 -I$PREFIX/include/libart-2.0 -I$PREFIX/include/libpng"
export LDFLAGS=" -L$PREFIX/lib"
export PKG_CONFIG_PATH=$PREFIX/lib/pkgconfig

# compile and install
cd $TEMP/$RRDTOOL
./configure --disable-tcl --disable-python --disable-rrdcgi --prefix=$PREFIX
make
make install

# perl bindings
cd bindings/perl-shared
perl Makefile.PL PREFIX=$PREFIX
make
make install

# stupid script doesn't copy the files to the right place
mv -f $PREFIX/lib/perl/$PERLVERSION/$PERLSTRING/auto/* $PREFIX/lib/perl5/site_perl/$PERLVERSION/$PERLSTRING//auto/
mv  $PREFIX/lib/perl/$PERLVERSION/$PERLSTRING/*  $PREFIX/lib/perl5/site_perl/$PERLVERSION/$PERLSTRING/
rm -rf $PREFIX/lib/perl/

#Make sure FlowScan finds rrdtool
export PATH=$PATH:$PREFIX/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$PREFIX/lib
export PERL5LIB=$PERL5LIB:$PREFIX/lib/perl5/site_perl/$PERLVERSION

#Make sure FlowScan finds RRDs
#Exchange comments here when your distribution doesn't use ld.so.conf.d directory
#echo '/usr/local/rrdtool-1.2.11/lib' >> /etc/ld.so.conf

# add the library paths
#echo "$PREFIX/lib" >> /etc/ld.so.conf
#/sbin/ldconfig

echo "======================================================================="
echo "Installing Boulder Perl Module..."
echo "======================================================================="

cd $TEMP/$BOULDER
perl Makefile.PL  PREFIX=$PREFIX
make
make install

echo "======================================================================="
echo "Installing Patricia Perl Module..."
echo "======================================================================="

cd $TEMP/$PATRICIA
perl Makefile.PL  PREFIX=$PREFIX
make
make install


echo "======================================================================="
echo "Installing ConfigReader Perl Module..."
echo "======================================================================="

cd $TEMP/$CONFIGREADER
# default /usr/lib/perl5/vendor_perl
cp ConfigReader.pod $PREFIX/lib/perl5/site_perl/$PERLVERSION/
mkdir $PREFIX/lib/perl5/site_perl/$PERLVERSION/ConfigReader
cp DirectiveStyle.pm $PREFIX/lib/perl5/site_perl/$PERLVERSION/ConfigReader
cp Spec.pm $PREFIX/lib/perl5/site_perl/$PERLVERSION/ConfigReader
cp Values.pm $PREFIX/lib/perl5/site_perl/$PERLVERSION/ConfigReader

echo "======================================================================="
echo "Installing Flow-Tools..."
echo "======================================================================="

cd $TEMP/$FLOWTOOLS
./configure --prefix=$PREFIX
make
make install
#Exchange comments here when your distribution doesn't use ld.so.conf.d directory
#echo '/usr/local/netflow/lib' >> /etc/ld.so.conf
#echo '$PREFIX/netflow/lib' > /etc/ld.so.conf.d/netflow.conf
#/sbin/ldconfig

#Cflow must be compiled for flow-tools
cd contrib
# get version number
CFLOW=`ls | grep Cflow*.tar.gz | sed s%.tar.gz%%g`
tar -zxf $CFLOW.tar.gz
cd $CFLOW
perl Makefile.PL PREFIX=$PREFIX
make
make install

echo "======================================================================="
echo "Installing HTML Table Perl Module..."
echo "======================================================================="

cd $TEMP/$HTMLTABLE
perl Makefile.PL PREFIX=$PREFIX
make
make install

echo "======================================================================="
echo "Installing XML-Simple Perl Module..."
echo "======================================================================="

cd $TEMP/$XMLSIMPLE
perl Makefile.PL PREFIX=$PREFIX
make
make install

echo "======================================================================="
echo "Installing DBM-Deep Perl Module..."
echo "======================================================================="

cd $TEMP/$DBMDEEP
perl Makefile.PL PREFIX=$PREFIX
make
make install


#echo "======================================================================="
#echo "Installing GDGraph Perl Module..."
#echo "======================================================================="

#cd $TEMP/$GDGRAPH
#perl Makefile.PL PREFIX=$PREFIX
#make
#make install

#echo "======================================================================="
#echo "Installing GDText Perl Module..."
#echo "======================================================================="

#cd $TEMP/$GDTEXT
#perl Makefile.PL PREFIX=$PREFIX
#make
#make install

echo "======================================================================="
echo "Installing FlowScan..."
echo "======================================================================="

cd $TEMP/$FLOWSCAN
./configure --prefix=$PREFIX
make install

# If you get an error such as:
#    gcc  -I../lib -L../lib    -g -Wall -c aclyacc.c
#    y.tab.c: In function `yyparse':
#    y.tab.c:417: warning: implicit declaration of function `yylex'
#    y.tab.c:458: warning: implicit declaration of function `yyerror'
# then you need to install the following RPMS:
#    flex-2.5.4a-33.i386.rpm  bison-1.875c-2.i386.rpm

# modify the config file for JKFlow
export FLOWDIR=`echo "$DATADIR" | sed s%'/'%'\\\\/'%g`
cat cf/flowscan.cf | \
sed s/FlowFileGlob\ flows.*/FlowFileGlob\ $FLOWDIR\\/flows\\/ft-v05.*/ | \
sed s/CampusIO/JKFlow/ > $PREFIX/conf/flowscan.cf

# link in the config file
if [ -e "$PREFIX/bin/flowscan.cf" ]
then rm -f $PREFIX/bin/flowscan.cf; fi
ln -s $PREFIX/conf/flowscan.cf $PREFIX/bin/flowscan.cf

echo "======================================================================="
echo "Installing JKFlow..."
echo "======================================================================="

# may be better idea just to install from a 'SLAC' distro

cd $TEMP/$JKFLOW

# JKFlow.pm is hardcoded with /usr/local/bin -> modify
cat JKFlow.pm | sed s%/usr/local/bin/%$PREFIX/conf/%g > $PREFIX/bin/JKFlow.pm

#avoid reporting nothing, because when there is no traffic from/to 10.0.0.0/8 no traffic shows up
cat JKFlow_example_verybasic.xml | \
sed s/\ localsubnets=\"10.0.0.0\\/8\"// > $PREFIX/conf/JKFlow.xml

# set up the config file
if [ -e "$PREFIX/bin/JKFlow.xml" ]
then rm -f $PREFIX/bin/JKFlow.xml; fi
ln -s $PREFIX/conf/JKFlow.xml $PREFIX/bin/JKFlow.xml

#TODO download patches

echo "======================================================================="
echo "Installing Webpages..."
echo "======================================================================="

# TODO: do a copy?
# set up the html directory
# ln -s /opt/terapaths-monitoring/www/html/netflow /var/www/html/netflow
# apache setup?

## link the html files and script directory
## terapaths monitoring netflow html files and scripts
# Alias /netflow "/opt/terapaths-monitoring/www/html/"
# ScriptAliasMatch /netflow/(.*).cgi "/opt/terapaths-monitoring/www/cgi-bin/$1.cgi"

echo "======================================================================="
echo "Setting up system..."
echo "======================================================================="

mkdir -p $DATADIR/flows
mkdir -p $DATADIR/reports/rrds
mkdir -p $DATADIR/score
# SELinux disables CGI-scripts, enable boolean flag CGI-script SELinux if you don't want this
#/usr/sbin/setenforce 0
# dynamic graph generation script
#cp $TEMP/$JKFLOW/JKGrapher.pl $CGIBIN/

### TODO copy the init script to the /etc/init.d

cd $TEMP

echo "======================================================================="
echo "Finished!"
echo "======================================================================="

echo 'RRDtool 1.2.11 + FlowScan 1.006 + flow-tools 0.67 + JKFlow 3.5.2 has been installed.'
echo "A basic configuraiton has been setup, please modify $PREFIX/bin/JKFlow.xml for your reporting needs."
echo "Netflow records captured using flow-capture should be dumped to $DATADIR/flows/."
echo ''
echo "The suite libraries have been installed to $PREFIX/lib. The path has been added"
echo 'to you system wide install at /etc/ld.so.conf.'
echo ''
echo 'Please add the following rule in /etc/profile, and relogin to activate the changes:'
echo ''
echo "  pathmunge $PREFIX"
echo ''
echo 'To start flowscan, use:'
echo ''
echo "  $PREFIX/bin/flowscan"
echo ''
echo 'To start flow-capture use:'
echo ''
echo "  $PREFIX/bin/flow-capture -p /var/run/flow-capture.pid -n 287 -N 0 -w $DATADIR/flows/ -S 5 0/0/2055"
echo ''


