#! /bin/sh

# Script designed to clean up (zip/delete) old files
# Adjust the variables below and then copy/symlink this script
# to /etc/cron/cron.{hourly,daily}

# We want to keep the filenames dated and that confuses logrotate,
# hence this script.

# Method used either AGE or FILES
METHOD="AGE"
# Maximum age of the logs
MAXAGE=120
# Maximum number of logs to keep
MAXFILES=30
# Zip all files after the first n files
ZIPAFTER=3
# Set to 1 for debug output
VERBOSE=0

# Path where the logfiles reside in
BASEPATH="/var/lib/osmo-pcap/"


# Find the client names present in basepath
# Delete files older than MAXAGE days
# Zip all but the first ZIPAFTER files
cd "$BASEPATH"


do_cleanup_age()
{
	find . -ctime +$MAXAGE -name "trace-$1-*.pcap*" |sort -r | while read LOG; do
		[ $VERBOSE -eq 1 ] && echo "Deleting file \"$LOG\""
		rm -f "$LOG"
	done
}

do_cleanup_files()
{
	i=1
	find . -name "trace-$1-*.pcap*" |sort -r | while read LOG; do
		if [ $i -gt $MAXFILES ]; then
			[ $VERBOSE -eq 1 ] && echo "Deleting file \"$LOG\""
			rm -f "$LOG"
		fi
		i=$(($i+1))
	done
}

do_zip()
{
	i=1
	find . -name "trace-$1-*.pcap*" |sort -r | while read LOG; do
		if [ $i -gt $ZIPAFTER ]; then
				if [ "${LOG##*.}" != "gz" ]; then
					[ $VERBOSE -eq 1 ] && echo "Compressing file \"$LOG\""
					gzip "$LOG"
				fi
		fi
		i=$(($i+1))
	done
}

# Use an explicit pattern here
find . -name "trace-*.pcap*" |sed -n -e "s/.*trace-\(.\+\)-[0-9]\{8\}_[0-9]\{6\}\.pcap\(\..\+\)\?/\1/p" |sort |uniq | while read CLIENT; do

	[ $VERBOSE -eq 1 ] && echo "Cleaning logs for $CLIENT"

	if [ "x$METHOD" == "xAGE" ]; then
		do_cleanup_age "$CLIENT"
	elif [ "x$METHOD" == "xFILES" ]; then
		do_cleanup_files "$CLIENT"
	else
		echo "Error, set METHOD to AGE or FILES"
		exit 1
	fi

	do_zip "$CLIENT"
done
