#!/bin/bash

# netdisco_bootstrap - Mandriva Configuration Script for Netdicso
# based on debian_install.sh written by Kaven Rousseau.

if [ "x`whoami`" != "xroot" ]; then
    echo 'You must be root to run this.'
    exit
fi

echo ""
echo "[ netdisco_bootstrap ] Mandriva Configuration Script for Netdicso"
echo ""
echo "   * This script will configure Netdisco on a stock Mandriva installation"
echo ""
echo "   * You _must_ read INSTALL completely first.  "
echo "     Some Manual steps are still required."
echo ""
echo "UNSUPPORTED -  Please use at your own risk.   "
echo "               This script was contributed by Kaven Rousseau and is untested."
echo "               This script was modified by Oden Eriksson <oeriksson@mandriva.com>"
echo ""
echo -n "Hit Return to continue or Ctrl-C to exit : "
read foo

# get hostname
ipaddress=`ifconfig eth0 | grep "inet addr:" | cut -c21-36 | cut -d' ' -f1`

PASSWORD=`perl -e 'for ($i = 0, $bit = "!", $key = ""; $i < 8; $i++) {while ($bit !~ /^[0-9A-Za-z]$/) { $bit = chr(rand(90) + 32); } $key .= $bit; $bit = "!"; } print "$key";'`

# netdisco config files
echo -n "Please enter Netdisco Database password, press enter for the generated one (${PASSWORD}): "
read netdisco_db_passwd
if [ -z ${netdisco_db_passwd} ]; then netdisco_db_passwd=${PASSWORD}; fi
perl -pi -e "s|^db_Pg_pw.*|db_Pg_pw = ${netdisco_db_passwd}|g" /etc/netdisco.conf
perl -pi -e "s|session_password.*|session_password =\> \'${netdisco_db_passwd}\'\,|g" /etc/httpd/conf/webapps.d/netdisco_apache.conf
chmod 640 /etc/netdisco.conf /etc/httpd/conf/webapps.d/netdisco_apache.conf
chown netdisco:root /etc/netdisco.conf

# create db
echo "Creating an populating a PostgreSQL database for Netdisco."
service postgresql status | grep '(pid' >/dev/null 2>&1 || service postgresql start

pushd /var/www/netdisco/sql >/dev/null 2>&1
    #su -l postgres -s /bin/sh -c "dropdb netdisco >/dev/null 2>&1 ; dropuser netdisco >/dev/null 2>&1"
    su -l postgres -s /bin/sh -c "createdb netdisco >/dev/null 2>&1"
    su -l postgres -c "psql -c \"CREATE USER netdisco WITH PASSWORD '${netdisco_db_passwd}' NOCREATEUSER\" netdisco >/dev/null 2>&1"
    perl ./pg --init -d /var/www/netdisco
popd

# oui database
echo "Populating the OUI database for Netdisco."
pushd /var/www/netdisco >/dev/null 2>&1
    # /usr/bin/wget -q http://standards.ieee.org/regauth/oui/oui.txt
    /usr/sbin/netdisco -O
popd

echo -n "Enter center CDP device for network discovery: "
read center_network_device
perl -pi -e "s|/usr/sbin/netdisco -b -r .*|/usr/sbin/netdisco -b -r ${center_network_device}|g" /var/www/netdisco/netdisco.crontab

# create netdisco user
echo "Creating Netdisco users in Netdisco."
pushd /var/www/netdisco >/dev/null 2>&1
    /usr/sbin/netdisco -u eonadmin
    /usr/sbin/netdisco -r ${center_network_device}
    /usr/sbin/netdisco -m
    /usr/sbin/netdisco -g
popd

# fix permissions
touch /var/www/netdisco/html/netmap.map /var/www/netdisco/html/netmap.gif
chmod 644 /var/www/netdisco/html/netmap.map /var/www/netdisco/html/netmap.gif
chown netdisco:netdisco /var/www/netdisco/html/netmap.map /var/www/netdisco/html/netmap.gif

# add a crontab entry
echo "Adding a cronjob for Netdisco."
crontab -u netdisco /var/www/netdisco/netdisco.crontab

echo "Restarting apache."
/sbin/service httpd restart >/dev/null 2>&1

echo "Restarting netdisco."
/sbin/service netdisco restart >/dev/null 2>&1

echo "Installation completed."
echo "Restart server to verify if everything starts starts."
echo "login via: http://$ipaddress/netdisco/"

