#!/bin/sh

# mkbb.page
# SUBPAGE SCRIPT
# RETURNS BACKGROUND COLOR AND CREATES A TEMPORARY PAGE HOST FILE
# Sean MacGuire
# Version 1.9
# Mar 13th, 2002
#
# (c) Copyright Quest Software, Inc.  1997-2002  All rights reserved.
#

#
# WE NEED AT LEAST ON ARGUMENT....
#
if [ "$#" -eq 1 ]
then
	BBHOSTLINE="$1"
else
	exit
fi

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

OLDIFS=$IFS

TMPHOSTS=$BBTMP/bb-hosts.${BBPAGENAME}.${PARPID}
$RM -f $TMPHOSTS			# GET RID OF THIS...
$TOUCH $TMPHOSTS

if [ "$BBREP" = "$BBTMP" ]
then
	$RM -f ${REPDATA}.$$
fi

$SED 1,${BBHOSTLINE}d ${BBHOSTS} > $BBTMP/mkbb.page.1.$$

LINETAGS=""
BBFILES=""
GROUPONLY="NONE" 			# WE DONT RESTRICT COLUMNS

exec < $BBTMP/mkbb.page.1.$$
while read line
do
	linestate="read"
	LINETAGS="${LINETAGS}."

	set bogus $line >/dev/null 2>&1
	shift

	case "$1"
	in

		[0-9]* )

			# SMM SANITY CHECK
			echo $line >> $TMPHOSTS

			IFS='.'
			set -f
			set $2 >/dev/null 2>&1
			set +f
			IFS=$OLDIFS
			ANAME=$1
			if test "$FQDN" = "TRUE"
			then
				shift
				for arg in $*
				do
					ANAME="$ANAME,$arg"
				done
			fi

			#
			# HANDLE COLORS IN HERE
			#
			# WE SHOULD CHECK ONLY IN SPECIFIC LOGS
			# WHEN group-only IS SPECIFIED
			case "$GROUPONLY" in
			NONE )
				case "$BBTMP" in
				$BBREP )
					$GREP "^${ANAME}\.[^ 	]*[ 	]" $REPDATA  >>${REPDATA}.$$
					;;
				* )
					BBFILES="${BBFILES} ${ANAME}.*"
					;;
				esac
				;;
			* )
				# BUILD THE LIST OF LOG FILES FOR THAT HOST
				hostlogs=""
				for group in $GROUPONLY
				do
					if [ -f "$BBLOGS/${ANAME}.$group" ]
					then
						case "$BBTMP" in
						$BBREP )
							$GREP "^${ANAME}.${group}[ 	]" $REPDATA  >>${REPDATA}.$$
							;;
						* )
							BBFILES="$BBFILES ${ANAME}.$group"
							;;
						esac
					fi
				done
				;;
			esac
			;;

		group-compress | group-only | group )

			echo $line >> $TMPHOSTS

			case "$1" in
			group-only )
				case "$#" in
				1 | 0 )
					GROUPONLY="NONE"
					;;
				* )
					IFS='|'
					set $2 > /dev/null
					IFS=$OLDIFS
					GROUPONLY="$*"
					;;
				esac
				;;
			* )
				GROUPONLY="NONE"
				;;
			esac
			;;

		subpage )
			if [ "$PAGETYPE" = "subpage" ]
			then
				break
			fi
			echo $line >> $TMPHOSTS
			;;
		page )
			# Stop here, a new page begins
			break
			;;
	esac
	linestate=""
done

# If we're at end of file, insert a tag so the last line is not repeated
if [ "$linestate" != "read" ]
then
	LINETAGS="${LINETAGS}."
fi

BBBKG="green"
COLORS="red purple yellow"

if [ "$BBFILES" != "" -o "$BBREP" = "$BBTMP" ]
then
	cd $BBLOGS
	# SMM BUG... IF THERE ARE NO LOGFILES FOR A GIVEN LINE
	# IN THE bb-hosts FILE, THEN THE FILENAME IS JUST LISTED
	# AS somehost,bb4,com.* - AND THE '*' SCREWS UP THE grep
	BBFILES=`$LS $BBFILES 2>/dev/null`		# SMM TEST
	for COLOR in $COLORS
	do
		case "$BBREP" in
		$BBTMP )
			$GREP "${COLOR}[ 	]" ${REPDATA}.$$ >/dev/null 2>&1
			;;
		* )
			$GREP "^${COLOR} " $BBFILES >/dev/null 2>&1
			;;
		esac
		case "$?" in
		0 )
			BBBKG="$COLOR" 		# SET THE COLOR
			break
			;;
		esac
	done
fi

#
# MAKE SURE THE LAST GROUP GETS HANDLED OK...
#
if test "$BBBKG"
then
	echo $BBBKG
fi

# Reset the file position
echo "$LINETAGS" > $BBTMP/BBSKIP2.${BBPAGENAME}.${PARPID}

$RM -f $BBTMP/mkbb.page.1.$$
if [ "$BBREP" = "$BBTMP" ]
then
	$RM -f ${REPDATA}.$$
fi

exit 0			# LEAVE, NOW!
