#!/bin/sh

# mkbb.page
# SUBPAGE SCRIPT
# RETURNS BACKGROUND COLOR AND CREATES A TEMPORARY PAGE HOST FILE
# 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

#
# WE NEED AT LEAST ON ARGUMENT....
#
if test "$#" = 1
then
	BBHOSTLINE="$1"
else
	exit
fi

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

BBHOSTEND=`$CAT $BBHOME/etc/bb-hosts | $WC`
BBHOSTEND=`expr $BBHOSTEND + 0`

$RM -f $BBTMP/bb-hosts			# GET RID OF THIS...
COLORS="yellow purple red"
BBBKG="green"

cat $BBHOSTS | $SED 1,${BBHOSTLINE}d |
while read line
do
	BBHOSTLINE=`expr $BBHOSTLINE + 1`

	echo $line | $GREP "^page" > /dev/null
	if test "$?" = 0
	then
		if test "$BBBKG"
		then
			echo "$BBHOSTLINE" > $BBTMP/BBSKIP
			echo $BBBKG
		fi
		exit
	fi

	# SMM SANITY CHECK
	echo $line | $GREP "^[0-9]" > /dev/null
	if test "$?" = "0"
	then
		echo $line >> $BBTMP/bb-hosts

		set $line > /dev/null

		if test "$FQDN" = "TRUE"
       		then
               		MACHINE=`echo $2 | $SED "s/\./,/g"`
       		else
               		set `echo $2 | $SED "s/\./ /"`
               		MACHINE=$1
		fi
		#
		# HANDLE COLORS IN HERE
		#
		for COLOR in $COLORS
		do
			$GREP "^${COLOR}" $BBLOGS/${MACHINE}.* > /dev/null

			if test "$?" = "0"
			then
				BBBKG="$COLOR" 		# SET THE COLOR
        			case "$BBBKG" in
				yellow ) COLORS="purple red"
						;;
				purple ) COLORS="red"
						;;
				red )	   COLORS=""
						;;
				esac
			fi
		done

	else	# SAVE IT IF IT'S A GROUP LINE...
		echo $line | $GREP "^group" > /dev/null
		if test "$?" = "0"
		then
			echo $line >> $BBTMP/bb-hosts
		fi
	fi
	#
	# MAKE SURE THE LAST GROUP GETS HANDLED OK...
	#
	if test "$BBHOSTLINE" = "$BBHOSTEND"	# LAST LINE
	then
		if test "$BBBKG"
		then
			echo "$BBHOSTLINE" > $BBTMP/BBSKIP
			echo $BBBKG
		fi
		exit			# LEAVE, NOW!
	fi
done
