#!/bin/sh

# Prepare Cacti/Weathermap dev/test environment for Ubuntu Server 12.04
#
# Assumed: 
#   * Selected LAMP option during install.
#   * System WON'T be internet accessible or otherwise vulnerable!
#

#######################################################################
# 
# Configuration

CACTI_VERSION="0.8.8a"
SPINE_VERSION="0.8.8a"

WEATHERMAP_VERSION="svn" # version number or svn or 'none'

MYSQL_ROOT_PWD="mypassword"

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

# Cacti stuff
apt-get -y install git subversion php5 php5-gd snmp rrdtool

# General C stuff (to build spine)
apt-get -y install autoconf build-essential linux-headers-`uname -r`

# Spine stuff
apt-get -y install libmysqlclient-dev libsnmp-dev 

# Weathermap - the manual uses XSLT and Weathermap uses PEAR
apt-get -y install xsltproc php-pear

# Weathermap - fping datasource, xdebug & ImageMagick for unit tests
apt-get -y install fping imagemagick php5-xdebug

# Generally useful for testing
apt-get -y install snmpd zip unzip

# Download Cacti & Spine, patches
cd /var/www
wget http://www.cacti.net/downloads/cacti-0.8.8a.tar.gz
wget http://www.cacti.net/downloads/spine/cacti-spine-0.8.8a.tar.gz

# Extract Cacti & Patch
tar xvfz cacti-0.8.8a.tar.gz
cd cacti-0.8.8a
wget http://www.cacti.net/downloads/patches/0.8.8a/snmpv3_priv_proto_none.patch
patch -p1 -N < snmpv3_priv_proto_none.patch

echo "This is about to ask for the mysql root password..."

# Create Cacti database
(echo "create database cacti;"; echo "use cacti;"; cat cacti.sql ) | mysql -uroot -p

# build & install Spine
cd ..
tar xvfz cacti-spine-0.8.8a.tar.gz
cd cacti-spine-0.8.8a
./configure && make && make install

# Weathermap from SVN (NOT RECOMMENDED for normal use!)
cd ../cacti-0.8.8a/plugins/
svn checkout http://www.network-weathermap.com/svn/repos/trunk  weathermap
cd ../

# Fix some perms, ready for the poller
useradd -d /var/www/cacti-0.8.8a cacti
chown -R cacti /var/www/cacti-0.8.8a/log
chown -R cacti /var/www/cacti-0.8.8a/rra

# create the cron job, ready to be symlinked to /etc/cron.d
echo "*/5 * * * * cacti /usr/bin/php /var/www/cacti-0.8.8a/poller.php > /var/www/cacti-0.8.8a/last-poller-output.txt 2>&1" > cacti.cron 
touch /var/www/cacti-0.8.8a/last-poller-output.txt
chown cacti /var/www/cacti-0.8.8a/last-poller-output.txt

# Some extra PEAR stuff we use - PHPUnit and Markdown
pear channel-discover pear.michelf.com
pear install michelf/MarkdownExtra
pear config-set auto_discover 1
pear install pear.phpunit.de/PHPUnit

# You still need to edit cacti-0.8.8a/include/config.php at this point - The URL base and the database parameters need to be changed, at least.
#
# You also need to set up the poller for the cacti user, if necessary (it's for DEV, right?)
#
# This will do it:
#   ln -s /var/www/cacti-0.8.8a/cacti.cron /etc/cron.d/cacti

# And you should have a running Cacti, with Weathermap dev trunk installed, and enough tools to make changes and tests

