#!/bin/sh

# Copyright 2009-2011 Holger Levsen (holger@layer-acht.org)
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA


PIUPARTS_CONF=${PIUPARTS_CONF:-/etc/piuparts/piuparts.conf}
[ -f "$PIUPARTS_CONF" ] || exit 0

# usage: get_config_value VARIABLE section key [default]
get_config_value()
{
	local section key value
	test -n "$1" && test "$1" = "$(echo "$1" | tr -c -d '[:alnum:]_')" || exit 1
	section="$2"
	key="$3"
	value="$(sed -rn '\#^\['"$section"'\]#,/^\[/ {/^'"$key"'\s*=/ {s/^'"$key"'\s*=\s*//; s/\s*$//; p}}' "$PIUPARTS_CONF")"
	test -n "$value" || value="$4"
	if [ -z "$value" ]; then
		echo "'$key' not set in section [$section] of $PIUPARTS_CONF, exiting." >&2
		exit 1
	fi
	eval "$1"='"$value"'
}

get_config_value MASTER global master-directory
get_config_value SECTIONS global sections

#
# reschedule 200 oldest log files, if they are older than 180 days
# reschedule 25 oldest fail log files, if they are older than 30 days
# delete $reschedule-old-count oldest logfiles, if they are scheduled
#   for recycling and older than $expire-old-days
#   and $expire-old-days > $reschedule-old-days
#
get_config_value EXPIRE_AGE global expire-old-days 0
get_config_value AGE        global reschedule-old-days 180
get_config_value COUNT      global reschedule-old-count 200
get_config_value EXPIRE_FAIL_AGE global expire-fail-days 0
get_config_value FAIL_AGE   global reschedule-fail-days 30
get_config_value FAIL_COUNT global reschedule-fail-count 25

get_config_value AUTO_RESCHEDULE	global auto-reschedule yes


list_logs()
{
	__AGE="$1"
	__COUNT="$2"
	shift 2
	find "$@" -name "*.log" -mtime +$__AGE \
		| xargs --no-run-if-empty -n99999 -s999999 ls -dt \
		| tail -n $__COUNT
}


TOTAL=0
TOTAL_EXPIRED=0
UNSCHEDULE=0
LOGS=$(mktemp)
OBSOLETE=$(mktemp)
EXPIRED=$(mktemp)
UNSORTED=$(mktemp)
OLDPWD=$(pwd)
for SECTION in $SECTIONS ; do
	test -d $MASTER/$SECTION || continue
	cd $MASTER/$SECTION
	mkdir -p pass fail bugged affected recycle

	# Clean up obsolete rescheduling requests
	for log in $(find recycle/ -name '*.log' | cut -d"/" -f2) ; do
		for dir in pass bugged affected fail ; do
			test ! -e "$dir/$log" || continue 2
		done
		echo "recycle/$log"
	done | sort > $OBSOLETE

	# Reschedule old logs
	>$LOGS
	>$EXPIRED
	get_config_value RESCHEDULE $SECTION auto-reschedule $AUTO_RESCHEDULE
	if [ "$RESCHEDULE" = "yes" ]; then
		get_config_value _EXPIRE_AGE $SECTION expire-old-days $EXPIRE_AGE
		get_config_value _AGE        $SECTION reschedule-old-days $AGE
		get_config_value _COUNT      $SECTION reschedule-old-count $COUNT
		get_config_value _EXPIRE_FAIL_AGE $SECTION expire-fail-days $EXPIRE_FAIL_AGE
		get_config_value _FAIL_AGE   $SECTION reschedule-fail-days $FAIL_AGE
		get_config_value _FAIL_COUNT $SECTION reschedule-fail-count $FAIL_COUNT
		# FIXME: we ignore bugged here - ptyhon-bts is really the way to go
		>$UNSORTED
		if [ "$_EXPIRE_AGE" -gt "$_AGE" ]; then
			list_logs $_EXPIRE_AGE      $_COUNT      pass fail affected >> $UNSORTED
		fi
		if [ "$_EXPIRE_FAIL_AGE" -gt "$_FAIL_AGE" ]; then
			list_logs $_EXPIRE_FAIL_AGE $_FAIL_COUNT      fail affected >> $UNSORTED
		fi
		for log in $(sort -u $UNSORTED) ; do
			# the log needs to be scheduled for recycling before it gets expired
			test -f "recycle/${log#*/}" && echo "$log"
		done > $EXPIRED
		>$UNSORTED
		list_logs $_AGE      $_COUNT      pass fail affected >> $UNSORTED
		list_logs $_FAIL_AGE $_FAIL_COUNT      fail affected >> $UNSORTED
		for log in $(sort -u $UNSORTED) ; do
			# skip if already scheduled
			test -f "recycle/${log#*/}" || echo "$log"
		done > $LOGS
	fi

	if [ -s $LOGS ] || [ -s $OBSOLETE ] || [ -s $EXPIRED ]; then
		RCOUNT=$(wc -l $LOGS | awk '{ print $1 }')
		TOTAL=$(($TOTAL + $RCOUNT))
		ECOUNT=$(wc -l $EXPIRED | awk '{ print $1 }')
		TOTAL_EXPIRED=$(($TOTAL_EXPIRED + $ECOUNT))
		UCOUNT=$(wc -l $OBSOLETE | awk '{ print $1 }')
		UNSCHEDULE=$(($UNSCHEDULE + $UCOUNT))
		echo "$SECTION: $RCOUNT/$ECOUNT/$UCOUNT"
		if [ -s $LOGS ]; then
			ls -dtl $(cat $LOGS)
			ln -f $(cat $LOGS) recycle/
		fi
		if [ -s $EXPIRED ]; then
			ls -dtl $(cat $EXPIRED)
			rm -fv $(cat $EXPIRED)
		fi
		if [ -s $OBSOLETE ]; then
			rm -fv $(cat $OBSOLETE)
		fi
		echo
		echo "#########################################################"
		echo
	fi
	cd $OLDPWD
done
rm $LOGS
rm $OBSOLETE
rm $EXPIRED
rm $UNSORTED

if [ "$TOTAL" -gt "0" ]; then
	echo "Rescheduled $TOTAL logs."
fi
if [ "$TOTAL_EXPIRED" -gt "0" ]; then
	echo "Deleted $TOTAL_EXPIRED expired logs."
fi
if [ "$UNSCHEDULE" -gt "0" ]; then
	echo "Cancelled $UNSCHEDULE outdated rescheduling requests."
fi
