#!/bin/sh

# Copyright 2009 Holger Levsen (holger@layer-acht.org)
# Copyright © 2011-2013 Andreas Beckmann (anbe@debian.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


. /usr/share/piuparts/lib/read_config.sh

get_config_value MASTER global master-directory
get_config_value SECTIONS global sections
get_config_value URLBASE global urlbase https://piuparts.debian.org
get_config_value DAYS global reschedule-untestable-days 7


#
# detect packages which are/were untestable due to archive issue and mark them as such
#

LOGS=`mktemp`
URLS=`mktemp`
for SECTION in $SECTIONS ; do
	test -d $MASTER/$SECTION || continue
	mkdir -p $MASTER/$SECTION/fail/ $MASTER/$SECTION/untestable/
	find $MASTER/$SECTION/fail -name '*.log' -mtime +1 | xargs -r \
		grep -l -E "E: Broken packages|E: Unable to correct problems, you have held broken packages|E: Error, pkgProblemResolver::Resolve generated breaks" 2>/dev/null > $LOGS
	if [ -s $LOGS ]; then
		for package_log in $(cat $LOGS)
		do
			mv $package_log $MASTER/$SECTION/untestable/
		done
		sed "s#$MASTER/$SECTION/fail#$URLBASE/$SECTION/untestable#" $LOGS >> $URLS
	fi
done
if [ -s $URLS ]; then
	date >> $MASTER/archive_issues.txt
	cat $URLS >> $MASTER/archive_issues.txt
	echo "Broken packages detected!"
	echo "(By grep'ing for"
	echo "        'E: Broken packages',"
	echo "        'E: Unable to correct problems, you have held broken packages',"
	echo "        'E: Error, pkgProblemResolver::Resolve generated breaks'"
	echo "in failed logs.)"
	echo
	echo 'The following packages have been moved to $section/untestable and will be'
	echo "tested again in $DAYS days."
	echo
        echo "Broken packages are usually a temporary problem in the archive and are"
        echo "caught by other tools like britney or https://qa.debian.org/dose/debcheck.html"
        echo
	echo "If it is always the same package failing, it's likely to be an issue in the"
	echo "package."
	echo
	grep -f $URLS $MASTER/archive_issues.txt | sort | uniq -c | sort -rn
	echo
fi
rm $LOGS $URLS
