#!/bin/bash


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

echo ""
echo "[ netdisco_config ] Configuration Script for Netdicso"
echo ""
echo "   * This script will configure Netdisco on a stock Fedora installation"
echo ""
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 "               This script was modified by Walter Gould <gouldwp@fedoraproject.org> for Fedora install"
echo ""
echo -n "Hit Return to continue or Ctrl-C to exit : "
read foo


# start postgresql
echo ""
echo ""
echo "Starting postgresql......."
echo ""
echo ""
/sbin/service postgresql initdb
/etc/init.d/postgresql start
sleep 3


# Edit pg_hba.conf file
echo ""
echo ""
echo "Backing up and editing postgresql files......."
echo ""
echo ""
cp /var/lib/pgsql/data/pg_hba.conf /var/lib/pgsql/data/pg_hba.conf.orig
echo "# TYPE DATABASE USER IP-ADDRESS IP-MASK METHOD" > /var/lib/pgsql/data/pg_hba.conf
echo "local all all trust" >> /var/lib/pgsql/data/pg_hba.conf
echo "host all all 127.0.0.1 255.255.255.255 trust" >> /var/lib/pgsql/data/pg_hba.conf
/etc/init.d/postgresql restart
sleep 3


# netdisco config file
echo ""
echo ""
echo "About to edit the netdisco config files.........."
echo ""
echo ""
stty -echo
echo -n "Enter Netdisco Database password: "
read netdisco_passwd
echo ""
stty echo
cat /etc/netdisco.conf | sed "s/dbpassword/$netdisco_passwd/g" > /tmp/netdisco.conf.$$
mv -f /tmp/netdisco.conf.$$ /etc/netdisco.conf
cat /etc/httpd/conf.d/netdisco_apache.conf | sed "s/PASSWORDHERE/$netdisco_passwd/g" > /tmp/netdisco_apache.conf.$$
mv -f /tmp/netdisco_apache.conf.$$ /etc/httpd/conf.d/netdisco_apache.conf
#
echo -n "Enter domain name: "
read domain_read
echo ""
cat /etc/netdisco.conf | sed "s/mycompany\.com/$domain_read/g" > /tmp/netdisco.conf.$$
mv -f /tmp/netdisco.conf.$$ /etc/netdisco.conf
#
echo -n "Enter SNMP read string: "
read snmp_read
echo ""
cat /etc/netdisco.conf | sed "s/public/$snmp_read/g" > /tmp/netdisco.conf.$$
mv -f /tmp/netdisco.conf.$$ /etc/netdisco.conf
#
echo -n "Enter SNMP write string: "
read snmp_write
echo ""
cat /etc/netdisco.conf | sed "s/private/$snmp_write/g" > /tmp/netdisco.conf.$$
mv -f /tmp/netdisco.conf.$$ /etc/netdisco.conf
#
#


# Making Apache2 - Modperl2 changes

if [ `/bin/uname -i` == "i386" ]; then

cat /etc/httpd/conf.d/netdisco_apache.conf | sed 's/libexec\/apache2\/mod_perl.so/\/usr\/lib\/httpd\/modules\/mod_perl.so/' > /tmp/netdisco_apache.conf.$$
mv -f /tmp/netdisco_apache.conf.$$ /etc/httpd/conf.d/netdisco_apache.conf
#
cat /etc/httpd/conf.d/netdisco_apache.conf | sed 's/libexec\/apache2\/mod_apreq2.so/\/usr\/lib\/httpd\/modules\/mod_apreq2.so/' > /tmp/netdisco_apache.conf.$$
mv -f /tmp/netdisco_apache.conf.$$ /etc/httpd/conf.d/netdisco_apache.conf

  elif [ `/bin/uname -i` == "x86_64" ]; then

cat /etc/httpd/conf.d/netdisco_apache.conf | sed 's/libexec\/apache2\/mod_perl.so/\/usr\/lib64\/httpd\/modules\/mod_perl.so/' > /tmp/netdisco_apache.conf.$$
mv -f /tmp/netdisco_apache.conf.$$ /etc/httpd/conf.d/netdisco_apache.conf
#
cat /etc/httpd/conf.d/netdisco_apache.conf | sed 's/libexec\/apache2\/mod_apreq2.so/\/usr\/lib64\/httpd\/modules\/mod_apreq2.so/' > /tmp/netdisco_apache.conf.$$
mv -f /tmp/netdisco_apache.conf.$$ /etc/httpd/conf.d/netdisco_apache.conf

  else 

echo ""
echo ""
echo "Your machine's architecture is unknown to us.  You need to find the path of mod_perl.so and mod_apreq2.so, and update /etc/httpd/conf.d/netdisco_apache.conf with that information." 
echo ""
echo ""

fi


# Stop apache errors at startup
mv -f /etc/httpd/conf.d/apreq.conf /etc/httpd/conf.d/apreq.conf.orig
mv -f /etc/httpd/conf.d/perl.conf /etc/httpd/conf.d/perl.conf.orig
#
cat /etc/httpd/conf.d/netdisco_apache_dir.conf | sed 's/Alias \/netdisco\//#Alias \/netdisco\//' > /tmp/netdisco_apache_dir.conf.$$
mv -f /tmp/netdisco_apache_dir.conf.$$ /etc/httpd/conf.d/netdisco_apache_dir.conf


echo ""
echo ""
echo "Creating Netdisco database tables........"
echo ""
echo ""
cd /usr/share/netdisco/sql 
./pg --init > /dev/null 2>&1
#
echo ""
echo ""
echo ""
echo "Finished creating Netdisco database tables........"
echo ""
echo ""
cd $curdir
sleep 3




# oui database
echo "Populating the OUI database for Netdisco........"
cd /usr/share/netdisco
/usr/bin/wget -q http://standards.ieee.org/regauth/oui/oui.txt
/usr/sbin/netdisco -O > /dev/null 2>&1
echo ""
echo ""
echo ""
echo "Finished installing OUI database........"
echo ""
echo ""
sleep 3


# crontab
echo ""
echo ""
echo "About to setup netdisco crontab........"
echo ""
echo ""
echo -n "Enter center CDP device for network discovery: "
read center_dev
cat /usr/share/netdisco/netdisco.crontab | sed "s/center_network_device/$center_dev/g" > /tmp/netdisco.crontab.$$
mv -f /tmp/netdisco.crontab.$$ /usr/share/netdisco/netdisco.crontab
crontab -u netdisco /usr/share/netdisco/netdisco.crontab
sleep 3
echo ""
echo ""
echo "Finished setting up netdisco crontab........"
echo ""
echo ""
sleep 3

# fix permissions
chown -R netdisco.netdisco /usr/share/netdisco
chcon system_u:object_r:etc_t:s0 /etc/netdisco.conf
chcon system_u:object_r:httpd_config_t:s0 /etc/httpd/conf.d/netdisco_apache.conf
chcon system_u:object_r:httpd_config_t:s0 /etc/httpd/conf.d/netdisco_apache_dir.conf
mkdir /usr/share/netdisco/mason/
chcon -R system_u:object_r:httpd_sys_content_t:s0 /usr/share/netdisco/mason/
#
touch /usr/share/netdisco/html/netmap.map /usr/share/netdisco/html/netmap.gif
chcon system_u:object_r:usr_t:s0 /usr/share/netdisco/html/netmap.map /usr/share/netdisco/html/netmap.gif
chmod 644 /usr/share/netdisco/html/netmap.map /usr/share/netdisco/html/netmap.gif
chown netdisco:netdisco /usr/share/netdisco/html/netmap.map /usr/share/netdisco/html/netmap.gif

# EPEL
/etc/init.d/postgresql start
/etc/init.d/httpd start

# Fedora
#/bin/systemctl start postgresql.service
#/usr/sbin/httpd > /dev/null 2>&1

# Start postgres and httpd at system startup
/sbin/chkconfig postgresql on
/sbin/chkconfig httpd on
/sbin/chkconfig netdisco on

# create netdisco user - and start netdisco
echo ""
echo ""
echo "About to create netdisco admin user........"
echo ""
echo ""
echo ""
/usr/sbin/netdisco -u admin


# Discover, macsuck and graph process
echo ""
echo ""
echo ""
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo "Netdisco is about to discover your network, perform a macsuck on each device, and"
echo "setup your network graph."
echo ""
echo "This portion of the install process may take some time, depending on the size of your"
echo "network.  Please be patient and don't cancel out of it.  This would be a good time"
echo "to get up and take a break, get a drink or go to lunch...."
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo ""
echo ""
echo ""
sleep 10
/usr/sbin/netdisco -r $center_dev
/usr/sbin/netdisco -m
/usr/sbin/netdisco -g > /dev/null 2>&1
echo ""
echo ""
echo " Starting up Netdisco front-end........"
echo ""
echo ""
/etc/init.d/netdisco start
echo ""
echo ""
echo ""
echo "Netdisco Installation completed!!!!"
echo ""
echo ""
ipaddress=`/sbin/ifconfig eth0 | grep "inet addr:" | cut -c21-36 | cut -d' ' -f1`
echo "Login via https://$ipaddress/netdisco/"
echo ""
echo ""
echo "Enjoy!!!"
