#!/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

STARTDATE=$(date -u +%s)

#
# detect network/mirror problems
#

newline="
"
tab="	"
PATTERN=
DISPLAY_PATTERN=

add_pattern()
{
	PATTERN="${PATTERN:+${PATTERN}|}($1)"
	DISPLAY_PATTERN="${DISPLAY_PATTERN:+${DISPLAY_PATTERN}${newline}}${tab}$1"
}

add_pattern "Cannot initiate the connection to"
add_pattern "Hash Sum mismatch"
add_pattern "Failed to fetch.*Could not resolve"
add_pattern "Failed to fetch.*Something wicked happened resolving"
add_pattern "Failed to fetch.*Unable to connect to"
add_pattern "E: Method http has died unexpectedly"
add_pattern "Some index files failed to download, they have been ignored, or old ones used instead."
add_pattern "Some index files failed to download. They have been ignored, or old ones used instead."
add_pattern "W: GPG error: .* Release: The following signatures were invalid: BADSIG"
add_pattern "W: GPG error: .* InRelease: The following signatures were invalid: NODATA"
add_pattern "W: GPG error: .* InRelease: Clearsigned file isn't valid, got 'NODATA'"
add_pattern "E: The repository '.*' does no longer have a Release file."
add_pattern "E: The repository '.*' no longer has a Release file."
add_pattern "E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing"
add_pattern "E: Version '.*' for '.*' was not found"
add_pattern 'E: Method file has died unexpectedly!'
add_pattern "E: Sub-process rred received a segmentation fault."
add_pattern "ERROR: Command failed \(status=-7\):.*'apt-get', 'update'"
add_pattern 'WARNING: The following packages cannot be authenticated!'
add_pattern 'Package .* .* not found in .*, .* is available'
add_pattern "AppStream system cache was updated, but problems were found: Metadata files have errors"

FILE=`mktemp`
for SECTION in $SECTIONS ; do
	test -d $MASTER/$SECTION || continue
	for subdir in fail bugged affected untestable ; do
		test -d $MASTER/$SECTION/$subdir || continue
		grep -r -l -E --include '*.log' "$PATTERN" $MASTER/$SECTION/$subdir >> $FILE
	done
done
if [ -s $FILE ] ; then
	FINALDATE=$(date -u +%s)
	RUNTIME=$(date -u -d "0 $FINALDATE seconds - $STARTDATE seconds" +%T)
	echo "Runtime: $RUNTIME"
	echo
	echo "Network problems on detected!"
	echo "(By grep'ing for"
	echo "$DISPLAY_PATTERN"
	echo "in failed logs.)"
	echo
	echo "The following logfiles have been deleted:"
	echo
	echo "----------------------------------------------------------------------"
	echo
	for log in $(sort -u $FILE)
	do
		echo "$log"
		grep -E "$PATTERN" "$log" | perl -e 'print grep !$seen{$_}++, <>;' | head -n 10 | sed "s/^/${tab}/"
		rm -f "$log"
	done
	echo
fi
rm $FILE
