#!/bin/sh

# Copyright © 2009-2017 Holger Levsen (holger@layer-acht.org)
# Copyright © 2011-2017 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, see <https://www.gnu.org/licenses/>


. /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

ISSUE_LOG="$MASTER/archive_issues.txt"

STARTDATE=$(date -u +%s)

#
# 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
	URL_SED="s#$MASTER/$SECTION/fail#$URLBASE/$SECTION/untestable#"
	mkdir -p $MASTER/$SECTION/fail/ $MASTER/$SECTION/untestable/ $MASTER/$SECTION/recycle/
	# '-mtime +0' actually means '24 hours or older'
	find $MASTER/$SECTION/fail -name '*.log' -mtime +0 | 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
			URL=$(echo "$package_log" | sed "$URL_SED")
			if ! grep -q "$URL" $ISSUE_LOG ; then
				# immediately recycle upon first appearance
				ln -f $package_log $MASTER/$SECTION/recycle/
			fi
			mv $package_log $MASTER/$SECTION/untestable/
		done
		sed "$URL_SED" $LOGS >> $URLS
	fi
done
if [ -s $URLS ]; then
	date >> $ISSUE_LOG
	cat $URLS >> $ISSUE_LOG
	FINALDATE=$(date -u +%s)
	RUNTIME=$(date -u -d "0 $FINALDATE seconds - $STARTDATE seconds" +%T)
	echo "Runtime: $RUNTIME"
	echo
	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 $ISSUE_LOG | sort | uniq -c | sort -rn
	echo
fi
rm $LOGS $URLS
