#!/bin/sh

# runbb.sh
# BIG BROTHER MAIN SCRIPT
# Sean MacGuire - The MacLawran Group Inc.
# Version 1.4
# Mar 05th, 2000
#
# This program is Copyright (c) 1997-2000
# The MacLawran Group Inc.
# All Rights Reserved
#
# THE MOTHER OF ALL SHELL SCRIPTS...
#

#
# BBHOME IS THE ROOT DIRECTORY OF BIG BROTHER!!!
# YOU MUST SET THE NEXT LINE TO WHERE BB LIVES

BBHOME="&BBHOME"
export BBHOME

if [ "$BBHOME" = "" -o ! -d "$BBHOME" ]
then
	echo "BBHOME IS INVALID IN runbb.sh"
	echo "Please edit runbb.sh to correct this problem!"
	exit 1
fi

BBPID=""

ACTION="$1"
if [ -z "$ACTION" ]
then
	ACTION="start"
fi

. $BBHOME/etc/bbdef.sh		# GET STANDARD INCLUDES

case $ACTION in

	stop )

		echo "Stopping Big Brother..." 
		echo "`date`: Stopping Big Brother..." >> $BBHOME/BBOUT
		kill -9 `$CAT $BBTMP/BBPID` 
		$RM $BBTMP/BBPID
		exit 0
		;;

	restart )
	
		echo "Restarting Big Brother..." 
		echo "`date`: Restarting Big Brother..." >>  $BBHOME/BBOUT
		kill -9 `$CAT $BBTMP/BBPID`
		$RM $BBTMP/BBPID
		sleep 5
		;;

	start ) 
	
		echo "Starting Big Brother..." 
		echo "`date`: Starting Big Brother..." >>  $BBHOME/BBOUT
		if [ -f "$BBTMP/BBPID" ]
		then
			$PS | $GREP "$BBHOME/bin/bb" | $GREP -v "grep" >/dev/null 2>&1
			if [ "$?" -eq 0 ]
			then
				echo "Big Brother is already running, aborting start procedure"
				exit 1
			else
				$RM $BBTMP/BBPID	# remove the stale BBPID file
			fi
		fi
		;;

	* )

		echo "Usage: runbb.sh start|restart|stop"
		exit 1
		;;

esac

$PS | $GREP "$BBHOME" | $GREP -v "grep" >/dev/null 2>&1 
if [ "$?" -eq 0 ]
then
	echo "You seem to have rogue BB processes running, please investigate"
	echo "  They may not have been killed properly when BB was stopped"
	echo "  Startup of BB will continue anyhow"
	echo "You seem to have rogue BB processes running, please investigate" >> BBOUT
	echo "  They may not have been killed properly when BB was stopped" >> BBOUT
	echo "  Startup of BB will continue anyhow" >> BBOUT
fi

# echo "BBDISPLAY=$BBDISPLAY BBPAGER=$BBPAGER BBNET=$BBNET"

BBSLEEP=300			# HOW OFTEN TO UPDATE (IN SECONDS)
export BBSLEEP

#
# IF WE NEED THE BIG BROTHER DAEMON ON THIS MACHINE THEN START IT
#
if test "$BBSERVER" = "TRUE"
then
	echo "Starting Big Brother Daemon (bbd)..."
	echo "Starting Big Brother Daemon (bbd)..." >> $BBHOME/BBOUT
	$BBHOME/bin/bbd >> $BBHOME/BBOUT 2>&1
	sleep 2
fi

$RM -f ${BBTMP}/BB.$$
$RM -f ${PAGINGLOCK}_*		# Spotted by Martin Richard

BBSLEEP=300			# HOW OFTEN TO UPDATE LOCAL INFO (IN SECONDS)
export BBSLEEP

# echo "*** CALLING BB-LOCAL ***"
{ nohup $BBHOME/bin/bbrun $BBHOME/bin/bb-local.sh ;} >> $BBHOME/BBOUT 2>&1 &

BBSLEEP=300			# HOW OFTEN TO UPDATE NETWORK (IN SECONDS)
export BBSLEEP

if test "$BBNET" = "TRUE"
then
	# echo "*** CALLING BB-NETWORK ***"
	{ sleep 30; nohup $BBHOME/bin/bbrun $BBHOME/bin/bb-network.sh ;} >> $BBHOME/BBOUT 2>&1 &
fi

BBSLEEP=300			# HOW OFTEN TO UPDATE DISPLAY (IN SECONDS)
export BBSLEEP

if test "$BBDISPLAY" = "TRUE"
then
	# echo "*** CALLING BB-DISPLAY ***"
	> $BBHOME/www/logs/.bbstartup
	{ sleep 90; nohup $BBHOME/bin/bbrun $BBHOME/bin/bb-display.sh ;} >> $BBHOME/BBOUT 2>&1 &
fi

#
# EXECUTE LOCAL SCRIPTS FROM HERE...
# SCRIPTS SHOULD LIVE IN $BBHOME/ext DIRECTORY
# BBEXT CONTAINS THE FILENAMES TO EXECUTE
#
BBEXT=""
BBDEFSLEEP=300			# HOW OFTEN TO UPDATE (IN SECONDS)
for file in $BBEXT 
do
	# Get the interval of testing of defined (i.e. test:120 -> run every 120 seconds)
	OLDIFS=$IFS
	IFS=':'
	set $file
	IFS=$OLDIFS
	file=$1
	if [ "$#" -gt 1 ]
	then
		BBSLEEP=$2
	else
		BBSLEEP=$BBDEFSLEEP
	fi
	export BBSLEEP

	{ sleep 15; nohup $BBHOME/bin/bbrun $BBHOME/ext/$file ;} >> $BBHOME/BBOUT 2>&1 &
done

echo $BBPID >> $BBTMP/BBPID
echo "Big Brother started"
echo "Big Brother started" >> $BBHOME/BBOUT
