#!/bin/sh

# bb-enable.sh
#
# Robert-Andre Croteau
# Version 1.4
# Mar 05th, 2000
#
# This program is Copyright (c) 1997-2000
# Robert-Andre Croteau, The MacLawran Group Inc.
# All Rights Reserved
#

# Just make sure BB environment is set before executing
# Thanks to Jason Englander <jason@interl.net>

args=$1                 # just keep what's needed
argnum=$#

if [ "$BBHOME" = "" -o ! -d "$BBHOME" ]
then
	echo "BBHOME is not set"
	exit 1
fi

. $BBHOME/etc/bbdef.sh

if [ "$args" = "" ]
then
	echo "Usage: bb-disable.sh \"<host(s) regular expression> <duration>\""
	exit 1
fi

if [ "$argnum" -ne 1 ]
then
	echo "Usage: bb-enable.sh \"<host(s) regular expression>\""
	exit 1
fi

set $args

if [ "$#" -eq 0 -o "$#" -gt 1 ]
then
	echo "Usage: bb-enable.sh \"<host(s) regular expression>\""
	exit 1
fi

hostsexp=$1

# Go into the logs directory and get all of the hosts that match the regular expression
cd $BBHOME/disabled
lsregexp=`echo "$hostsexp" | $SED 's/[,\.]/\[,\.\]/g'`
$LS -1 $lsregexp 2>/dev/null | $GREP -v "^\." >$BBTMP/BBENABLE.$$ 2>/dev/null
if [ "$?" -ne 0 ]
then
	$RM -f $BBTMP/BBENABLE.$$
	echo "bb-enable.sh: No disabled hosts matched <$hostsexp>"
	exit 0
fi

#
# online message should only go to local BBDISPLAY that started this script
# and ALL BBPAGERS ... This way we're sure that there's no SPOF
# to receive an online message for the BBPAGERS.
#
BBDISP="0.0.0.0"
if [ "$BBPAGE" = "0.0.0.0" ]
then
	BBDISPLAYS="$MACHINEADDR $BBPAGERS"
else
	BBDISPLAYS="$MACHINEADDR $BBPAGE"
fi
	
$CAT $BBTMP/BBENABLE.$$ | \
while read host
do
	$BB $BBDISP "online $host"
done

$RM -f $BBTMP/BBENABLE.$$
