#!/bin/sh

# bbconfig
# BIG BROTHER - AUTOMATIC CONFIG SCRIPT
# Sean MacGuire
# Version 1.9c
# May 15th, 2002

#
# (c) Copyright Quest Software, Inc.  1997-2002  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

isroot=FALSE
idline=`id 2>/dev/null`
if [ "$idline" != "" ]
then
	set $idline 2>/dev/null
	idline=`echo $1 | sed 's/=/ /' | sed 's/(/ /' | sed 's/)/ /'`
	set $idline 2>/dev/null
	BBUID=$3
	if [ "$2" -eq 1 -o "$BBUID" = "root" ]
	then
		isroot=TRUE
	fi
fi

if [ "$isroot" != "TRUE" ]
then
	echo ""
	echo "You are installing Big Brother logged in as $BBUID"
	echo "If you are installing a BBDISPLAY host, you should be installing"
	echo "    Big Brother logged in as root"
	echo $ECHOOPTION "Press any key to continue... $ECHOTAIL"
       	read ANS
fi

tput clear

if test ! -f $BBHOME/tmp/.license
then
	more $BBHOME/LICENSE
	echo

	echo $ECHOOPTION "Do you agree to the terms of this license (y/n): $ECHOTAIL"
       	read ANS

       	ANS=`echo "$ANS" | tr '[A-Z]' '[a-z]'`

       	if [ "$ANS" != "y" ]
       	then
		echo
		echo "Exiting..."
		exit 1

	else
		echo
		echo "Thank you, installation continuing..."
		sleep 2
		tput clear
		echo > $BBHOME/tmp/.license
       	fi
fi

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

WOPTION=""
case $MACHINE
in
	linux )
		# ARE WE A REDHAT MACHINE?
		grep "Red" /etc/issue >/dev/null
		if test "$?" = "0"
		then
			MACHINE="redhat"
			# bbconfig expects "Red Hat Linux release 7.3 (Valhalla)"
			# and got at least "Red" in /etc/issue
			# now, go get the version.
			set bogus `grep "Red" /etc/issue` 2>/dev/null
			shift		# remove bogus
		fi
		grep "SuSE[ 	]" /etc/issue >/dev/null
		if test "$?" = "0"
		then
			MACHINE="suse"
			# bbconfig expects "Welcome to SuSE Linux 7.3 (i386) - Kernel ..."
			# and got at least "SuSE" in /etc/issue
			# now, go get the version.
			set bogus `grep "SuSE[ 	]" /etc/issue` 2>/dev/null
			shift		# remove bogus
		fi
		# If MACHINE is either RedHat or SuSE then determine which
		#	version it is.  PING may need a special directive
		# SuSE - 7.0 and up need -w3 in PINGPAR1
		# RH   - 7.3 and up need -w3 in PINGPAR1
		if [ "$MACHINE" = "redhat" -o "$MACHINE" = "suse" ]
		then
			# We're expecting the version at the 5th argument,
			# so make sure there's that many arguments
			if test "$#" -ge "5"
			then
				# Remove first 4 arguments
				shift;shift;shift;shift
				OLDIFS=$IFS
				IFS='.'
				set bogus $1		# Split 7.3 argument
				IFS=$OLDIFS
				shift
				# Test if it's a digit/number
				expr "$1" + 0 >/dev/null 2>&1
				retc1=$?
				expr "$2" + 0 >/dev/null 2>&1
				retc2=$?
				if [ $retc1 -eq 0 -a $retc2 -eq 0 ]
				then
					# It is
					VERSION=$1	
					SUBVERSION=$2
					if [ "$MACHINE" = "suse" -a "$VERSION" -ge 7 ]
					then
						WOPTION="-w3"
					elif [ "$MACHINE" = "redhat" -a "$VERSION" -eq 7 -a "$SUBVERSION" -ge 3 ]
					then
						WOPTION="-w3"
					elif [ "$MACHINE" = "redhat" -a "$VERSION" -ge 8 ]
					then
						WOPTION="-w3"
					fi
				else
					VERSION=-1
					SUBVERSION=-1
				fi
			fi
		fi
		grep "Caldera" /etc/issue > /dev/null
		if test "$?" = "0"
		then
			MACHINE="caldera"
		fi
		echo "$3" | grep "mdk" 2>&1 >/dev/null
		if [ "$?" -eq 0 ]
		then
			MACHINE="mandrake"
		fi
		;;

	sunos )
		#echo "VERSION=$VERSION"
		# Real SunOS are 4.X and less, Solaris are 5.X and up
		# Thanks to  Bill Earle <bille@imsi.com>
		VERSION=`echo $VERSION | sed "s/\./ /g"`
		set bogus $VERSION 2>/dev/null
		shift
		if test "$1" -ge "5"
		then
			MACHINE="solaris"
		fi
		;;

	hp-ux )
		MACHINE="hpux"
		;;

	bsdi )
		OLDIFS=$IFS
		IFS='.'
		set $3 >/dev/null 2>&1
		IFS=$OLDIFS
		if [ "$#" -gt 1 -a "$1" -ge 4 ]
		then
			if [ "$1" -gt 4 -o "$2" -ge 1 ]
			then
				MACHINE="bsdi4"
			fi
		fi
		;;

	irix* )
		MACHINE=irix
		;;

	unixware* )
		VERSION=`echo $4 | sed "s/\./ /g"`
		set bogus $VERSION
		shift
		if test "$1" -ge "7"
		then
			MACHINE="unixware7"
		fi
		;;
esac

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

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

#--------------------------------------------------------
# WARN ABOUT RUNNING BB AS root
#--------------------------------------------------------

STOPROOT=""
while [ "$STOPROOT" = "" ]
do
	STOPROOT="y"

		echo "

*** WARNING: Don't run BB as root ! ***
             Executing BB as root is not recommended

"

	echo $ECHOOPTION "Prevent the execution of BB as user 'root' (y/n) [$STOPROOT]: $ECHOTAIL"
	read ANS

	ANS=`echo "$ANS" | tr '[A-Z]' '[a-z]'`

	if [ "$ANS" != "" ]
	then
		STOPROOT="$ANS"
	fi

	if [ "$STOPROOT" = "y" ]
	then
		echo "---> OK... BB is NOT ALLOWED to run as root"
		STOPROOT=TRUE
	elif [ "$STOPROOT" = "n" ]
	then
		echo "---> OK... BB is ALLOWED to run as root"
		STOPROOT=FALSE
	else
		ANS=""
		STOPROOT=""
	fi
done

BBUSER=""
while [ "$BBUSER" = "" ]
do

	echo "

	BB will only start under a designated user id. The
	startup script will verify that the current user ID
	and the designated user ID are identical.
	Note: This check is only performed during the startup script.
              It does not prevent the execution of other BB binaries/scripts
              while working using another user ID. It only prevents
              you from starting BB while working another user ID.
        " 

	BBUSER="bb"

	echo $ECHOOPTION "What will be the user ID of BB [$BBUSER]: $ECHOTAIL"
	read ANS

	ANS=`echo "$ANS"`

	if [ "$ANS" != "" ]
	then
		BBUSER="$ANS"
	fi

	if [ "$BBUSER" = "root" -a "$STOPROOT" = "TRUE" ]
	then
		echo "---> User root has been previously disallowed"
		BBUSER=""
		continue
	fi

	# Hummmmm, id should be fine for everything
	id $BBUSER >/dev/null 2>&1
	if [ "$?" -eq 0 ]
	then
		echo "---> BB will only run from user '$BBUSER'"
	else
		echo "---> User '$BBUSER' has not yet been created"
		echo "---> Don't forget to create the user '$BBUSER'"
	fi
done

#--------------------------------------------------------
# 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 OLD-STYLE DIRECTORY STRUCTURE
#-----------------------------------------------------

OLDSTYLE=""
while [ "$OLDSTYLE" = "" ]
do
	echo "

	Do you want to preserve the old style directory structure ?
	You may want to do so if you use BB extensions or
	externals that do not understand the new directory structure.
	This option is *NOT* recommend as keeping the old directory
	around represents a security risk.
	"
	ANS=""
	echo $ECHOOPTION "Old-style directory structure (y/n): [n] $ECHOTAIL"
	read ANS

	if [ "$ANS" = "" ]
	then
		OLDSTYLE="n"
	else
		if [ "$ANS" != 'y' -a "$ANS" != 'Y' -a "$ANS" != 'n' -a "$ANS" != 'N' ]
		then
			OLDSTYLE=""
		else
			OLDSTYLE="$ANS"
		fi
	fi
done

# Get the top directory in the BB structure
OLDIFS=$IFS
IFS="/"
set $BBHOME
IFS=$OLDIFS
BBTOP=""
while [ "$#" -gt 1 ]
do
        # Don't accept empty args
        if [ "$1" != "" ]
        then
                BBTOP="$BBTOP/$1"
        fi
        shift
done
BBVAR=$BBTOP/bbvar

if [ "$OLDSTYLE" = "y" -o "$OLDSTYLE" = "Y" ]
then
	ln -s $BBVAR/acks $BBHOME/www/acks
	ln -s $BBVAR/data $BBHOME/www/data
	ln -s $BBVAR/disabled $BBHOME/disabled
	ln -s $BBVAR/hist $BBHOME/www/hist
	ln -s $BBVAR/histlogs $BBHOME/www/histlogs
	ln -s $BBVAR/logs $BBHOME/www/logs
fi

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

THISHOST=`hostname 2>/dev/null`

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

	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

if [ "$BBFQDN" = "TRUE" ]
then
	OLDIFS=$IFS
	IFS="."
	set $BBDISPLAY
	IFS=$OLDIFS

	if [ "$#" -eq 1 ]
	then
		echo "
---> !!! FQDN=y while the BBDISPLAY ($BBDISPLAY) is not!
"
	fi
fi

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

if [ "$BBFQDN" = "TRUE" ]
then
	OLDIFS=$IFS
	IFS="."
	set $BBPAGER
	IFS=$OLDIFS

	if [ "$#" -eq 1 ]
	then
		echo "
---> !!! FQDN=y while the BBPAGER ($BBPAGER) is not!
"
	fi
fi

echo "
	Some questions regarding the current host
	($THISHOST) will be asked. 
"

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
	elif [ "$ANS" = 'n' -o "$ANS" = 'N' ]
	then
		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 [ "$ANS" = 'y' -o "$ANS" = 'Y' ]
	then
		DOPAGE=TRUE
	elif [ "$ANS" = 'n' -o "$ANS" = 'N' ]
	then
		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 the base URL for BB [/bb]: $ECHOTAIL"
	read ANS
	if test "$ANS" != ""
	then
		BBWEB="$ANS"
	else	
		BBWEB="/bb"
	fi
	if [ "$BBWEB" = "/" ]
	then
		rc=1
	else
		echo "$BBWEB" | grep ".*/$" >/dev/null 2>&1
		rc=$?
	fi
	if [ "$rc" -eq 0 ]
	then
		echo "
The base URL <$BBWEB> is invalid, it should not end with a '/'
"
		BBWEB=""
	else
		BBWEB=`echo $BBWEB | sed 's/[ 	]//g'`
		case "$BBWEB" in
			/* )
				BBWEBHOST="http://$BBDISPLAY"
				;;	# It's clean

			http* )
				BBWEBURL="$BBWEB"
				case $BBWEB in
					http://* )
						BBWEB=`echo "$BBWEBURL" | sed 's!^http://[^/]*/!/!g'`
						BBWEBHOST=`echo "$BBWEBURL" | sed "s!$BBWEB!!g"`
						;;
					https://* )
						BBWEB=`echo "$BBWEBURL" | sed 's!^https://[^/]*/!/!g'`
						BBWEBHOST=`echo "$BBWEBURL" | sed "s!$BBWEB!!g"`
						;;
					* )
						echo "
The base URL <$BBWEB> is invalid, it should begin with a '/',http:// or https://
"
						BBWEB=""
						continue
						;;
				esac
	
				;;

			* )
				echo "
The base URL <$BBWEB> is invalid, it should begin with a '/',http:// or https://
"
				BBWEB=""
				continue
				;;
		esac
		echo "---> OK... Big Brother will live under ${BBWEBHOST}${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="/cgi-bin"
	echo $ECHOOPTION "Enter the base URL of the CGI scripts [$CGIBINURL]: $ECHOTAIL"
	read ANS
	if test "$ANS" != ""
	then
		CGIBINURL="$ANS"
	fi

	echo "---> OK... The base URL location of CGI scripts is in $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

if [ "$MACHINE" = "suse" -o "$MACHINE" = "redhat" ]
then
	echo "--> UPDATING bbsys.local for RedHat/SuSE requiring -w3 option in PINGPAR1"
	cat etc/bbsys.local | sed "s/&WOPTION/${WOPTION}/" > tmp/bbsys.local.suse
	mv tmp/bbsys.local.suse etc/bbsys.local
fi

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.$$
# Insert if BB is prevented to run a root
cat tmp/bbdef.sh.$$ | sed "s/&STOPROOT/$STOPROOT/g" > tmp/bbdef.sh
# Insert BB's user ID into bbdef.sh
cat tmp/bbdef.sh | sed "s/&BBUSER/$BBUSER/g" > tmp/bbdef.sh.$$
mv tmp/bbdef.sh.$$ etc/bbdef.sh
rm -f tmp/bbdef.sh tmp/bbdef.sh.$$

# Insert BBHOME into bbmv & bbrm scripts
cat bin/bbrm.DIST | sed "s!&&BBHOME!$BBHOME!g" > bin/bbrm
cat bin/bbmv.DIST | sed "s!&&BBHOME!$BBHOME!g" > bin/bbmv
cat bin/bbprune.DIST | sed "s!&&BBHOME!$BBHOME!g" > bin/bbprune
chmod 755 bin/bbrm bin/bbmv bin/bbprune

# Check the availability of certain include files
# Namely, stdlib.c, string.h and unistd.h
cp src/bb.h.DIST src/bb.h
for file in stdlib.h string.h unistd.h
do
	if [ ! -f /usr/include/${file} ]
	then    
        	direc="#undef"
	else
        	direc="#define"
	fi
	FILE=`echo ${file} | tr "[a-z]/." "[A-Z]__" 2>/dev/null`
	cat src/bb.h | sed "s/&_HAVE_${FILE}_/${direc} _HAVE_${FILE}_/g" > tmp/bb.h
	mv tmp/bb.h src/bb.h
done

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

	# OOps must do BBWEBHOST before BBWEB
	# Thanks to helge.jacobsen@mch.sbs.de
	# FULL URL of BBDISPLAY
	cat etc/bbdef.sh | sed "s!&BBWEBHOST!$BBWEBHOST!g" > tmp/bbdef.sh.$$
	# Base URL location
	cat tmp/bbdef.sh.$$ | sed "s!&BBWEB!$BBWEB!g" > tmp/bbdef.sh
	# Base 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 -f 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

	cat www/help/bb-rep.html.DIST | sed "s!&CGIBINURL!$CGIBINURL!g" > tmp/bb-rep.html.$$
	mv  tmp/bb-rep.html.$$ www/help/bb-rep.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 bb-rep.sh bb-replog.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

	WEBUSER=""
	while [ "$WEBUSER" = "" ]
	do
		echo "
	BB needs to set the group name of the www/rep directory
	to the group name of the web server by using its user name
"
		echo $ECHOOPTION "Enter web server user id [nobody]: $ECHOTAIL"
		read ANS
		if test "$ANS" != ""
		then
			WEBUSER="$ANS"
		else	
			WEBUSER=nobody
		fi

		# Validate web server ID
		touch $BBHOME/tmp/bbconfig.$$
		chown $WEBUSER $BBHOME/tmp/bbconfig.$$
		if [ "$?" -ne 0 ]
		then
			echo "Invalid user id: $WEBUSER"
			WEBUSER=""
		else
			userline=`id $WEBUSER 2>/dev/null`
			if [ "$userline" = "" ]
			then
				# If "id" fails then try thru /etc/passwd
				userline=`grep "^$WEBUSER:" /etc/passwd 2>/dev/null`
				if [ "$userline" = "" ]
				then
					echo "Invalid user id: $WEBUSER"
					WEBUSER=""
				else
					OLDIFS=$IFS
					IFS=':'
					set -f
					set $userline 2>/dev/null
					set +f
					IFS=$OLDIFS
					groupid=$4
					groupline=`grep ":$4:" /etc/group 2>/dev/null`
					if [ "$groupline" != "" ]
					then
						OLDIFS=$IFS
						IFS=':'
						set -f
						set $groupline 2>/dev/null
						set +f
						IFS=$OLDIFS
						TMPWEBGROUP=$1
					else
						echo "Group name for $WEBUSER could not be determined, using nobody"
						echo "    You will manually have to change the group name of the www/rep directory
"
						TMPWEBGROUP=nobody
					fi
				fi
			else
				set $userline 2>/dev/null
				shift
				idline=`echo $1 | sed 's/=/ /' | sed 's/(/ /' | sed 's/)/ /'`
				set $idline 2>/dev/null
				TMPWEBGROUP=$3
				if [ "$TMPWEBGROUP" = "" ]
				then
					echo "Group name for $WEBUSER could not be determined, using nobody"
					echo "    You will manually have to change the group name of the www/rep directory
"
					TMPWEBGROUP=nobody
				fi
			fi
		fi
		rm $BBHOME/tmp/bbconfig.$$
	done

	WEBGROUP=""
	while [ "$WEBGROUP" = "" ]
	do
		echo "
	You may override the group name determined by the previous step.
"
		echo $ECHOOPTION "Enter group name [$TMPWEBGROUP]: $ECHOTAIL"
		read ANS
		if test "$ANS" != ""
		then
			WEBGROUP="$ANS"
		else	
			WEBGROUP="$TMPWEBGROUP"
		fi

		touch $BBHOME/tmp/bbconfig.$$
		chgrp $WEBGROUP $BBHOME/tmp/bbconfig.$$
		if [ "$?" -ne 0 ]
		then
			groupline=`grep "^$WEBGROUP:" /etc/group`
			if [ "$?" -ne 0 ]
			then
				echo "Invalid group name: $WEBGROUP"
				WEBGROUP=""
			fi
		fi
		rm $BBHOME/tmp/bbconfig.$$
	done

	# Reset permissions of www/rep directory where availability reports are saved
	echo "
--> SETTING WRITE PERMISSION FOR OWNER AND GROUP FOR www/rep"
	chmod 775 www/rep
	echo "--> CHANGING THE GROUP ID OF www/rep"
	chgrp $WEBGROUP www/rep
	if [ "$?" -ne 0 ]
	then
		echo "
The group name of the www/rep directory could not be changed to the
group name ($WEBGROUP) of the web server.  Please set manually or
otherwise the availability reporting will fail.
"
	fi

fi

if [ "$DOPAGE" = "TRUE" ]
then
	echo "--> UPDATING pager scripts"
	case ${MACHINE}
	in
		solaris )
			# Force the use of /usr/xpg4/bin if it's available
			if [ -d  /usr/xpg4/bin ]
			then
				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 etc/bbchkwarnrules.sh.DIST etc/bbchkwarnrules.sh
			;;
	esac
	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"
echo "               cd ../.."
echo "               chown -R $BBUSER bbvar bb<ver>"
echo "                    where bb<ver> is the new version's directory name"
echo "               su - $BBUSER"
echo "                    to continue installation using that user ID"

if [ "$MACHINE" = "redhat" ]
then
	echo ""
	echo "Note to RedHat 6.1 users, ping is broken, please use ping from"
	echo "http://bb4.com/netkit-base-0.10-29.i386.rpm.gz"
fi

exit 0
