#!/bin/sh

# bbconfig
# BIG BROTHER - AUTOMATIC CONFIG SCRIPT
# Sean MacGuire - The MacLawran Group Inc.
# Version 1.4
# Mar 05th, 2000
#
# Copyright (c) 1997-2000 The MacLawran Group Inc - All Rights Reserved

#--------------------------------------------------------
# FIGURE OUT WHERE WE ARE AND SET BBHOME
#--------------------------------------------------------

PROG="$0"; export PROG
OSARG="$1"
# put all OS type to lower case
# thanks for the tip to "Thomas E. Alex" <talex@commgensys.com>
OSARG=`echo "$OSARG" | tr '[A-Z]' '[a-z]' `
ARGNUM="$#"
NAME=`basename $0`; export NAME
DIR=`echo $PROG | sed "s/${NAME}$//"`

cd $DIR				# GET TO THE INSTALL DIRECTORY
cd ..				# GO UP TO BBHOME
BBHOME=`pwd`			# SET IT THE EASY WAY
cd $DIR				# GO BACK WHERE WE WERE...

#--------------------------------------------------------
# WHAT VERSION OF echo?
#--------------------------------------------------------
# Find out which version of echo we have
# So we use the proper directive to not print out the newline character
# There are differences in echo on a SYSV system and a *BSD based system

testline=`echo -n bobo`
# echo "TESTLINE=$testline"

if [ "$testline" = "bobo" ]
then
	ECHOOPTION=" -n "
	ECHOTAIL=""
else
	ECHOOPTION=""
	ECHOTAIL='\c'
fi


tput clear

#--------------------------------------------------------
# WE REALLY SHOULD TRY TO FIGURE WHAT MACHINE WE'RE ON.
#--------------------------------------------------------

set `uname -a`
VERSION="$3"
if [ "$OSARG" = "" ]
then
	MACHINE="$1"		# THIS IS WHAT THE MACHINE THINKS IT IS
	MACHINE=`echo "$MACHINE" | tr '[A-Z]' '[a-z]' `
	# echo MACHINE IS $MACHINE
else
	MACHINE=$OSARG
fi

case $MACHINE
in
	linux )
		# ARE WE A REDHAT MACHINE?
		grep "Red" /etc/issue >/dev/null
		if test "$?" = "0"
		then
			MACHINE="redhat"
		fi
		grep "Caldera" /etc/issue > /dev/null
		if test "$?" = "0"
		then
			MACHINE="caldera"
		fi	
		;;

	sunos )
		echo "VERSION=$VERSION"
		VERSION=`echo $VERSION | sed "s/\.//g"`
		echo "VERSION=$VERSION"
		if test "$VERSION" -ge "24"
		then
			MACHINE="solaris"
		fi
		;;

	hp-ux )
		MACHINE="hpux"
		;;
esac

OS="bsdi sco3 sco freebsd solaris hpux9 hpux linux sunos netbsd osf ultrix irix unixware redhat aix dynix debian dgux openbsd caldera sinix"

#--------------------------------------------------------
# SET UP BBHOME
#--------------------------------------------------------

if test "$ARGNUM" != "1"
then
echo "

	Welcome to the Big Brother installation procedure...
	The first thing we need to know is that OS you're
	running...

	Choose one of the following: $OS

"
	echo $ECHOOPTION "Please enter your OS: [$MACHINE]: $ECHOTAIL"
	read ANS

	if test "$ANS" != ""
	then
		MACHINE="$ANS"
	fi
fi

echo $OS | grep "$MACHINE" > /dev/null 2>&1
if test "$?" = "1"
then
	echo "---> $MACHINE unsupported... configure manually"
	echo "---> ./bbconfig [OS]"
	echo "--->	where OS is one of: $OS"
	exit 2
else
	echo "---> OK, we'll try $MACHINE..."
fi

case ${MACHINE}
in
	solaris )
		# Force the use of /usr/xpg4/bin if it's available
		if [ ! -d  /usr/xpg4/bin ]
		then
			echo "** It is stronly suggested that you get the xpg4 package **
** and install it on this server and rerun this script **
** before continuing any further **"
		fi
		;;
esac


#--------------------------------------------------------
# SET UP BBHOME
#--------------------------------------------------------

echo "
	Making sure BBHOME <$BBHOME> is writable...
"

if [ ! -w "$BBHOME" ]
then
	echo "<$BBHOME> is not a writable directory"
	echo "Please become root or change permissions".
	echo "Exiting."
	exit 1
	ANS=""
fi
echo "---> OK, $BBHOME is fine..."

#--------------------------------------------------------
# SET UP FQDN
#--------------------------------------------------------

FQDN="y"
BBFQDN="FALSE"
while [ "$BBFQDN" != "TRUE" -a "$BBFQDN" != "" ]
do
	echo "

	When you set up your machines, you should use Fully Qualified
	Domain names, this means you use the whole name, like www.bb4.com,
	instead of just 'www'.  This is recommended.
	"
	echo $ECHOOPTION "Use FQDN (y/n): [$FQDN] $ECHOTAIL"
	read ANS

	if [ "$ANS" = "" ]
	then
		BBFQDN="TRUE"
		echo "---> Good, we'll use FQDN"
	else
		if [ "$ANS" = 'y' -o "$ANS" = 'Y' ]
		then
			BBFQDN="TRUE"
			echo "---> Good, we'll use FQDN"
		else
			BBFQDN=""
			echo "---> OK... if you must..."
		fi
	fi
done

#--------------------------------------------------------
# SET UP BBDISPLAY
#--------------------------------------------------------
	
BBDISPLAY=""
while [ "$BBDISPLAY" = "" ]
do
	BBDISPLAY=`hostname` 2>/dev/null

	echo "

	Big Brother creates HTML pages with the status of your network.
	You'll need a web server to publish this information.
	"
	echo $ECHOOPTION "What machine will be the BBDISPLAY [$BBDISPLAY]: $ECHOTAIL"
	read ANS

	if [ "$ANS" = "" ]
	then
		echo "---> OK... $BBDISPLAY will be a BBDISPLAY"
	else
		BBDISPLAY=$ANS
		echo "---> OK... $BBDISPLAY will be a BBDISPLAY"
	fi
	
	BBDISPLAY=`echo "$BBDISPLAY" | tr '[A-Z]' '[a-z]'`
done

#--------------------------------------------------------
# SET UP BBPAGER
#--------------------------------------------------------

BBPAGER=""
while [ "$BBPAGER" = "" ]
do
	BBPAGER=$BBDISPLAY

	echo "

	Big Brother sends important messages to a pager server.  This
	machine will at a minimum to be able to send mail. 
	"
	echo $ECHOOPTION "What machine will be the BBPAGER [$BBPAGER]: $ECHOTAIL"
	read ANS

	if [ "$ANS" = "" ]
	then
		echo "---> OK... $BBPAGER will be a BBPAGER"
	else
		BBPAGER=$ANS
		echo "---> OK... $BBPAGER will be a BBPAGER"
	fi
	
	BBPAGER=`echo "$BBPAGER" | tr '[A-Z]' '[a-z]'`
	
done

DOWEB="y"
while [ "$DOWEB" != "TRUE" -a "$DOWEB" != "FALSE" ]
do
        echo $ECHOOPTION "
Is this host a BBDISPLAY host (y/n): [$DOWEB] $ECHOTAIL"
	read ANS
	if [ "$ANS" = "" ]
	then
		ANS=$DOWEB
	fi
	if [ "$ANS" = 'y' -o "$ANS" = 'Y' ]
	then
		DOWEB=TRUE
	else
		DOWEB=FALSE
	fi
done

DOPAGE="y"
while [ "$DOPAGE" != "TRUE" -a "$DOPAGE" != "FALSE" ]
do
        echo $ECHOOPTION "
Is this host a BBPAGER host (y/n): [$DOPAGE] $ECHOTAIL"
	read ANS
	if [ "$ANS" = "" ]
	then
		ANS=$DOPAGE
	fi
	if [ "$DOPAGE" = 'y' -o "$ANS" = 'y' -o "$ANS" = 'Y' ]
	then
		DOPAGE=TRUE
	else
		DOPAGE=FALSE
	fi
done

DEFAULTPAGE=""
if [ "$DOPAGE" = "TRUE" ]
then
	while [ "$DEFAULTPAGE" = "" ]
	do
		DEFAULTPAGE="root@localhost"
        	echo $ECHOOPTION "
Enter the default recipient: [$DEFAULTPAGE] $ECHOTAIL"
        read ANS
	if [ "$ANS" != "" ]
	then
		DEFAULTPAGE=$ANS
	fi
done                            

fi
#--------------------------------------------------------
# SET URL OF BB
#--------------------------------------------------------

BBWEB=""
if [ "$DOWEB" = "TRUE" ]
then
while [ "$BBWEB" = "" ]
do
	echo "
	Since Big Brother produces results to be displayed on web
	pages, we need to know where to view these results.
	"
	echo $ECHOOPTION "Enter URL for BB [http://${BBDISPLAY}/bb/]: $ECHOTAIL"
	read ANS
	if test "$ANS" != ""
	then
		BBWEB="$ANS"
	else	
		BBWEB="http://$BBDISPLAY/bb/"
	fi
	echo "$BBWEB" | grep ".*/$" >/dev/null 2>&1
	if [ "$?" -ne 0 ]
	then
		echo "
The URL <$BBWEB> is invalid, it must terminate with a '/'
"
		BBWEB=""
	else
		echo "---> OK... Big Brother will live at $BBWEB"
	fi
done

#--------------------------------------------------------
# SET LOCATION OF CGI DIRECTORIES
#--------------------------------------------------------

CGIBIN=""
while [ "$CGIBIN" = "" ]
do
	echo "
	Big Brother also uses CGI scripts to create dynamic output.
	What directory do these scripts live in?
	"
	echo $ECHOOPTION "Enter CGI directory [/home/www/httpd/cgi-bin]: $ECHOTAIL"
	read ANS
	if test "$ANS" != ""
	then
		CGIBIN="$ANS"
	else	
		CGIBIN="/home/www/httpd/cgi-bin"
	fi

	if [ ! -d "$CGIBIN" ]
	then
		echo "***---> $CGIBIN doesn't exist..."
		CGIBIN=""
	elif [ ! -w "$CGIBIN" ]
	then
		echo "***---> $CGIBIN isn't writable..."
		CGIBIN=""
	else
		WEB="OK"
		echo "---> OK... CGI scripts will live at $CGIBIN"
	fi
done

#--------------------------------------------------------
# SET CGI SCRIPTS URL LOCATION
#--------------------------------------------------------

CGIBINURL=""
while [ "$CGIBINURL" = "" ]
do
	CGIBINURL="http://$BBDISPLAY/cgi-bin"
	echo $ECHOOPTION "Enter the URL of the CGI scripts [$CGIBINURL]: $ECHOTAIL"
	read ANS
	if test "$ANS" != ""
	then
		CGIBINURL="$ANS"
	fi

	echo "---> OK... URL location of CGI scripts at $CGIBINURL"
done

fi	# "$DOWEB" = TRUE

cd $BBHOME

#--------------------------------------------------------
# NOW COPY STUFF ACROSS
#--------------------------------------------------------

echo "
--------------------------------------------------------
"
echo "--> UPDATING Makefile"

cp install/Makefile.${MACHINE} src/Makefile

# Insert OS specific -D option
# Not yet implemented
BBOSTYPE=`echo $MACHINE | tr '[a-z]' '[A-Z]'`
cat src/Makefile | sed "s/&BBOSTYPE/$BBOSTYPE/g" > tmp/Makefile.$$
mv tmp/Makefile.$$ src/Makefile

echo "--> UPDATING runbb.sh"
cat runbb.sh.DIST | sed "s:&BBHOME:$BBHOME:g" > runbb.sh
chmod 755 runbb.sh

echo "--> UPDATING bbsys.local"

cp install/bbsys.${MACHINE} etc/bbsys.local

echo "--> CHECKING COMMAND PATHNAMES"

$BBHOME/etc/bbchkcmds.sh

echo "--> UPDATING bbdef.sh"


# Insert OS specific in an env variable
cat etc/bbdef.sh.DIST | sed "s/&BBOSTYPE/$MACHINE/g" > tmp/bbdef.sh
# Insert FQDN specific into bbdef.sh
cat tmp/bbdef.sh | sed "s/&BBFQDN/$BBFQDN/g" > tmp/bbdef.sh.$$
mv tmp/bbdef.sh.$$ etc/bbdef.sh
rm tmp/bbdef.sh

if [ "$DOWEB" = "TRUE" ]
then
	# Insert WEB specific into bbdef.sh
	echo "--> UPDATING URL location"

	# URL location
	cat etc/bbdef.sh | sed "s!&BBWEB!$BBWEB!g" > tmp/bbdef.sh.$$
	# URL location of CGI scripts
	cat tmp/bbdef.sh.$$ | sed "s!&CGIBINURL!$CGIBINURL!g" > tmp/bbdef.sh
	mv tmp/bbdef.sh etc/bbdef.sh
	rm tmp/bbdef.sh.$$

	cat www/help/bb-ack.html.DIST | sed "s!&CGIBINURL!$CGIBINURL!g" > tmp/bb-ack.html.$$
	mv  tmp/bb-ack.html.$$ www/help/bb-ack.html
fi

if test "$DOWEB" = "TRUE"
then
	echo "--> INSTALLING CGI scripts"
	# setup BB's CGI scripts
	for script in bb-hist.sh bb-histlog.sh bb-ack.sh bb-hostsvc.sh
	do
		# Does the script already exists ?
		RC="Y"
		if [ -f "$CGIBIN/${script}" ]
		then
			RC=""
			while [ "$RC" != "Y" -a "$RC" != "N" ]
			do
				echo $ECHOOPTION "Overwrite the current version of $CGIBIN/${script} (y/n)?: $ECHOTAIL"
				read RC
				RC=`echo "$RC" | tr '[a-z]' '[A-Z]'`
			done
		fi
		if [ "$RC" = "Y" ]
		then
			if [ ! -f "$CGIBIN/${script}" -o -w "$CGIBIN/${script}" ]
			then
				cat web/${script}.DIST | sed "s!&&BBHOME!$BBHOME!1" > tmp/${script}.$$
				mv  tmp/${script}.$$ $CGIBIN/${script}
				chmod 755 $CGIBIN/${script}
			else
				echo "Can't create <$CGIBIN/${script}>: permission denied"
				echo "  Either fix the permissions and rerun this script"
				echo "  or create it manually (update BBHOME/BBWEB in the script)"
			fi
		fi
	done
fi

if [ "$DOPAGE" = "TRUE" ]
then
	echo "--> UPDATING bb-pager scripts"
	case ${MACHINE}
	in
		solaris )
			# Force the use of /usr/xpg4/bin if it's available
			if [ -d  /usr/xpg4/bin ]
			then
				cat bin/bb-page.sh.DIST | sed 's/\/bin\/sh/\/usr\/xpg4\/bin\/sh/1' > tmp/bb-page.sh.$$
				mv  tmp/bb-page.sh.$$ bin/bb-page.sh
				cat bin/bb-page1.sh.DIST | sed 's/\/bin\/sh/\/usr\/xpg4\/bin\/sh/1' > tmp/bb-page1.sh.$$
				mv  tmp/bb-page1.sh.$$ bin/bb-page1.sh
				cat etc/bbchkwarnrules.sh.DIST | sed 's/\/bin\/sh/\/usr\/xpg4\/bin\/sh/1' > tmp/bbchkwarnrules.sh.$$
				mv  tmp/bbchkwarnrules.sh.$$ etc/bbchkwarnrules.sh
			else
				echo "
	*** It is stronly suggested that you get the xpg4 package
	*** and install it on this server and rerun this script
	*** before continuing any further
			"
				fi
				;;
		*)
			cp bin/bb-page.sh.DIST bin/bb-page.sh
			cp bin/bb-page1.sh.DIST bin/bb-page1.sh
			cp etc/bbchkwarnrules.sh.DIST etc/bbchkwarnrules.sh
			;;
	esac
	chmod 755 bin/bb-page.sh
	chmod 755 bin/bb-page1.sh
	chmod 755 etc/bbchkwarnrules.sh

	# Insert the default recipient
	cat etc/bbwarnrules.cfg.DIST | sed "s/&DEFAULTPAGE/$DEFAULTPAGE/g" > etc/bbwarnrules.cfg
fi

echo "
--------------------------------------------------------
"
echo "--> Done.  Now do"
echo "               cd ../src"
echo "               make"
echo "               make install"

if [ "$MACHINE" = "redhat" ]
then
	echo ""
	echo "Note to RedHat 6.1 users, ping is broken, please use ping from"
	echo "http://www.rpmfind.net/linux/RPM/redhat/6.0/i386/netkit-base-0.10-29.i386.html"
fi

exit 0
