#!/bin/sh

# just in case
umask 0022

if [ -z "$AWARE_HOME" ]
then
    echo "Environment variable AWARE_HOME not defined!"
    echo "Please read INSTALL for instructions."
    exit
fi

echo 'Do you want to configure the Aware Management Interface [y,n](y): '
read y_n
if [ -n "$y_n" -a "$y_n" != "y" ]; then exit ; fi

echo 'Configuring Aware Management Interface...'
echo

echo 'You need to have a web server installed with'
echo 'PHP (www.php.net) and MySQL (www.mysql.com) support.'
echo

echo 'You will likely need to be root to perform this installation'
echo

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

# check dependencies

nmblookup=`which nmblookup 2>/dev/null`
if [ -z "$nmblookup" ]
then
    cat <<EOF
Failed dependancy:
  nmblookup is not in your PATH.
  nmblookup is required for network discovery.
  nmblookup is part of the Samba package.
  Please read $AWARE_HOME/EXTERNAL_PACKAGES for information on Samba.
  Configuration failed.
EOF
    exit
fi

snmpget=`which snmpget 2>/dev/null`
if [ -z "$snmpget" ]
then
    cat <<EOF
Failed dependancy:
  snmpget is not in your PATH.
  snmpget is required for network discovery.
  snmpget is part of the Net-SNMP package.
  Please read $AWARE_HOME/EXTERNAL_PACKAGES for information on Net-SNMP.
  Configuration failed.
EOF
    exit
fi

snmpwalk=`which snmpwalk 2>/dev/null`
if [ -z "$snmpwalk" ]
then
    cat <<EOF
Failed dependancy:
  snmpwalk is not in your PATH.
  snmpwalk is required for network discovery.
  snmpwalk is part of the Net-SNMP package.
  Please read $AWARE_HOME/EXTERNAL_PACKAGES for information on Net-SNMP.
  Configuration failed.
EOF
    exit
fi

rrdcgi=`which rrdcgi 2>/dev/null`
if [ -z "$rrdcgi" ]
then
    cat <<EOF
Failed dependancy:
  rrdcgi is not in your PATH.
  rrdcgi is required for plotting data.
  rrdcgi is part of the RRDTOOL package.
  Please read $AWARE_HOME/EXTERNAL_PACKAGES for information on RRDTOOL.
  Configuration failed.
EOF
    exit
fi

php=`which php 2>/dev/null`
if [ -z "$php" ]
then
    cat <<EOF
Failed dependancy:
  php is not in your PATH.
  php is required to generate the master aware wire file from the database.
  php is part of the PHP package.
  Please read $AWARE_HOME/EXTERNAL_PACKAGES for information on PHP.
  Configuration failed.
EOF
    exit
fi


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

echo
echo '--- Aware Parameters ---'
echo


# given install dir?
if [ -n "$1" ]
then
    AWARE_HOME=$1
else
    echo "Input directory where you have installed Aware ($AWARE_HOME): "
    read _aware_home ;
    if [ -n "$_aware_home" ]; then AWARE_HOME=$_aware_home ; fi
fi

_aware_home=$AWARE_HOME ; # remember for later

# run file
awared_start=`ls -1 $AWARE_HOME/bin/*/awared.sh`;

# read the startup script to get values
echo "Reading $awared_start for configuration."
echo "[NOTE: Edit this file to change file locations]";
echo

#somfin?
if [ ! -f "$awared_start" ] ; then echo "Cannot find $awared_start!"; exit ; fi

# source the file
EVALONLY=1 # this will keep sure we don't run any code
. $awared_start

if [ "$AWARE_HOME" != "$_aware_home" ] 
then
cat <<EOF
ERROR:
 $awared_start has different value for AWARE_HOME!

   $awared_start => $AWARE_HOME

   You selected  => $_aware_home
  
 Edit $awared_start or change your selection

EOF
  exit
fi

### Aware config dependencies

# rrdtool
if [ ! -f $AWARE_HOME/lib/$MACHTYPE/librrdhandler.a ]
then
cat <<EOF
ERROR:
 Aware was not compiled with RRDTOOL support.
 Please recompile Aware with RRDTOOL support.
EOF
 exit
fi

# mysql
if [ ! -f $AWARE_HOME/lib/$MACHTYPE/libmysqlhandler.a ]
then
cat <<EOF
ERROR:
 Aware was not compiled with MySQL support.
 Please recompile Aware with MySQL support.
EOF
 exit
fi

echo 'Generating 'settings' for database'
echo

# get version info
. $AWARE_HOME/lib/$MACHTYPE/version.sh

cat > ./sql/aw_settings.defaults <<EOF
insert into aw_settings (kw,val,note) values ('aware.major_version','$AW_MAJOR_VERSION','Major version of aware');
insert into aw_settings (kw,val,note) values ('aware.minor_version','$AW_MINOR_VERSION','Minor version of aware');
insert into aw_settings (kw,val,note) values ('aware.patch_level','$AW_PATCH_LEVEL','Patch level of aware');
insert into aw_settings (kw,val,note) values ('aware.version','$AW_VERSION','Version of aware');
insert into aw_settings (kw,val,note) values ('aware.home','$AWARE_HOME','Directory of Aware installation');
insert into aw_settings (kw,val,note) values ('aware.rcscript','$awared_start',' Path to start/stop script for awared');
insert into aw_settings (kw,val,note) values ('aware.datadir','$awaredata/','Directory for holding Aware data outside database (e.g., logs)');
insert into aw_settings (kw,val,note) values ('aware.pidfile','$pidfile','Path to pid file');
insert into aw_settings (kw,val,note) values ('aware.logdir','$logdir','Directory for log data');
insert into aw_settings (kw,val,note) values ('aware.logfile','$logfile','Path to log for awared process');
insert into aw_settings (kw,val,note) values ('aware.wire','$master_wirefile','Path to wire file');
insert into aw_settings (kw,val,note) values ('aware.discoverycycle','daily','Cycletime for discovery [manual,daily,weekly,monthly]');
insert into aw_settings (kw,val,note) values ('aware.rrddir','$rrddir','Path to Round Robin Databases');
insert into aw_settings (kw,val,note) values ('aware.rxeventport','$rxeventport','Port to receive external alerts');
EOF


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

echo
echo '--- MySQL Parameters ---'
echo

dbhost=localhost

# read old values if present
if [ -f scripts/mysql.sh ]; then . scripts/mysql.sh ; fi

echo "Input MySQL database host ($dbhost): "
read _dbhost ;
if [ -n "$_dbhost" ]; then dbhost=$_dbhost ; fi

echo "Input MySQL database name ($db): "
read _db;
if [ -n "$_db" ]; then db=$_db ; fi

echo "Input MySQL database user account ($dbuser): "
read _dbuser ;
if [ -n "$_dbuser" ]; then dbuser=$_dbuser ; fi

echo "Input MySQL database user account password ($dbpasswd): "
read _dbpasswd ;
if [ -n "$_dbpasswd" ]; then dbpasswd=$_dbpasswd ; fi

# update dependent files
password_files='scripts/mysql.sh wire/mysql.wire lib/dbconfig.inc'

# for the shell scripts
cat > scripts/mysql.sh <<EOF
# connect info for the database
dbhost=$dbhost
dbuser=$dbuser
dbpasswd=$dbpasswd
db=$db
EOF

# for the wire scripts
cat > wire/mysql.wire <<EOF
// set these to your local MySQL installation
set dbhost $dbhost
set dbuser $dbuser
set dbpasswd $dbpasswd
set db $db
EOF


# for the php scripts
cat lib/dbconfig.inc.top > lib/dbconfig.inc
cat >> lib/dbconfig.inc <<EOF
\$dbhost = '$dbhost';
\$dbuser = '$dbuser';
\$dbpasswd = '$dbpasswd';
\$db = '$db';
EOF
cat lib/dbconfig.inc.bottom >> lib/dbconfig.inc

echo "The following files contain the login/password for $db"
echo "(you should probably make these read only for privileged users):"
for f in $password_files
do
  echo "$PWD/$f"
done

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

echo
echo '--- Web Server Config ---'
echo 'Your Web Server Requires PHP and MySQL support'
echo

docroot=/var/www/html
echo "Docroot of your web server ($docroot): "
read _docroot ;
if [ -n "$_docroot" ]; then docroot=$_docroot ; fi

rm -f $docroot/aware
if [ `ln -s $AWARE_HOME/src/mgmtui/www $docroot/aware` ]
then
  echo "Could not make symbolic link $docroot/aware -> $AWARE_HOME/src/mgmtui/www"
  exit
fi

echo "Made symbolic link $docroot/aware -> $AWARE_HOME/src/mgmtui/www"
echo

rm -f $AWARE_HOME/src/mgmtui/www/pages/node/rrd
if [ `ln -s $rrddir $AWARE_HOME/src/mgmtui/www/pages/node/rrd` ]
then
  echo "Could not make symbolic link $AWARE_HOME/src/mgmtui/www/pages/node/rrd -> $rrddir"
  exit
fi

echo "Made symbolic link $AWARE_HOME/src/mgmtui/www/pages/node/rrd -> $rrddir"
echo

httpuser=apache
echo "User web server process runs under ($httpuser): "
read _httpuser ;
if [ -n "$_httpuser" ]; then httpuser=$_httpuser ; fi

# .. so web server can write graphs
chgrp -R $httpuser $rrddir
chmod -R g+wx $rrddir

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

echo
echo '--- Database Intialization ---'
echo 'WARNING ALL PREVIOUS AWARE DATA WILL BE LOST!'
echo
echo 'Not initializing the database will allow an upgrade'
echo 'using existing aware data.'
echo

echo 'Do you want to initialize the database? [y,n](y):'
read y_n
if [ -z "$y_n" -o "$y_n" = "y" ]
then 

    # remove old rrd files
    rm -f $rrddir/ping/*.rrd $rrddir/service/*.rrd $rrddir/traffic/*.rrd 

    # run create script
    (cd scripts ; ./create.sh );
else

    # run update script
    (cd scripts ; ./update.sh );
fi

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

echo
echo '--- Network Discovery ---'
echo 'This will take from 5 to 15 minutes for most networks.'
echo 'Larger networks with hundreds of nodes may take much longer.'
echo 'It will scan the class C IP networks attached to this computer'
echo 'to create monitored entries in the database for all found ip:ports'
echo 
echo 'You can skip this and manually enter the nodes to monitor.'
echo

echo 'Do you want to run network discovery? [y,n](y):'
read y_n
if [ -z "$y_n" -o "$y_n" = "y" ]
then 
    (cd scripts ; ./discover.sh);
fi




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


echo
echo '--- Creating initial awared wire file ---'
echo

# create a script that generates the wire file
cat > $genwire <<EOF
echo "<?php ini_set('short_open_tag', 'On'); include '$AWARE_HOME/src/mgmtui/lib/mkwire.inc'; wire_create('$genwire','$master_wirefile'); ?>" | php 
EOF
chmod u+x $genwire

echo "Writing wire file to $master_wirefile"
$genwire


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

echo
echo '--- Done ---'
echo

echo
echo 'NOTE: The default initial login is administrator/administrator .'
echo 'NOTE: You should login under this account to create other accounts.'
echo 'NOTE: It is recommended that you change the administrator password.'
echo

echo
echo 'NOTE: After you login to the UI, read the online help for more info.'
echo

