#!/bin/sh

# ext-proto
#
# BIG BROTHER - PROTOTYPE EXTERNAL SCRIPT
# Sean MacGuire
# Version 1.9
# Mar 13th, 2002
#
# (c) Copyright Quest Software, Inc.  1997-2002  All rights reserved.
#

#
# SCRIPTS IN THE BBHOME/ext DIRECTORY ARE ONLY RUN IF 
# THEY ARE DEFINED IN THE ENTRY FOR THE CURRENT HOST
# LISTED IN THE ext/bb-bbexttab FILE.
#

# 
# BBPROG SHOULD JUST CONTAIN THE NAME OF THIS FILE
# USEFUL WHEN YOU GET ENVIRONMENT DUMPS TO LOCATE 
# THE OFFENDING SCRIPT...
#
BBPROG=ext-proto; export BBPROG

#
# TEST NAME: THIS WILL BECOME A COLUMN ON THE DISPLAY
# IT SHOULD BE AS SHORT AS POSSIBLE TO SAVE SPACE...
# NOTE YOU CAN ALSO CREATE A HELP FILE FOR YOUR TEST
# WHICH SHOULD BE PUT IN www/help/$TEST.html.  IT WILL
# BE LINKED INTO THE DISPLAY AUTOMATICALLY.
#
TEST="wx"

#
# BBHOME CAN BE SET MANUALLY WHEN TESTING.
# OTHERWISE IT SHOULD BE SET FROM THE BB ENVIRONMENT
#
# BBHOME=/home/sean/bb; export BBHOME	# FOR TESTING

if test "$BBHOME" = ""
then
	echo "BBHOME is not set... exiting"
	exit 1
fi

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

#
# NOW COLLECT SOME DATA
# IN THIS CASE, IT'S THE CURRENT TEMPERATURE IN CELCIUS
# FOR MONTREAL, QUEBEC

# SELECT SOME LEVELS... GREEN IS THE DEFAULT...

WARN="10"	# GO YELLOW AT THIS LEVEL
PANIC="15"	# GO RED AND PAGE AT THIS LEVEL

# USING LYNX TO GRAB A WEB PAGE WITH THE CURRENT WEATHER ON IT
/usr/local/bin/lynx -dump -source http://weather.ec.gc.ca/forecast/yul.html | $GREP "176" | $SED "2,\$d" > /tmp/OUTPUT.$$

if test -s "/tmp/OUTPUT.$$"			# IF WE HAVE OUTPUT...
then
	set `$CAT /tmp/OUTPUT.$$`
	RESULT=`echo $1 | $SED "s/\<B\>//"`
	set `echo $RESULT | $SED "s/\./ /"`	# SHELL DOESN'T LIKE DECIMAL PTS
	RESULT="$1"

	#
	# DETERMINE RED/YELLOW/GREEN
	#
	if test "$RESULT" -ge "$PANIC"
	then
		COLOR="red"
	elif test "$RESULT" -ge "$WARN"
	then
		COLOR="yellow"
	else
		COLOR="green"
	fi
else
	COLOR="clear"
fi

#
# AT THIS POINT WE HAVE OUR RESULTS.  NOW WE HAVE TO SEND IT TO
# THE BBDISPLAY TO BE DISPLAYED...
#
	
# MACHINE NAME MUST EITHER BE A REAL MACHINE NAME, OR
# LOOK LIKE A REAL MACHINE (in the case of arbitrary measurements
# like temperature).  IF THE NAME YOU ARE USING DOESN'T EXIST
# IN THE DNS THEN IT SHOULD BE LISTED IN THE bb-hosts FILE WITH noping,
# PREFERABLY IN IT'S OWN GROUP... 

# NOTE THE COMMAS HERE - YOU NEED THEM!

MACHINE="montreal,quebec,canada"	# HAS TO BE IN A,B,C FORM

#
# THE FIRST LINE IS STATUS INFORMATION... STRUCTURE IMPORANT!
# THE REST IS FREE-FORM - WHATEVER YOU'D LIKE TO SEND...
#
LINE="status $MACHINE.$TEST $COLOR `date`
The weather from Environment Canada
`cat /tmp/OUTPUT.$$`

<A HREF=http://weather.ec.gc.ca/forecast/yul.html>
Full Report</A>"

$RM -f /tmp/OUTPUT.$$

# NOW USE THE BB COMMAND TO SEND THE DATA ACROSS
$BB $BBDISP "$LINE"			# SEND IT TO BBDISPLAY
