#!/bin/sh


# Installation of JFFNMS from source
# Main install script
# Please see jffnms-files.txt for list of files needed


TMPDIR=/tmp
BUILDDIR=$TMPDIR/jffnms.build-$$
JFFNMS_FILE_CACHE=/usr/src/jffnms
MYSQL_PREFIX=/usr/local/mysql
APACHE_PREFIX=/usr/local/apache
PROFILE_D=/etc/profile.d
MYSQL_PATH_SH=$PROFILE_D/mysql.sh
APACHE_PATH_SH=$PROFILE_D/apache.sh
RRDTOOL_PREFIX=/usr/local/rrdtool
CRONTAB=/etc/crontab
RC_LOCAL=/etc/rc.d/rc.local


mkdir $BUILDDIR
cd $BUILDDIR


#################################################


echo Checking for Disk Space


#################################################


SPACE=`df -m /tmp | grep dev | awk '{print $4}'`


if (($SPACE < 300)); then


        echo "There isn't enough diskspace"
        echo "this requires 300MB"
        rm -rf $BUILDDIR
        exit 1


fi


#################################################


echo Checking running as ROOT


#################################################


if [ `id -u` != 0 ]; then


        echo "You must be root to run this script"
        echo "Login as root and try again"
        exit 1
fi


#################################################


echo Building MYSQL


#################################################




if  [ ! `cat /etc/group | grep -q mysql` ] ; then
        echo adding mysql group
        groupadd mysql
fi





# -M don't create a user dir
if  [ ! `cat /etc/passwd | grep -q mysql` ] ; then
        echo adding mysql user
        useradd -M -g mysql mysql
fi


echo Untarring Files
tar zxf $JFFNMS_FILE_CACHE/mysql-*
cd mysql-*



./configure --prefix=$MYSQL_PREFIX \
                --localstatedir=$MYSQL_PREFIX/data \
                --disable-maintainer-mode \
                --with-mysqld-user=mysql \
                --enable-large-files \
                --without-comment \
                --without-debug \
                # --without-bench Include this if you don't want to run the benchmark
tests
                --without-bench \
                --with-named-curses-libs=/lib/libncurses.so.5
                # added --with-named-curses-libs=/lib/libncurses.so.5 to suppress ncurses/termcap error


make


make install


scripts/mysql_install_db


echo set correct permission on mysql dirs
chown -R root:mysql $MYSQL_PREFIX
chown -R mysql:mysql $MYSQL_PREFIX/data


# update ld library



if [ ! `cat /etc/ld.so.conf | grep -q  "$MYSQL_PREFIX/lib/mysql"` ] ; then

	echo "$MYSQL_PREFIX/lib/mysql" >> /etc/ld.so.conf

fi

if [ `ldconfig -v | grep -q libmysqlclient` ] ; then
	echo Have found mysql libs
fi


# This next line adds the MySQL startup command into your rc.local file so
# that MySQL starts automatically everytime your server comes up.
if ! `cat $RC_LOCAL | grep -q safe_mysqld` ; then
        echo "$MYSQL_PREFIX/bin/safe_mysqld --user=mysql  > /dev/null 2>&1 &" >> $RC_LOCAL
fi


$MYSQL_PREFIX/bin/safe_mysqld --user=mysql > /dev/null 2>&1 &


# if we get a reply we are working woo hoo


echo -n "Please enter the New password for MYSQL root account : "
read $new-password


if [ -z $new-password ] ; then
        echo warning password is blank still
fi


# change blank password for new-password
$MYSQL_PREFIX/bin/mysqladmin -uroot -p password $new-password



echo adding the mysql bin dir to PATH
touch $MYSQL_PATH_SH


cat << EOF > $MYSQL_PATH_SH
# Mysql Path Initialisation Script
# James McDonald


if [ ! \`echo \$PATH | grep $MYSQL_PREFIX/bin\` ] ; then


        PATH=\$PATH:$MYSQL_PREFIX/bin


fi
export PATH


EOF


chmod +x $MYSQL_PATH_SH


##############################################


echo Compiling / Installing Apache


##############################################


cd $BUILDDIR


echo Untarring apache openssl and mod_ssl


tar zxf $JFFNMS_FILE_CACHE/apache*
tar zxf $JFFNMS_FILE_CACHE/openssl*
tar zxf $JFFNMS_FILE_CACHE/mod_ssl*


cd $BUILDDIR/openssl*
echo Configuring OpenSSL
./config
echo Making OpenSSL
make


cd $BUILDDIR


MOD_SSL=`ls -1 | grep mod_ssl`
echo $MOD_SSL


OPEN_SSL=`ls -1 | grep openssl`
echo $OPEN_SSL


APACHE=`ls -1 | grep apache`
echo $APACHE


cd $BUILDDIR/mod_ssl*


# patch apache source with mod_ssl
./configure \
              --with-apache=../$APACHE \
              --with-ssl=../$OPEN_SSL \
              --prefix=$APACHE_PREFIX



cd $BUILDDIR/apache*


# enable dso's and all of them
./configure --prefix=$APACHE_PREFIX \
	--enable-module=all \
	--enable-shared=ssl \
	--enable-shared=max


make


# This is if you want to isntall a test https cert.
make certificate


make install


touch $APACHE_PATH_SH
chmod +x $APACHE_PATH_SH


cat << EOF > $APACHE_PATH_SH
# Apache Path Initialisation Script
# James McDonald


if [ ! \`echo \$PATH | grep $APACHE_PATH_SH/bin\` ] ; then


        PATH=\$PATH:$APACHE_PATH_SH/bin


fi
export PATH


EOF


echo Adding start up script to rc.local


if ! `cat $RC_LOCAL | grep -q apachectl` ; then


        echo "$APACHE_PREFIX/bin/apachectl startssl > /dev/null 2>&1 &" >> $RC_LOCAL


fi


echo Starting apache webserver


$APACHE_PREFIX/bin/apachectl startssl > /dev/null 2>&1 &



echo `$APACHE_PREFIX/bin/httpd -v | grep version | cut -d: -f2` up and running



##############################################################


echo Building Graphviz and Webfonts and supporting libraries


##############################################################
# libpng-1.2.4.tar.bz2
# zlib-1.1.4.tar.gz


# **************************************************************************


echo building zlib


# **************************************************************************


# need zlib for png and jpeg compression

cd $BUILDDIR

tar zxf $JFFNMS_FILE_CACHE/zlib*


cd $BUILDDIR/zlib*


./configure --prefix=/usr/local


make


make install


# **************************************************************************


echo Building libpng


# **************************************************************************


cd $BUILDDIR
bunzip2 -c  $JFFNMS_FILE_CACHE/libpng* | tar -x


cd $BUILDDIR/libpng*


rm -f *akfile


cp scripts/makefile.linux Makefile.old


# Change Path to the zlibs
sed -e s+^ZLIBLIB.*+ZLIBLIB=/usr/local/lib+ < Makefile.old > Makefile.$$
sed -e s+^ZLIBINC.*+ZLIBINC=/usr/local/include+ < Makefile.$$ > Makefile


make
make install


# **************************************************************************


echo Building libjpeg


#
***************************************************************************


cd $BUILDDIR
tar zxf $JFFNMS_FILE_CACHE/jpegsrc*
./configure --prefix=/usr/local --enable-shared


make
make install



# make sure you do this or you will be having issues compiling php with
# imagepng() createfrompng() functions


echo /usr/local/lib >> /etc/ld.so.conf
ldconfig -v | grep libpng


cd $BUILDDIR


# **************************************************************************


echo building gd


# **************************************************************************


# gd needs libjpeg and libpng so build after them


tar zxf $JFFNMS_FILE_CACHE/gd*
cd $BUILDDIR/gd*
make


# isntalls to /usr/local
make install


###############################################################################


echo Building Graphviz


################################################################################


cd $BUILDDIR


tar zxf $JFFNMS_FILE_CACHE/graphviz*


cd $BUILDDIR/graphviz*


./configure --prefix=/usr/local \
                --with-zlibdir=/usr/local/lib \
                --with-zlibincludedir=/usr/local/include \
                --with-pngincludedir=/usr/local/include \
                --with-pnglibdir=/usr/local/lib


make
make install


# **************************************************************************


echo Building NMAP


# **************************************************************************


cd $BUILDDIR


tar zxf $JFFNMS_FILE_CACHE/nmap*


cd $BUILDDIR/nmap*


./configure --prefix=/usr/local
make
make install


##############################################################################


echo BUILDING RRDTOOL


##############################################################################


cd $BUILDDIR
tar zxf $JFFNMS_FILE_CACHE/rrdtool*


cd $BUILDDIR/rrdtool*


./configure --enable-shared --prefix=$RRDTOOL_PREFIX


make
make install


################################################################################


echo Building SNMP


#################################################################################


cd $BUILDDIR


tar zxf $JFFNMS_FILE_CACHE/ucd-snmp*
cd ucd-snmp*


./configure --prefix=/usr/local --enable-shared



make


make install


#################################################################################


echo Building PHP4


#################################################################################
# build php first with apache then without


cd $BUILDDIR


bunzip2 -c $JFFNMS_FILE_CACHE/php-4* | tar x
tar xzf $JFFNMS_FILE_CACHE/php4-rrdtool*



cd php-4*


# need to know the exact dir name for later
PHP_DIR=`pwd`


# add rrdtool


mkdir $PHP_DIR/ext/rrdtool


cd $BUILDDIR


cd php4-rrdtool*


cp config.m4 rrdtool.c php_rrdtool.h Makefile.in $PHP_DIR/ext/rrdtool/


cd $PHP_DIR


# remove configure and rebuild it to give us --with-rrdtool opiton


rm -f configure


./buildconf


# Build without apache (php binary)
./configure \
--prefix=/usr/local \
--disable-debug \
--enable-inline-optimization \
--with-regex=system \
--with-ttf \
--with-zlib \
--enable-sockets \
--enable-track-vars \
--with-mysql=$MYSQL_PREFIX \
--enable-shared=rrdtool \
--with-rrdtool=$RRDTOOL_PREFIX \
--with-snmp=/usr/local \
--with-readline \
--with-openssl \
--with-gd=/usr/local \
--with-png-dir=/usr/local \
--with-jpeg-dir=/usr/local


make
make install


make clean


# configure and build w/ apache (libphp4.so)
./configure \
--prefix=/usr/local \
--disable-debug \
--enable-inline-optimization \
--with-apxs=$APACHE_PREFIX/bin/apxs \
--with-regex=system \
--with-ttf \
--with-zlib \
--enable-sockets \
--enable-track-vars \
--with-mysql=$MYSQL_PREFIX \
--enable-shared=rrdtool \
--with-rrdtool=$RRDTOOL_PREFIX \
--with-snmp=/usr/local \
--with-readline \
--with-openssl \
--with-gd=/usr/local \
--with-png-dir=/usr/local \
--with-jpeg-dir=/usr/local


# make sure to remember gd


make
make install



cp php.ini-dist /usr/local/lib/php.ini.$$


# jffnms doesn't work without register_globals = On
sed s/"^register_globals = Off"/"register_globals = On"/g < /usr/local/lib/php.ini.$$ > /usr/local/lib/php.ini



#########################################################################################


echo Installing MSYSLOG - modular system logger


##########################################################################################


# I have no idea how this works so your guess is as good as mine?


cd $BUILDDIR


tar zxf $JFFNMS_FILE_CACHE/msyslog*


cd msyslog*


./configure --prefix=/usr/local


make
make install


##################################


echo Building tac_plus


##################################


cd $BUILDDIR


tar zxf $JFFNMS_FILE_CACHE/tac_plus*


cd tac_plus*


./configure --prefix=/usr/local


# fix tac_plus bug
mv $MYSQL_PREFIX/include/mysql/mysql.h $MYSQL_PREFIX/include/mysql/mysql.h.old
sed s+DB+db+g < $MYSQL_PREFIX/include/mysql/mysql.h.old mv > $MYSQL_PREFIX/include/mysql/mysql.h


# point tac_plus to correct mysql.h
mv Makefile Makfile.old
sed s+^CPPFLAGS.*$+CPPFLAGS=-I$MYSQL_PREFIX/include/mysql+ < Makefile.old > Makefile


#finally ready to compile


make tac_plus
make install


###############################################


echo Build tftpd


#################################################


cd $BUILDDIR
tar zxf $JFFNMS_FILE_CACHE/netkit-tftp*
cd netkit-tftp*


./configure --prefix=/usr/local
make
make install



#################################################


echo Installing JFFNMS


##################################################


cd $BUILDDIR
tar zxf $JFFNMS_FILE_CACHE/jffnms*


mv jffnms* /opt/jffnms


chown -R nobody.nobody /opt/jffnms/


# backup crontab
cp $CRONTAB $CRONTAB.$$


cat << EOF >> $CRONTAB
# added by jffnms install
*/1 * * * * nobody php -q /opt/jffnms/engine/consolidate.php >/dev/null 2>&1
*/5 * * * * nobody php -q /opt/jffnms/engine/poller.php >/dev/null 2>&1
*/30 * * * * nobody php -q /opt/jffnms/engine/rrd_analizer.php >/dev/null 2>&1
*/30 * * * * nobody php -q /opt/jffnms/engine/autodiscovery_interfaces.php > /dev/null 2>&1
02 4 * * * nobody php -q /opt/jffnms/engine/tftpget_hosts.php >/dev/null 2>&1
EOF


# create the jffnms db
mysqladmin -uroot -p create jffnms


# add the tables
mysql -uroot -p jffnms < /opt/jffnms/docs/jffnms*.sql


# make sure you create a user with sufficient rights to the db


echo You now have to add a user please execute the following in mysql
echo GRANT ALL PRIVILEGES ON jffnms.* TO jffnms@localhost
echo IDENTIFIED BY 'some_pass' WITH GRANT OPTION;


mysql --uroot -p mysql


echo Configure apache httpd.conf


#

# now add the type statements to the httpd.conf Manually I haven't been able
# to get this working in sed


#AddType application/x-httpd-php .php
#AddType application/x-httpd-php-source .phps



#<VirtualHost *>
#    ServerAdmin you@yournet.com
#    DocumentRoot /opt/jffnms/htdocs
#    ServerName nms.yournet.com
#</VirtualHost>





