#!/bin/sh

NAME=tomcat8
DEFAULT=/etc/default/$NAME
LOGEXT="log txt"

# The following variables can be overwritten in $DEFAULT

# Default for number of days to keep old log files in /var/log/tomcatN/
LOGFILE_DAYS=14
# Whether to compress logfiles older than today's
LOGFILE_COMPRESS=1

# End of variables that can be overwritten in $DEFAULT

# overwrite settings from default file
if [ -f "$DEFAULT" ]; then
	. "$DEFAULT"
fi

if [ -d /var/log/$NAME ]; then
	for EXT in $LOGEXT; do
		# Compress the log files
		if [ $LOGFILE_COMPRESS = 1 ]; then
			find /var/log/$NAME/ -name \*.$EXT -daystart -mtime +0 -print0 \
				| xargs --no-run-if-empty -0 gzip -9
			EXT=$EXT.gz
		fi

		# Remove the log files older than LOGFILE_DAYS
		find /var/log/$NAME/ -name \*.$EXT -mtime +$LOGFILE_DAYS -print0 \
			| xargs --no-run-if-empty -0 rm --
	done
fi
