#!/bin/sh

# bbconfig
# BIG BROTHER - AUTOMATIC CONFIG SCRIPT
# Sean MacGuire - The MacLawran Group Inc.
# Version 1.3
# November 7, 1999
#
# This program is Copyright (c) 1997-1999
# The MacLawran Group Inc.
# All Rights Reserved

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

PROG="$0"
OSARG="$1"
ARGNUM="$#"
NAME=`basename $0`
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
		;;

	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"

#--------------------------------------------------------
# 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 "

	Big Brother needs to know where it is installed
	This directory is known as BBHOME, and is usually
	the directory where BB was unpacked...
"

ANS=""
while test "$ANS" = ""
do
	echo $ECHOOPTION "Please enter BBHOME: [$BBHOME]: $ECHOTAIL"
	read ANS
	if test "$ANS" != ""
	then
		BBHOME=$ANS
	else
		ANS=$BBHOME
	fi
	if [ ! -d "$BBHOME" ]
	then
		echo "<$BBHOME> is not a directory"
		ANS=""
		continue
	fi
	if [ ! -w "$BBHOME" ]
	then
		echo "<$BBHOME> is not a writable directory"
		ANS=""
		continue
	fi
done
echo "---> OK, BBHOME is set to $BBHOME"

#--------------------------------------------------------
# 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

#--------------------------------------------------------
# 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 "---> OK... Big Brother will live at $BBWEB"
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..."
		echo "***---> You'll have to install the CGI scripts manually"
		echo "***---> Or re-run this script"
	elif [ ! -w "$CGIBIN" ]
	then
		echo "***---> $CGIBIN isn't writable..."
		echo "***---> You'll have to install the CGI scripts manually"
		echo "***---> Or fix the permissions and re-run this script"
	else
		WEB="OK"
		echo "---> OK... CGI scripts will live at $CGIBIN"
	fi
done
fi

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 "--> 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

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.$$
	mv tmp/bbdef.sh.$$ etc/bbdef.sh
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
	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!&&BBWEB!$BBWEB!1" > tmp/${script}.$$
				cat tmp/${script}.$$ | sed "s!&&BBHOME!$BBHOME!1" > $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
			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
			;;
	esac
	chmod 755 bin/bb-page.sh
	chmod 755 bin/bb-page1.sh
fi

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

exit 0
