#!/bin/sh

# mkbb.bkg
# SHELL SCRIPT TO DETERMINE THE BACKGROUND COLOR OF THE BIG BROTHER PAGE
# BIG BROTHER CLIENT PROGRAM
# Sean MacGuire
# Version 1.9
# Mar 13th, 2002
#
# (c) Copyright Quest Software, Inc.  1997-2002  All rights reserved.
#

if test ! "$BBTMP"                      # GET DEFINITIONS IF NEEDED
then
        . $BBHOME/etc/bbdef.sh          # INCLUDE STANDARD DEFINITIONS
fi

$RM -f $BBTMP/BB.$$

#
# NOW ESTABLISH THE BACKGROUND COLOR...
# IF THERE'S ANY EMERGENCY FILES, THEN WE'RE RED
# THEN IF YELLOW
# THEN IF PURPLE
#
BKG="green"

if [ "$BBREP" != "$BBTMP" ]
then
	thisdir=`pwd`
	cd $BBLOGS
	$LS * >/dev/null 2>$BBTMP/mkbb.bkg.$$
	for COLOR in yellow purple red
	do
		rcode=1
		case "$SUMMARY_SET_BKG" in
		TRUE )
			# It now check for ^colorX where X is either a space or tab
			# Thanks to Thomas Eckert <tom@uni-freiburg.de>
			# If there's too many files (argg too long error), then split the work
			if [ -s $BBTMP/mkbb.bkg.$$ ]
			then
				set -f
				for expr in "[a-i]*" "[j-r]*" "[s-z]*" "[A-I]*" "[J-R]*" "[S-Z]*" "[0-9]*" "[^A-Za-z0-9]*"
				do
					set +f
					$GREP -l "^${COLOR}[ 	]" $expr >> $BBTMP/BB.$$ 2>/dev/null
					if [ "$?" -eq 0 ]
					then
						rcode="$?"
					fi
				done
				set +f
			else
				$GREP -l "^${COLOR}[ 	]" * >> $BBTMP/BB.$$
				rcode="$?"
			fi
			;;
		* )
			# If there's too many files (argg too long error), then split the work
			if [ -s $BBTMP/mkbb.bkg.$$ ]
			then
				set -f
				for expr in "[a-i]*" "[j-r]*" "[s-z]*" "[A-I]*" "[J-R]*" "[S-Z]*" "[0-9]*" "[^A-Za-z0-9]*"
				do
					set +f
					$GREP -l "^${COLOR}[ 	]" $expr | $GREP -v "summary." >> $BBTMP/BB.$$ 2>/dev/null
					if [ "$?" -eq 0 ]
					then
						rcode="$?"
					fi
				done
				set +f
				rcode="$?"
			else
				$GREP -l "^${COLOR}[ 	]" * | $GREP -v "summary." >> $BBTMP/BB.$$
				rcode="$?"
			fi
			;;
		esac
		case "$rcode" in
		0 )
			BKG="$COLOR" 		# SET THE COLOR
			;;
		esac
	done
	cd $thisdir
	echo $BKG
	echo $BKG > $BBLOGS/.bkg		# SAVE THE COLOR HERE
	$UNIQ $BBTMP/BB.$$ > $BBTMP/BB
	$RM -f $BBTMP/BB.$$ $BBTMP/mkbb.bkg.$$
else
	for COLOR in yellow red
	do
		$GREP "${COLOR}[ 	]" $REPDATA  > /dev/null
		case "$?" in
		0 )
			BKG="$COLOR"            # SET THE COLOR
			;;
		esac
	done
	# PRINT OUT THE COLOR
	echo $BKG 
fi

