#! /bin/sh
#
# Setup script for the master host
#
if [ -r scripts/setup_functions ]
	then
		. scripts/setup_functions
else
	if [ -r /usr/local/bcnu/scripts/setup_functions ]
		then
			. /usr/local/bcnu/scripts/setup_functions
	fi
fi
clear

cat <<END;
Welcome to the setup script for bcnu.
 
This script is only used when you first install bcnu on your "master" host. 

The master is the host which receives alerts and where your web 
browsers can access information about the status of systems.

The script will offer default settings which you can override and will
ask for some basic information from you to enable the system to be set
up as quickly and easily as possible.

Defaults or detected settings will be presented in [brackets], accept
these by pressing enter, or enter an alternative value


END

# set bcnu home directory

bcnu_home_dir

# reinstate the defaults
if [ -r $BCNUHOME/etc/bcnuenv.distrib.setup ]
	then
		cp $BCNUHOME/etc/bcnuenv.distrib.setup $BCNUHOME/etc/bcnuenv.distrib
fi

# get the system type

bcnu_system_type

# get the host name

bcnu_hostname

# get the service port

bcnu_port

bcnu_sample_agent

# Setup the hostinfo file
#
printf "Enter the default email address for alerts:[root@$BCNUHOST] "
read email
if [ -z "$email" ]
	then
		email="root@$BCNUHOST"
fi

printf "Enter the default pager number for alerts:[none] "
read pager
if [ -z "$pager" ]
	then
		pager="none"
fi

# setup perl 
PERLBIN="/usr/local/bin/perl"

if [ ! -x $PERLBIN ]
	then
		PERL=`which perl`

		if expr "$PERL" : ".*no perl.*" >/dev/null
			then 
				echo "I can't find perl!"
				printf "Enter location of perl binary "
				read PERL
		fi
		echo "Will create symbolic link for:"
		printf "  $PERLBIN -> $PERL OK? y/n "
	        read yorn
		if [ "y" != "$yorn" ]
		        then
				echo "Setup aborted!"
				exit
		fi
else
	PERL=$PERLBIN
	PERLOK="YES"
fi


echo
echo "Setup: master host is ..........($BCNUHOST)"
echo "Setup: bcnu home directory is ..($BCNUHOME)"
echo "Setup: bcnu server type is .....($BCNUSERVER)"
echo "Setup: bcnu TCP port is ........($BCNUPORT)"
echo "Setup: perl is located at ......($PERL -> $PERLBIN)"
echo "Setup: copy sample agent file...($SAMPLEAGENT)"
echo "Setup: email address is ........($email)"
echo "Setup: pager address is ........($pager)"

echo 
printf "Are these settings OK? y/n "

read yorn
if [ "y" != "$yorn" ]
	then
		echo "Setup aborted!"
		exit
fi

# about to do the damage

echo "Setup: Updating $BCNUHOME/etc/bcnuenv.distrib"

cat $BCNUHOME/etc/bcnuenv.distrib | 		\
sed "s/BCNUHOST=dns1/BCNUHOST=$BCNUHOST/g" |	\
sed "s/BCNUPORT:=6666/BCNUPORT:=$BCNUPORT/g"	\
>/tmp/bcnuenv.distrib

cp $BCNUHOME/etc/bcnuenv.distrib $BCNUHOME/etc/bcnuenv.distrib.setup
cp /tmp/bcnuenv.distrib $BCNUHOME/etc/bcnuenv.distrib

echo "Setup: Updating $BCNUHOME/etc/hostinfo.distrib"

cat $BCNUHOME/etc/hostinfo.distrib | 		\
sed "s/DEFEMAIL=\"\$techmail\"/DEFEMAIL=\"$email\"/" |	\
sed "s/DEFPAGER=\"\$techpage\"/DEFPAGER=\"$pager\"/"	\
>/tmp/hostinfo.distrib

cp /tmp/hostinfo.distrib $BCNUHOME/etc/hostinfo.distrib

# update the hostinfo file
echo "\$defemail=\"$email\";" | sed "s/\@/\\\@/g" >$BCNUHOME/data/hostinfo
echo "\$defpager=\"$pager\";" >>$BCNUHOME/data/hostinfo
echo "1;" >>$BCNUHOME/data/hostinfo

if [ "$SAMPLEAGENT" = "yes" ]
	then
		echo "Setup: Updating $BCNUHOME/etc/agents.distrib"
		cat $BCNUHOME/platform/agents.$BCNUSERVER |
		sed "s/#agent:alert/agent:alert/" | \
		sed "s/#agent:buildweb/agent:buildweb/"  \
		>/tmp/agents.distrib
		cp /tmp/agents.distrib $BCNUHOME/etc/agents.distrib
fi

if [ -z "$PERLOK" ]
	then
		echo "Setup: Updating perl location"
		ln -s $PERL $PERLBIN
fi

echo "Setup: system prepared"

echo
echo "Setup: Running install script"

$BCNUHOME/scripts/client_install -h $BCNUHOME -c $BCNUSERVER -d n -s y

rm /tmp/*.distrib

sleep 5

echo "Setup: Master install completed"

if [ -f $BCNUHOME/README.$BCNUSERVER ]
	then
		more $BCNUHOME/README.$BCNUSERVER
fi

echo 
printf "Do you want to start up bcnu now and send some test alerts? y/n "

read yorn
if [ "y" != "$yorn" ]
	then
		echo "Setup complete - bcnud not started"
		exit
fi

echo
echo "Setup: Starting bcnud"
$BCNUHOME/etc/bcnud_server start

TESTHOST="testing"

cat >/tmp/bcnu.alert <<END;

This is a test alert from bcnu Setup. 

To remove these alerts from the web display, delete the directory

$BCNUHOME/data/hosts/$TESTHOST on $BCNUHOST

END

# start sending the alerts
echo "Sending Normal message - status 0 - no file"
BCNUAGENT=setup0 ; export BCNUAGENT
$BCNUHOME/bin/bcnu -s $BCNUPORT -h $TESTHOST -t 1 -e 0 -m "Normal message - status 0 - no file" $BCNUHOST

echo "Sending Normal message - status 0 - with file"
BCNUAGENT=setup0f ; export BCNUAGENT
$BCNUHOME/bin/bcnu -s $BCNUPORT -h $TESTHOST -t 1 -e 0 -f /tmp/bcnu.alert -m "Normal message - status 0 - with file" $BCNUHOST

echo "Sending Warning message - status 5 - no file"
BCNUAGENT=setup5 ; export BCNUAGENT
$BCNUHOME/bin/bcnu -s $BCNUPORT -h $TESTHOST -t 1 -e 5 -m "Warning message - status 5 - no file" $BCNUHOST

echo "Sending Warning message - status 5 - with file"
BCNUAGENT=setup5f ; export BCNUAGENT
$BCNUHOME/bin/bcnu -s $BCNUPORT -h $TESTHOST -t 1 -e 5 -f /tmp/bcnu.alert -m "Warning  message - status 5 - with file" $BCNUHOST

echo "Sending Error message - status 9 - no file"
BCNUAGENT=setup9 ; export BCNUAGENT
$BCNUHOME/bin/bcnu -s $BCNUPORT -h $TESTHOST -t 1 -e 9 -m "Error message - status 9 - no file" $BCNUHOST

echo "Sending Error message - status 9 - with file"
BCNUAGENT=setup9f ; export BCNUAGENT
$BCNUHOME/bin/bcnu -s $BCNUPORT -h $TESTHOST -t 1 -e 9 -f /tmp/bcnu.alert -m "Error  message - status 9 - with file" $BCNUHOST

rm /tmp/bcnu.alert

echo
echo "Check the directory $BCNUHOME/data/hosts/$TESTHOST for files"
echo "called setup0,5,9,0f,5f,9f - these are the alerts which have been sent"
echo "Mail will be sent to $email as well. If your web view is working you"
echo "should be able to see these alerts in your browser"

echo "Final tidy of permissions again"
$BCNUHOME/scripts/fix_perms

echo
echo "Setup: completed"
