#!/bin/sh

#
# bbprune
#
# PROGRAM TO PRUNE BB LOGFILES BY DATE
# Sean MacGuire
# Version 1.9
# Mar 13th, 2002
#
# (c) Copyright Quest Software, Inc.  1997-2002  All rights reserved.
#
#
# WE COULD DO THIS BY DAY, BUT IT WOULD MAKE DATA INPUT CONFUSING 
# BETWEEN US/EUROPE MMDD FORMATS, WHICH IN TURN COULD LEAD TO REALLY
# HORRIBLE RESULTS
#
# Format: prune YYYY MM (i.e. 2000 06 will archive everything before June 2000)
#

#
# FIRST, MAKE SURE WE KNOW WHERE BBHOME IS
#
BBHOME="&&BBHOME"
export BBHOME

if test "$#" != "2"
then
	echo "Format: prune YYYY MM"
	exit 1
else

	DA="01"		# BY DEFINITION!
	YR=$1
	MO=$2

	case $MO 
	in
	1|01) MON="Jan"
		;;
	2|02) MON="Feb"
		;;
	3|03) MON="Mar"
		;;
	4|04) MON="Apr"
		;;
	5|05) MON="May"
		;;
	6|06) MON="Jun"
		;;
	7|07) MON="Jul"
		;;
	8|08) MON="Aug"
		;;
	9|09) MON="Sep"
		;;
	10) MON="Oct"
		;;
	11) MON="Nov"
		;;
	12) MON="Dec"
		;;
	*) MON=""
		;;
esac

	if test "$MON" = ""
	then
		echo "Error - invalid month $MO"
		exit 2
	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
	
	PDATE=`$BBHOME/bin/touchtime -x "$MON $DA $YR"`
	echo "Archiving data prior to $MON $YR [$PDATE]"
fi

ARCHIVE="$BBVAR/archive"

#
# NEXT, MAKE SURE WE HAVE AN ARCHIVE DIRECTORY AVAILABLE
#
if test ! -d $ARCHIVE
then
	mkdir $ARCHIVE
	if test "$?" != "0"
	then
		echo "Couldn't make archive directory - perms trouble?"
		exit 1
	fi
fi

#
# NOW THE WORK BEGINS
#
YRMO="${YR}${MO}"
mkdir $ARCHIVE/$YRMO
if test "$?" != "0"
then
	echo "Couldn't make directory - can't continue"
	exit 3
fi

#
# THIS PILE OF UGLINESS IS BECAUSE touchtime CAN'T ACCEPT AN epoch date
# AS AN ARGUMENT, i.e. TO CREATE A FILE WITH A PARTICULAR TIMETICK...
#
OFFSET=`$BBHOME/bin/touchtime -d "$MON $DA $YR"`
OFFSET=`$EXPR \-1 \* $OFFSET`
OFFSET=`$EXPR $OFFSET \/ 60`
#echo "OFFSET IS $OFFSET"
$BBHOME/bin/touchtime $OFFSET $ARCHIVE/.$YRMO

# ARCHIVE HISTLOGS
#
# NOTICE - I'M USING cpio SINCE I CAN'T REMEMBER HOW TO GET
# tar TO READ A REALLY LONG LIST FROM stdin (THE `cat LIST`
# THING BLOWS UP WHEN THERE ARE A LOT OF FILES TO ARCHIVE
#
# TO RESTORE: cpio -idumBcv < FILENAME
#
echo "*** Processing histlogs"
cd $BBVAR/histlogs
$FIND . ! -newer $ARCHIVE/.$YRMO -type f -print > $BBTMP/histlogs.$$
$CAT $BBTMP/histlogs.$$ | cpio -ocvB > $ARCHIVE/$YRMO/histlogs.$YRMO
gzip $ARCHIVE/$YRMO/histlogs.$YRMO

echo "*** Removing old histlogs, this is very slow, be patient!"

# NOW TOAST 'EM 
for file in `$CAT $BBTMP/histlogs.$$`
do
	$RM $file
done
$RM $BBTMP/histlogs.$$

# AND REMOVE THE TAG FILE
$RM $ARCHIVE/.$YRMO

# NOW ARCHIVE THE REST OF THE IMPORTANT STUFF IN acks and hist
# THIS IS A LITTLE TRICKIER BECAUSE WE HAVE TO SPLIT EACH FILE
# BY TIMESTAMP... EACH OF WHICH HAS THEM IN A DIFFERENT POSITION.

cd $BBVAR/acks

# FIGURE OUT WHERE notifications.log SHOULD BE SPLIT
# THE TIMESTAMP IS IN THE 10th FIELD
# PARSE IT AND STORE THE LINE NUMBER
# SAME WITH THE REST OF THEM.

LINENO=1

echo "*** Processing notifications.log"

$CAT notifications.log |
while read line
do
	set $line
	shift

	# echo "TEST $9 vs $PDATE"

	if test "$9" -ge "$PDATE"
	then
#		echo "DONE AT $LINENO"
		echo $LINENO > $BBTMP/prune.notif.$$
		break		# OUR WORK IS DONE HERE.
	fi
	
	LINENO=`$EXPR $LINENO + 1`
done

if test -f "$BBTMP/prune.notif.$$"
then
	FIRSTLINE=`$CAT $BBTMP/prune.notif.$$`
	LASTLINE=`$EXPR $FIRSTLINE - 1`

#	echo "LASTLINE IS $LASTLINE"

	$CP notifications.log $BBTMP/not.$$
	if test "$LASTLINE" -gt "0"
	then
		$HEAD -$LASTLINE $BBTMP/not.$$ > $ARCHIVE/$YRMO/notifications.log.$YRMO
		$CAT $BBTMP/not.$$ | $SED "1,${LASTLINE}d" > notifications.log
	fi
	$RM $BBTMP/not.$$
	$RM $BBTMP/prune.notif.$$
	if test -f $ARCHIVE/$YRMO/notifications.log.$YRMO
	then
		gzip $ARCHIVE/$YRMO/notifications.log.$YRMO
	fi
fi

echo "*** Processing recoveries.log"

LINENO=1

$CAT recoveries.log |
while read line
do
	set $line
	if test "$#" = "10"	# 10 ARGS IN A LINE
	then
		shift;
	fi

#	echo "TEST $8 vs $PDATE"

	if test "$8" -ge "$PDATE"
	then
#		echo "DONE AT $LINENO"
		echo $LINENO > $BBTMP/prune.recoveries.$$
		break		# OUR WORK IS DONE HERE.
	fi
	
	LINENO=`$EXPR $LINENO + 1`
done

if test -f "$BBTMP/prune.recoveries.$$"
then
	FIRSTLINE=`$CAT $BBTMP/prune.recoveries.$$`
	LASTLINE=`$EXPR $FIRSTLINE - 1`

#	echo "LASTLINE IS $LASTLINE"

	$CP recoveries.log $BBTMP/recoveries.$$
	if test "$LASTLINE" -gt "0"
	then
		$HEAD -$LASTLINE $BBTMP/recoveries.$$ > $ARCHIVE/$YRMO/recoveries.log.$YRMO
		$CAT $BBTMP/recoveries.$$ | $SED "1,${LASTLINE}d" > recoveries.log
	fi

	$RM $BBTMP/recoveries.$$
	$RM $BBTMP/prune.recoveries.$$
	if test -f $ARCHIVE/$YRMO/recoveries.log.$YRMO
	then
		gzip $ARCHIVE/$YRMO/recoveries.log.$YRMO
	fi
fi

echo "*** Processing recoverymsgs.log"

LINENO=1

$CAT recoverymsgs.log |
while read line
do
	set $line
	if test "$#" = "16"	# 16 ARGS IN A LINE
	then
		shift;
	fi

#	echo "TEST $9 vs $PDATE"

	if test "$9" -ge "$PDATE"
	then
#		echo "DONE AT $LINENO"
		echo $LINENO > $BBTMP/prune.recoverymsgs.$$
		break		# OUR WORK IS DONE HERE.
	fi
	
	LINENO=`$EXPR $LINENO + 1`
done

if test -f "$BBTMP/prune.recoverymsgs.$$"
then
	FIRSTLINE=`$CAT $BBTMP/prune.recoverymsgs.$$`
	LASTLINE=`$EXPR $FIRSTLINE - 1`

#	echo "LASTLINE IS $LASTLINE"

	$CP recoverymsgs.log $BBTMP/recoverymsgs.$$
	if test "$LASTLINE" -gt "0"
	then
		$HEAD -$LASTLINE $BBTMP/recoverymsgs.$$ > $ARCHIVE/$YRMO/recoverymsgs.log.$YRMO
		$CAT $BBTMP/recoverymsgs.$$ | $SED "1,${LASTLINE}d" > recoverymsgs.log
	fi
	$RM $BBTMP/recoverymsgs.$$
	$RM $BBTMP/prune.recoverymsgs.$$
	if test -f $ARCHIVE/$YRMO/recoverymsgs.log.$YRMO
	then
		gzip $ARCHIVE/$YRMO/recoverymsgs.log.$YRMO
	fi
fi

echo "*** Processing acklog"

LINENO=1

$CAT acklog |
while read line
do
	set $line

#	echo "TEST $1 vs $PDATE"

	if test "$1" -ge "$PDATE"
	then
#		echo "DONE AT $LINENO"
		echo $LINENO > $BBTMP/prune.acklog.$$	
		break; 		# OUR WORK IS DONE HERE.
	fi
	
	LINENO=`$EXPR $LINENO + 1`
done

if test -f "$BBTMP/prune.acklog.$$"
then
	FIRSTLINE=`$CAT $BBTMP/prune.acklog.$$`
	LASTLINE=`$EXPR $FIRSTLINE - 1`

#	echo "LASTLINE IS $LASTLINE"

	$CP acklog $BBTMP/acklog.$$
	if test "$LASTLINE" -gt "0"
	then
		$HEAD -$LASTLINE $BBTMP/acklog.$$ > $ARCHIVE/$YRMO/acklog.$YRMO
		$CAT $BBTMP/acklog.$$ | $SED "1,${LASTLINE}d" > acklog
	fi
	$RM $BBTMP/acklog.$$
	$RM $BBTMP/prune.acklog.$$
	if test -f $ARCHIVE/$YRMO/acklog.$YRMO
	then
		gzip $ARCHIVE/$YRMO/acklog.$YRMO
	fi
fi

#
# NOW DO THE CONTENTS OF THE hist DIRECTORY
#
# 3 FORMATS
# allevents HAS THE TIMESTAMP IN THE 3rd FIELD
# summary-by-machine (i.e. www.bb4.com) has the timestamp in the 2nd field
# individual row.col tests have the results in the 7th field
# NORMALIZE EVERYTHING SO THE DATESTAMP IS IN THE SECOND COLUMN

mkdir $ARCHIVE/$YRMO/hist.$YRMO			# MAKE THE HISTORY DIR
if test "$?" != "0"
then
        echo "Couldn't make directory - can't continue"
        exit 4
fi

cd $BBVAR/hist
for file in `$LS`
do
	echo "*** Processing hist: $file"
	$RM -f $BBTMP/prune.$file.$$ $BBTMP/$file.$$
	LINENO=1
	$CAT $file |
	while read line
	do
        	set $line

		if test "$#" = "8"
		then
			if test "$file" = "allevents"
			then
				shift;
			else
				shift; shift; shift; shift; shift;
			fi
		elif [ "$3" -lt 32 ]	# if a day then it's a host.service file
		then
			if test "$file" != "allevents"
			then
				shift; shift; shift; shift; shift;
			fi
		fi

		# echo "TEST $2 vs $PDATE"

		if test "$2" -ge "$PDATE"
        	then
			# echo "DONE AT $LINENO"
                	echo $LINENO > $BBTMP/prune.$file.$$
                	break;          # OUR WORK IS DONE HERE.
		fi
        	LINENO=`$EXPR $LINENO + 1`
	done

	if test -f "$BBTMP/prune.$file.$$"
	then
        	FIRSTLINE=`$CAT $BBTMP/prune.$file.$$`
        	LASTLINE=`$EXPR $FIRSTLINE - 1`

	#       echo "LASTLINE IS $LASTLINE"
	
        	$CP $file $BBTMP/$file.$$
        	if test "$LASTLINE" -gt "0"
        	then
                	$HEAD -$LASTLINE $BBTMP/$file.$$ > $ARCHIVE/$YRMO/hist.$YRMO/$file
                	$CAT $BBTMP/$file.$$ | $SED "1,${LASTLINE}d" > $file
        	fi
        	$RM $BBTMP/$file.$$
        	$RM $BBTMP/prune.$file.$$
	fi
done

#
# NOW TAR UP AND COMPRESS THE HIST FILE AND REMOVE THE BITS...
#
cd $ARCHIVE/$YRMO
tar cvf hist.$YRMO.tar hist.$YRMO 
gzip hist.$YRMO.tar
$RM -rf hist.$YRMO
