#!/bin/sh

B="[1m"
N="[0m"
LOGDIR=/usr/farm/log
SAVEDIR=/usr/farm/SAVEDlog
umask 022

##################################
#Place files in exempt list here.
##################################
EXEMPT_LIST="fdei01 sdssdp2 sdssdp3"

##########################
# Echo only if DEBUG set
##########################
if [ "$1" = "DEBUG" ];
then
	DEBUG=ON
fi

##########################
# Create Savedir area 
##########################
if [ ! -d $SAVEDIR ]
then
	mkdir -p $SAVEDIR
fi


###################################################
# Set hostname and save-file series number (0..6)
###################################################
HOST=`hostname`
SERIES="`date +%w`"


#####################################################
# Prune all Saved logs that are larger than 2300k
#####################################################
cd $SAVEDIR
for file in `find . -size +2300k -print | cut -c3- `
do
	base="`echo $file | cut -d\. -f1`"
	if [  `expr match "${EXEMPT_LIST}"  \.\*${base}` -eq 0 ]
	then
		if [ "$DEBUG" = "ON" ]
		then
			echo "pruning file: $file size=`ls -l $file | awk '{print $5}'`"
		else
			tail -100 $file > ${file}.prune
			cp ${file}.prune $file
			rm $file.prune
		fi
	fi
done



#####################################################
# Save Log files to $SAVEDIR.  Mail admin if log
# file is too large.  Admin will have till next run
# to save file somewhere else.
#####################################################
cd $LOGDIR
for file in `find . -size +2300k -print | cut -c3- `
do
	cat <<- EOF | Mail -s "Log too large for $file" root@${file}

	********************************************
	Logfile       = ${SAVEDIR}/${file}.${SERIES}  
	Console Server= $HOST
	********************************************

	Dear root,
		Your console log file for machine $file  on 
	console server $HOST is over 2300k in length.  
	The file will be saved as: 

			${SAVEDIR}/${file}.${SERIES}  

	If this file, is not pruned down to under 2300k by 
	today (`date +"%a %b %d"` 23:59:59 CDT ) it will be 
	pruned for you. If you need to save this file, please 
	save it somewhere else (on some other machine).
	EOF
done

for file in *
do
	cp $file ${SAVEDIR}/${file}.${SERIES}
	cp /dev/null $file
done
