#!/bin/sh

LOG_DIR=/var/log/hp/tmp

# Default for number of days to keep old log files in /var/log/hp/tmp
LOGFILE_DAYS=1
MAXSIZE=1048576	# 1 GB

# Clears the logs which are less than 3 days.
if [ -d $LOG_DIR ]; then
	find $LOG_DIR -name \* -mtime +$LOGFILE_DAYS -print0  2>/dev/null | xargs -r -0 rm -rf
fi

USAGE=`du -c $LOG_DIR 2>/dev/null |grep total |cut -d't' -f1`

# Clears the logs if size is greater that specificed limit
while [ $USAGE -gt $MAXSIZE ]; do

	# changing the user specified LOGFILE_DAYS days to 1 days lesser.
	LOGFILE_DAYS=`expr $LOGFILE_DAYS "-" 1`

	# If same day logs are reaching Max size, deleting all log files.
	if [ $LOGFILE_DAYS -eq 0 ]; then
		find $LOG_DIR -name \* -print0 2>/dev/null | xargs -r -0 rm -rf
	else	
		find $LOG_DIR -name \* -mtime +$LOGFILE_DAYS -print0 2>/dev/null | xargs -r -0 rm -rf
	fi
	USAGE=`du -c $LOG_DIR 2>/dev/null |grep total |cut -d't' -f1`
done
