#!/bin/sh

#
# bbrm
#
# BIG BROTHER MONITORING SCRIPT
# Sean MacGuire
# Version 1.9
# Mar 13th, 2002
#
# (c) Copyright Quest Software, Inc.  1997-2002  All rights reserved.
#
# REMOVE MACHINE DATA - USE WITH EXTREME CAUTION
#
# You'll have to do the bb-hosts file yourself, and BB shouldn't be
# running when you do this.
#
# Format: bbrm machine1 [service]
# i.e.: bbrm bozo.bb4.com
#       bbrm bozo.bb4.com ftp
#

BBHOME="/bb/bb19c"
export BBHOME

if [ "$#" -lt 1 -o "$#" -gt 2 ]
then
	echo "bbrm: incorrect number of arguments"
	echo "bbrm name [service]"
	exit 1
else
	BX1="$1"
	if [ "$#" -eq 1 ]
	then
		BX2="*"
	else
		BX2="${2}"
	fi
fi

# echo "***** BBHOME IS SET TO $BBHOME"
# echo "***** BBTMP IS SET TO $BBTMP"

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

if test ! -d "$BBVAR"
then
	echo "I can't access BBVAR ($BBVAR)... exiting"
	exit 1
fi

# echo $BBVAR

BBX1="`echo "$BX1" | $SED 's/[^a-zA-Z0-9_\.,:-]//g' | $SED 's/\.\./\./g'`"
if [ "$BBX1" != "$BX1" ]
then
	echo "bbrm: Invalid hostname"
	echo "bbrm hostname [service]"
	exit 1
fi

# We're only removing a service, make sure service is valid
if [ "$BX2" != "*" ]
then
	BBX2="`echo "$BX2" | $SED 's/[^a-zA-Z0-9_-]//g'`"
	if [ "$BBX2" != "$BX2" ]
	then
		echo "bbrm: Invalid service"
		echo "bbrm hostname [service]"
		exit 1
	fi
fi

#
# logs, hist CONTAINS MACHINES IN THE FORM aaa,bbb,ccc.test
# hist also contains a file in the form aaa.bbb.ccc WITH ALL INFO
# histlogs contains directories named in the form aaa_bbb_ccc 
#

BOX1=`echo $BX1 | $SED "s/\./,/g"`
BOX1="./${BOX1}"				# ANCHOR IT
 
# DO THE logs DIRECTORY

cd $BBVAR/logs
for file in `$LS ${BOX1}.${BX2} 2>/dev/null`
do
	echo "Removing status log: $file"
	$RM -f $file
done

# DO THE hist DIRECTORY

cd $BBVAR/hist
for file in `$LS ${BOX1}.${BX2} 2>/dev/null | $GREP "${BOX1}\.[^.]*$" 2>/dev/null`
do
	echo "Removing history service file: $file"
	$RM -f $file 
done

if [ -f "$BX1" -a "$BX2" = "*" ]
then
	echo "Removing history host file: $BX1"
	$RM -f $BX1 		# REMOVE THE SUMMARY FILE AS WELL
fi

# DO THE histlogs DIRECTORY
cd $BBVAR/histlogs

BOX1=`echo $BX1 | $SED "s/\./_/g"`

if test -d "$BOX1"
then
	if [ "$BX2" = "*" ]
	then
		echo "Removing host history logs directory: $BOX1"
		$RM -rf $BOX1
	else
		echo "Removing service history logs directory: $BOX1/$BX2"
		$RM -rf $BOX1/$BX2
	fi
fi

# Don't forget to remove the HTML status files
cd $BBHOME/www/html
for file in `$LS ${BX1}.${BX2}.html 2>/dev/null | $GREP "${BX1}\.[^.]*$" 2>/dev/null`
do
	echo "Removing HTML file: $file"
	$RM -f $file
done

exit 0
