#!/bin/sh

# mkrep.page
# SUBPAGE SCRIPT FOR REPORTS
# 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 test "$#" = 1
then
	BBHOSTLINE="$1"
else
	exit
fi

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

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

GROUPONLY=""				# WE DONT RESTRICT COLUMNS

cat $BBHOSTS | $SED 1,${BBHOSTLINE}d > $BBREP/mkrep.page.1.$$

LINETAGS=""
# Use this file as input
exec < $BBREP/mkrep.page.1.$$
while read line
do
        LINETAGS="${LINETAGS}."

	set -f
	set bogus $line
	set +f
	shift
	if [ "$#" -gt 0 -a "$1" != "#" ]
	then
		case $1
		in
			page* )
				break
				;;

			[0-9]* )

				# IP Address entry must have at least a host name also
				if [ "$#" -ge 2 ]
				then
					# SMM SANITY CHECK
					echo $line >> $BBREP/bb-hosts

					OLDIFS=$IFS
					IFS='.'
					set $2 >/dev/null 2>&1
					IFS=$OLDIFS

					MACHINE=$1
					if test "$FQDN" = "TRUE"
       					then
						shift
						for arg in $*
						do
							MACHINE="$MACHINE,$arg"
						done
       					else
               					MACHINE=$1
					fi

					#
					# HANDLE COLORS IN HERE
					#
					for COLOR in $COLORS
					do
						# WE SHOULD CHECK ONLY IN SPECIFIC LOGS
						# WHEN group-only IS SPECIFIED
						if [ "$GROUPONLY" = "" ]
						then
							# FIND ALL LINES WITH THE HOST + CURRENT COLOR
							$GREP "${MACHINE}\..* ${COLOR} " $REPDATA > /dev/null
							RC=$?
						else
							# BUILD THE LIST OF LOG FILES FOR THAT HOST
							hostlogs=""
							for group in $GROUPONLY
							do
								$GREP "${MACHINE}\.$group ${COLOR} " $REPDATA > /dev/null
								RC=$?
								if [ "$RC" -eq 0 ]
								then	
									break
								fi
							done
						fi

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

			group | group-compress | group-only )
				# SAVE IT IF IT'S A GROUP LINE...
				echo $line >> $BBREP/bb-hosts
				if [ "$1" = "group-only" ]
				then
					set $line > /dev/null
					if [ "$#" -lt 2 ]
					then
						GROUPONLY=""
					else
						OLDIFS=$IFS
						IFS='|'
						set $2 > /dev/null
						IFS=$OLDIFS
						GROUPONLY="$*"
					fi
				else
					GROUPONLY=""
				fi
				;;
		esac
	fi

done

$RM -f $BBREP/mkrep.page.1.$$

if test "$BBBKG"
then
	echo $BBBKG
fi

echo "$LINETAGS" > $BBREP/BBREPSKIP2

exit 0
