#!/bin/sh
#
# Cleanup the log files which is occupying space
#

. /etc/profile.d/SetNNEnvVar.sh

#------------------
#TimeStamp Function
#------------------
TimeStamp()
{
	echo "------------------------------------------------------------" >> $LogPath/LogsCleanup.log
	date >> $LogPath/LogsCleanup.log
	echo "------------------------------------------------------------" >> $LogPath/LogsCleanup.log
}

# ------------------------------------------------------------
#Finding and deleting Files that are greater that 16MB of size
# ------------------------------------------------------------
LogFilesCleanup(){
result=$(find $LogPath -size +16777216c)
for process in $result; do
	/nn/bin/AddNnuDiagLog -mLogsCleanup -sE -wN -v11250 "The size of $process is greater than 16MB, $process will be deleted to recover $NNLOGS partition"

 	#keeps first 20 lines of the file
 	head -20 $process > $process.head

	headsize=`du -b $process.head | cut -f1`

	echo "The head size value of the $process is $headsize" >> $LogPath/LogsCleanup.log
 	if [ $headsize -ge 2048 ]
  then
  	echo "The $process file will be removed completely as it might be a binary file" >> $LogPath/LogsCleanup.log
		/nn/bin/AddNnuDiagLog -mLogsCleanup -sE -wN -v11250 "$process.head will be deleted to recover $NNLOGS partition as it might be a binary file"
    rm -f $process.head
  fi
 	rm -f $process
done

# -----------------------------------------------------------
#Finding and rotating Files that are greater that 5MB of size
# -----------------------------------------------------------
head -12 /etc/logrotate.conf > $LogPath/logrotate.conf
echo "$LogPath/voicecti* {" >> $LogPath/logrotate.conf
echo "rotate 5"  >> $LogPath/logrotate.conf
echo "size 6M"   >> $LogPath/logrotate.conf
echo "missingok" >> $LogPath/logrotate.conf
echo "}"         >> $LogPath/logrotate.conf

/usr/sbin/logrotate $LogPath/logrotate.conf
rm -f $LogPath/logrotate.conf

# -------------------------------
# Searching and deleting old logs '*.5', '*.4', '*.3' and '*.2'
# -------------------------------
#---------------------------------------------------
# Cleaning *.Log.*
#---------------------------------------------------
LogNo=5
LOG_HISTORY=1
while [ "$LogNo" -gt "$LOG_HISTORY" ];do
	ls $LogPath/[^c]*.$LogNo* > /dev/null 2>&1
	if [ $? -eq 0 ]; then
		# Logging the Deleted Files
  	TimeStamp
  	ls $LogPath/[^c]*.$LogNo* >> $LogPath/LogsCleanup.log
  	rm -f $LogPath/[^c]*.$LogNo* $LogPath/cron*.$LogNo*
  	#echo "*.$LogNo Cleaned"
  	break;
  fi
  let "LogNo -= 1"
done

# --------------------------
# Find other files to delete
# --------------------------
# ----------------------------------
# Get rid of the oldest boot-* files.
# One of these files is created each time the system is rebooted.
# ----------------------------------
lsCommandForBootFiles="ls -1 -t -d $LogPath/boot-*"
numBootFilesToKeep=7
numBootFilesFound=`$lsCommandForBootFiles 2> /dev/null | grep -c ^`
if [ "$numBootFilesFound" -gt "$numBootFilesToKeep" ]
then
	let numBootFilesToDelete=$numBootFilesFound-$numBootFilesToKeep
	bootFilesToDelete=`$lsCommandForBootFiles | tail -$numBootFilesToDelete`
	for currentBootFile in $bootFilesToDelete
  do
  	rm -f $currentBootFile
  done
fi
}

#--------------------------
#Sub-Directory contents (under /var/log) Cleanup
#--------------------------
SubDirectoryLogsCleanup()
{
	result=$(ls -p $NNLOGS |grep "/")
	for directories in $result; do
		/nn/bin/AddNnuDiagLog -mLogsCleanup -sE -wN -v11250 "The files under $directories will be deleted to recover $NNLOGS partition"
  	rm -rf $directories*
	done

	#Logging the directories for reference
  TimeStamp
  ls -p $NNLOGS |grep "/" >> $LogPath/LogsCleanup.log
}

# --------------------
# Triggering logrotate with it's regular configuration file
# --------------------
/usr/sbin/logrotate /etc/logrotate.conf

#----------------
#CRC Logs Cleanup
#----------------
LogPath=$NNLOGS
LogFilesCleanup
SubDirectoryLogsCleanup

