#!/bin/sh

#
# bbmv
#
# BIG BROTHER MONITORING SCRIPT
# Sean MacGuire
# Version 1.9
# Mar 13th, 2002
#
# (c) Copyright Quest Software, Inc.  1997-2002  All rights reserved.
#
# RENAME ALL OF ONE MACHINE'S INFO TO ANOTHER...
#
# You'll have to do the bb-hosts file yourself, and BB shouldn't be
# running when you do this.
#
# Format: bbmv machine1 -> machine2
#         bbmv machine.service1 -> machine.service2
# i.e.: bbmv www.bb4.com bozo.bb4.com
#       bbmv www.bb4.com bozo bizi
#

BBHOME="/bb/bb19c"
export BBHOME

if [ "$#" -lt 2 -o "$#" -gt 3 ]
then
	echo "bbmv: incorrect number of arguments"
	echo "bbmv old-hostname new-hostname"
	echo "bbmv hostname old-service new-service"
	exit 1
else
	BX1="$1"
	if [ "$#" -eq 2 ]
	then
		BX2="$2"
		OLDSVC=""
		NEWSVC=""
	else
		BX2=""
		OLDSVC="$2"
		NEWSVC="$3"
	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 "bbmv: Invalid hostname"             
	echo "Usage: bbmv old-hostname new-hostname"
	echo "       bbmv hostname old-service new-service"
	exit 1
fi

if [ "$BX2" != "" ]
then
	BBX2="`echo "$BX2" | $SED 's/[^a-zA-Z0-9_\.,:-]//g' | $SED 's/\.\./\./g'`"
	if [ "$BBX2" != "$BX2" ]
	then
		echo "bbmv: Invalid hostname"             
		echo "Usage: bbmv hostname"
		exit 1
	fi
else
	# We're only renaming a service, make sure service is valid
        TMPSVC="`echo "$OLDSVC" | $SED 's/[^a-zA-Z0-9_-]//g'`"
        if [ "$TMPSVC" != "$OLDSVC" ]
        then
                echo "bbmv: Invalid service"
		echo "Usage: bbmv old-hostname new-hostname"
		echo "       bbmv hostname old-service new-service"
                exit 1
        fi
        TMPSVC="`echo "$NEWSVC" | $SED 's/[^a-zA-Z0-9_-]//g'`"
        if [ "$TMPSVC" != "$NEWSVC" ]
        then
                echo "bbmv: Invalid service"
		echo "Usage: bbmv old-hostname new-hostname"
		echo "       bbmv hostname old-service new-service"
                exit 1
        fi
fi

#
# If we're renaming a host
#

if [ "$BX2" != "" ]
then
	#
	# 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"`
	BOX2=`echo $BX2 | $SED "s/\./,/g"`
 
	COUNT=`$LS $BBVAR/logs/$BOX2.* 2>/dev/null | $WC`
	COUNT=`echo $COUNT`

	if test "$COUNT" != "0"
	then
		echo "Can't move $BX1 - $BX2 data already exists"
		exit 1
	fi

	COUNT=`$LS $BBVAR/hist/$BOX2.* 2>/dev/null | $GREP "${BOX2}\.[^.]*$" 2>/dev/null | $WC`
	COUNT=`echo $COUNT`

	if test "$COUNT" != "0"
	then
		echo "Can't move $BX1 - $BX2 already exists"
		exit 1
	fi

	#COUNT=`$LS $BBVAR/logs/$BOX1.* 2>/dev/null | $WC`
	#COUNT=`echo $COUNT`
	#if test "$COUNT" = "0"
	#then
	#	echo "Can't move $BX1 - no data to move"
	#	exit 1
	#fi

	COUNT=`$LS $BBVAR/hist/$BOX1.* 2>/dev/null | $GREP "${BOX1}\.[^.]*$" 2>/dev/null | $WC`
	COUNT=`echo $COUNT`
	if test "$COUNT" = "0"
	then
		echo "Can't move $BX1 - no data to move"
		exit 1
	fi

	# DO THE logs DIRECTORY

	cd $BBVAR/logs
	for file in `$LS ./$BOX1.* 2>/dev/null`
	do
		set `echo $file | $SED "s/\./\ /g"`
		TEST="$2"
		echo "Moving logfile $file to $BOX2.$TEST"
		$MV -f $file $BOX2.$TEST
	done

	# DO THE hist DIRECTORY

	cd $BBVAR/hist
	for file in `$LS ./$BOX1.* 2>/dev/null | $GREP "${BOX1}\.[^.]*$" 2>/dev/null`
	do
		set `echo $file | $SED "s/\./\ /g"`
		TEST="$2"
		echo "Moving hist $file to $BOX2.$TEST"
		$MV -f $file $BOX2.$TEST
	done

	if test -f "./$BX1"
	then
		echo "Moving hist summary file $BX1 to $BX2"
		$MV -f ./$BX1 ./$BX2		# MOVE THE SUMMARY FILE AS WELL
	fi

	# DO THE histlogs DIRECTORY

	cd $BBVAR/histlogs

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

	if test -d "./$BOX1"
	then
		echo "Moving histlogs directory $BOX1 to $BOX2"
		$MV -f ./$BOX1 ./$BOX2
	fi

	# Don't forget to remove the HTML status files
	cd $BBHOME/www/html
	for file in `$LS $BX1.*.html 2>/dev/null | $GREP "${BX1}\.[^.]*.html" 2>/dev/null`
	do
		set `echo $file | $SED "s/\./\ /g"`
		# host.service.html
		shiftval=`$EXPR "$#" - 2`
		shift $shiftval
		TEST="$1"
		echo "Moving html status from $file to $BX2.$TEST.html"
        	$MV -f $file ./$BX2.$TEST.html
	done
else
	#
	# 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"`
 
	COUNT=`$LS $BBVAR/logs/$BOX1.${NEWSVC} 2>/dev/null | $WC`
	COUNT=`echo $COUNT`

	if test "$COUNT" != "0"
	then
		echo "Can't move $OLDSVC service for $BX1 - $NEWSVC service already exists"
		exit 1
	fi

	COUNT=`$LS $BBVAR/hist/$BOX1.${OLDSVC} 2>/dev/null | $WC`
	COUNT=`echo $COUNT`
	if test "$COUNT" = "0"
	then
		echo "Can't move $OLDSVC service for $BX1 - service does not exists"
		exit 1
	fi

	# DO THE logs DIRECTORY

	cd $BBVAR/logs
	echo "Moving status log $BOX1.${OLDSVC} to $BOX1.${NEWSVC}"
	$MV -f $BOX1.${OLDSVC} $BOX1.${NEWSVC}

	# DO THE hist DIRECTORY

	cd $BBVAR/hist
	echo "Moving history file $BOX1.${OLDSVC} to $BOX1.${NEWSVC}"
	$MV -f $BOX1.${OLDSVC} $BOX1.${NEWSVC}

	# DO THE histlogs DIRECTORY

	cd $BBVAR/histlogs
	BOX1=`echo $BX1 | $SED "s/\./_/g"`
	if test -d "./$BOX1/${OLDSVC}"
	then
		echo "Moving history log directory $BOX1/${OLDSVC} to $BOX1/${NEWSVC}"
		$MV -f ./$BOX1/${OLDSVC} ./$BOX1/${NEWSVC}
	fi

	# Don't forget to remove the HTML status files
	cd $BBHOME/www/html
	echo "Moving html file from $BX1.${OLDSVC}.html to $BX1.${NEWSVC}.html"
       	$MV -f $BX1.${OLDSVC}.html $BX1.${NEWSVC}.html
fi

exit 0
